Skip to content

cache.py

load()

Import cache file.

Source code in mps_youtube/cache.py
def load():
    """ Import cache file. """
    if os.path.isfile(g.CACHEFILE):

        try:

            with open(g.CACHEFILE, "rb") as cf:
                cached = pickle.load(cf)

            # Note: will be none for yewtube 0.2.5 or earlier
            version = cached.get('version')

            if 'streams' in cached:
                if version and version >= 1:
                    g.streams = cached['streams']
                    g.username_query_cache = cached['userdata']
            else:
                g.streams = cached

            if 'pafy' in cached:
                pass
                #pafy.load_cache(cached['pafy'])

            dbg(c.g + "%s cached streams imported%s", str(len(g.streams)), c.w)

        except (EOFError, IOError):
            dbg(c.r + "Cache file failed to open" + c.w)

        streams.prune()

save()

Save stream cache.

Source code in mps_youtube/cache.py
def save():
    """ Save stream cache. """
    caches = dict(
        version=CACHE_VERSION,
        streams=g.streams,
        userdata=g.username_query_cache
        #,pafy=pafy.dump_cache()
    )

    with open(g.CACHEFILE, "wb") as cf:
        pickle.dump(caches, cf, protocol=2)

    dbg(c.p + "saved cache file: " + g.CACHEFILE + c.w)