jindaxia / test.data.api

0 stars 1 forks source link

data api here #1

Open jindaxia opened 7 years ago

jindaxia commented 7 years ago

@Phil65

phil65 commented 7 years ago

Thx! Will check it out at the weekend, will be away for the next 3 days.

phil65 commented 7 years ago

"from common import *" fails... That one is probably needed for GetHttpData method. Could you fix that?

phil65 commented 7 years ago

ping @jindaxia

jindaxia commented 7 years ago
import urllib2
import gzip
import StringIO
import re
import json
import traceback
def GetHttpData(url, data=None, cookie=None, headers=None):
    try:
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) {0}{1}'.
                       format('AppleWebKit/537.36 (KHTML, like Gecko) ',
                              'Chrome/28.0.1500.71 Safari/537.36'))
        req.add_header('Accept-encoding', 'gzip')
        if cookie is not None:
            req.add_header('Cookie', cookie)
        if headers is not None:
            for header in headers:
                req.add_header(header, headers[header])
        if data:
            response = urllib2.urlopen(req, data, timeout=3)
        else:
            response = urllib2.urlopen(req, timeout=3)
        httpdata = response.read()
        if response.headers.get('content-encoding', None) == 'gzip':
            httpdata = gzip.GzipFile(fileobj=StringIO.StringIO(httpdata)).read()
        response.close()
        match = re.compile('encoding=(.+?)"').findall(httpdata)
        if not match:
            match = re.compile('meta charset="(.+?)"').findall(httpdata)
        if match:
            charset = match[0].lower()
            if (charset != 'utf-8') and (charset != 'utf8'):
                httpdata = unicode(httpdata, charset).encode('utf8')
    except Exception:
        traceback.print_exc()
        httpdata = '{"status": "Fail"}'
    return httpdata
phil65 commented 7 years ago

Please update GIT.

phil65 commented 7 years ago

Since this should be a thin wrapper module, please remove all xbmc/xbmcgui imports.