Esempio n. 1
0
class HttpHandler(object):

    def __init__(self, abs_cache_path, max_cache_size_in_bytes):
        self.cache = Cache(abs_cache_path, max_cache_size_in_bytes)

    @cherrypy.expose
    def stream(self, videoid):
        logging.debug("Received stream request for videoid: " + videoid)

        cachefile = self.cache.get(videoid)

        if cachefile is not None:
            if not cachefile.isprocessing:
                logging.debug("found in cache and is not processing")
                logging.debug("streaming file... " + cachefile.absfilepath)
                return serve_file(cachefile.absfilepath, cachefile.get_mimetype())

            else:
                logging.debug("cache file with videoid {0} is currently processing".format(videoid))
                return cherrypy.NotFound

        else:
            logging.debug("No file found in cache, caching new...")
            # TODO: change pafy api key
            video = pafy.new(videoid)
            bestaudio = video.getbestaudio(preftype="m4a")
            filename = videoid + "." + bestaudio.extension
            cachefile = self.cache.create(filename)
            cachefile.isprocessing = True
            logging.debug("downloading...")
            bestaudio.download(cachefile.absfilepath, remux_audio=True)
            logging.debug("downloading done")
            cachefile.isprocessing = False
            return serve_file(cachefile.absfilepath, cachefile.get_mimetype())
Esempio n. 2
0
 def __init__(self, abs_cache_path, max_cache_size_in_bytes):
     self.cache = Cache(abs_cache_path, max_cache_size_in_bytes)