示例#1
0
def __startCacheManager(firstRun=False):
    Thread.CreateTimer(HTTP.__autoUpdateCacheTime,
                       __triggerAutoHTTPCacheUpdate)
    if "UpdateCache" in __pluginModule.__dict__:
        if firstRun:
            Thread.Create(__triggerCacheUpdate)
        else:
            Thread.CreateTimer((__cacheUpdateInterval / 2) +
                               random.randrange(__cacheUpdateInterval),
                               __triggerCacheUpdate)
        PMS.Log("(Framework) Cache manager started")
示例#2
0
def __triggerCacheUpdate():
    global UpdatingCache
    if UpdatingCache: return
    UpdatingCache = True
    __callNamed("UpdateCache", addToLog=False)
    Thread.CreateTimer(__cacheUpdateInterval, __triggerCacheUpdate)
    UpdatingCache = False
示例#3
0
def Log(msg, debugOnly=True, encoding=None):
    if not debugOnly or Plugin.Debug:
        global __logSaveScheduled
        global __logSaveBuffer

        # Try to decode the message if the encoding is specified
        try:
            if encoding is not None:
                msg = str(msg.decode(encoding).encode("utf8"))
        except:
            pass

        msg = "%s: %-40s:   %s" % (str(
            Datetime.Now().time()), Plugin.Identifier, msg)

        # Don't write to stderr on Windows, it causes issues.
        if __sys.platform != "win32":
            __sys.stderr.write("%s\n" % msg)

        Thread.Lock("Framework.Log", addToLog=False)
        if __logSaveBuffer == None:
            __logSaveBuffer = msg + "\n"
        else:
            __logSaveBuffer += msg + "\n"
        if not __logSaveScheduled:
            Thread.CreateTimer(5, __saveLog)
            __logSaveScheduled = True
        Thread.Unlock("Framework.Log", addToLog=False)
示例#4
0
def Set(key, value, addToLog=True):
    global __dict
    global __saveScheduled
    Thread.Lock("Framework.Dict", addToLog=False)
    __dict[key] = value
    if not __saveScheduled:
        Thread.CreateTimer(5, __save, addToLog=addToLog)
        __saveScheduled = True
    Thread.Unlock("Framework.Dict", addToLog=False)
示例#5
0
def __triggerAutoHTTPCacheUpdate():
    HTTP.__autoUpdateCachedPages()
    Thread.CreateTimer(HTTP.__autoUpdateCacheTime,
                       __triggerAutoHTTPCacheUpdate)
示例#6
0
def Request(url,
            values=None,
            headers={},
            cacheTime=None,
            autoUpdate=False,
            encoding=None,
            errors=None,
            addToLog=True):
    global __cache
    global __saveScheduled
    now = Datetime.Now()

    # If no cache time is given, use the default
    if cacheTime is None:
        cacheTime = __cacheTime

    # Attempt to return a cached copy, fetching again if an exception occurs
    try:
        # Make sure we don't cache POST requests
        if values == None and cacheTime > 0:
            if __cache.has_key(url):
                cachedAt = __cache[url]["CheckTime"]
                expiresAt = cachedAt + Datetime.Delta(seconds=cacheTime)
                if Datetime.Now() < expiresAt:
                    if addToLog:
                        PMS.Log(
                            "(Framework) Loaded %s from the cache (expires at %s)"
                            % (url, expiresAt))
                    return __cache[url]["Content"]
    except:
        if addToLog:
            PMS.Log(
                "(Framework) Couldn't load %s from the cache, attempting to fetch again."
            )

    # Try to fetch the page from the server
    try:
        # Encode the values
        data = None
        if values is not None: data = urllib.urlencode(values)
        h = __headers.copy()
        for header in headers:
            h[header] = headers[header]
        request = urllib2.Request(url, data, h)
        f = urllib2.urlopen(request)
        response = f.read()

        # If the response is gzipped, unzip it
        if f.headers.get('Content-Encoding') == "gzip":
            if addToLog:
                PMS.Log("(Framework) Received gzipped response from %s" % url)
            stream = StringIO.StringIO(response)
            gzipper = gzip.GzipFile(fileobj=stream)
            response = gzipper.read()
        else:
            if addToLog: PMS.Log("(Framework) Received response from %s" % url)

        # Try to decode the response if manually specified
        try:
            if not (encoding is None and errors is None):
                if encoding is None: encoding = "utf8"
                if errors is None: errors = "strict"
                response = str(
                    response.decode(encoding, errors).encode("utf8", errors))
        except:
            if addToLog:
                PMS.Log(
                    "(Framework) Unable to decode response from '%s' with codec %s"
                    % (url, encoding))

    # Handle common errors
    except urllib2.HTTPError:
        PMS.Log("(Framework) HTTPError when requesting '%s'" % url)
        return None
    except urllib2.URLError:
        PMS.Log("(Framework) URLError when requesting '%s'" % url)
        return None

    Thread.Lock("Framework.HTTPCache", addToLog=False)
    try:
        # Cache the data if required
        if response is not None and cacheTime > 0:
            item = {}
            item["Content"] = response
            item["CheckTime"] = Datetime.Now()
            if autoUpdate:
                item["UpdateTime"] = Datetime.Now() + Datetime.Delta(
                    seconds=cacheTime)
                item["CacheTime"] = cacheTime
                item["Headers"] = headers
                item["Encoding"] = encoding
                item["Errors"] = errors
            __cache[url] = item
            if addToLog: PMS.Log("(Framework) Cached response from %s" % url)

        if not __saveScheduled:
            Thread.CreateTimer(5, __save)
            __saveScheduled = True
    finally:
        Thread.Unlock("Framework.HTTPCache", addToLog=False)

    # Return the data
    return response