Ejemplo n.º 1
0
    def _process(self):
        """runs in a thread, careful"""
        self.output.close()
        tlog("Thread started, creating RTMPClient")
        self.rtmp = RTMPClient(self.url.encode("utf-8"), **self.options)
        try:
            self.rtmp.connect()
        except RTMPError:
            raise
            event.call_from_thread(self.chunk.file.set_offline,
                                   "cannot connect")
            return
        path = self.chunk.file.get_download_file()
        startat = 0
        try:
            if os.path.isfile(path) and self.chunk.pos > 0:
                startat = self.rtmp.resumefrom(path)
        except RTMPResumeError as e:
            tlog("no resume, beginning from start " + e.message)
        try:
            if startat > 0:
                output = open(path, "r+b")
                output.seek(self.rtmp.tell())
                self.last_index = self.rtmp.tell()
            else:
                output = open(path, "wb+")
        except (OSError, IOError) as e:
            tlog("error opening file: " + traceback.format_exc(), log.error)
            event.call_from_thread(self.chunk.file.fatal, e.strerror)
            return

        try:
            self.rtmp.connectstream(startat)
            for buf in iter(self.rtmp.read, ""):
                output.write(buf)
                if self.stopped:
                    output.close()
                    break
                ratelimit.sleep(len(buf),
                                sleepfunc=monkey.get_original("time", "sleep"))
            else:
                output.close()
                event.call_from_thread(self.finalize)
        finally:
            self.rtmp.close()
            output.close()

        tlog("download rtmp finished")
        return
Ejemplo n.º 2
0
 def _process(self):
     """runs in a thread, careful"""
     self.output.close()
     tlog("Thread started, creating RTMPClient")
     self.rtmp = RTMPClient(self.url.encode("utf-8"), **self.options)
     try:
         self.rtmp.connect()
     except RTMPError:
         raise
         event.call_from_thread(self.chunk.file.set_offline, "cannot connect")
         return
     path = self.chunk.file.get_download_file()
     startat = 0
     try:
         if os.path.isfile(path) and self.chunk.pos > 0:
             startat = self.rtmp.resumefrom(path)
     except RTMPResumeError as e:
         tlog("no resume, beginning from start " + e.message)
     try:
         if startat > 0:
             output = open(path, "r+b")
             output.seek(self.rtmp.tell())
             self.last_index = self.rtmp.tell()
         else:
             output = open(path, "wb+")
     except (OSError, IOError) as e:
         tlog("error opening file: " + traceback.format_exc(), log.error)
         event.call_from_thread(self.chunk.file.fatal, e.strerror)
         return
          
     try:
         self.rtmp.connectstream(startat)
         for buf in iter(self.rtmp.read, ""):
             output.write(buf)
             if self.stopped:
                 output.close()
                 break
             ratelimit.sleep(len(buf), sleepfunc=monkey.get_original("time", "sleep"))
         else:
             output.close()
             event.call_from_thread(self.finalize)
     finally:
         self.rtmp.close()
         output.close()
         
     tlog("download rtmp finished")
     return
Ejemplo n.º 3
0
class RTMPDownload(DownloadFunction):
    def __init__(self, url, **options):
        if pylibrtmp is None:
            raise RuntimeError("No RTMP Download support.")
        
        DownloadFunction.__init__(self, None)
        print "RTMP Download init"
        url = url.encode("utf-8")
        try:
            url, query = url.split("?", 1)
        except (TypeError, ValueError):
            options = dict()
        else:
            options = dict(urlparse.parse_qsl(query))
        if is_rtmplink(url):
            url, options = load_rtmplink(url)
        self.url = url
        self.options = options
        self.rtmp = None
        self.last_index = 0
        self.next_update = 1
        if not "tcurl" in options:
            options["tcurl"] = self.url
        if "swfurl" in options and not "swfvfy" in options:
            options["swfvfy"] = "1"
        self.thread = threadpool.ThreadPool(1)
        self.stopped = False
        
    def process(self):
        if len(self.chunk.file.chunks) > 1:
            raise RuntimeError("Must be only 1 chunk, define max_chunks = 1 in hoster.")
        if not self.chunk.file.name.endswith(".flv"):
            self.chunk.file.set_infos(name=self.chunk.file.name + ".flv")
        print "process thread"
        with transaction:
            self.chunk.end = None
        try:
            return self.thread.spawn(self._process).get()
        finally:
            self.stopped = True
            
    def _process(self):
        """runs in a thread, careful"""
        self.output.close()
        tlog("Thread started, creating RTMPClient")
        self.rtmp = RTMPClient(self.url.encode("utf-8"), **self.options)
        try:
            self.rtmp.connect()
        except RTMPError:
            raise
            event.call_from_thread(self.chunk.file.set_offline, "cannot connect")
            return
        path = self.chunk.file.get_download_file()
        startat = 0
        try:
            if os.path.isfile(path) and self.chunk.pos > 0:
                startat = self.rtmp.resumefrom(path)
        except RTMPResumeError as e:
            tlog("no resume, beginning from start " + e.message)
        try:
            if startat > 0:
                output = open(path, "r+b")
                output.seek(self.rtmp.tell())
                self.last_index = self.rtmp.tell()
            else:
                output = open(path, "wb+")
        except (OSError, IOError) as e:
            tlog("error opening file: " + traceback.format_exc(), log.error)
            event.call_from_thread(self.chunk.file.fatal, e.strerror)
            return
             
        try:
            self.rtmp.connectstream(startat)
            for buf in iter(self.rtmp.read, ""):
                output.write(buf)
                if self.stopped:
                    output.close()
                    break
                ratelimit.sleep(len(buf), sleepfunc=monkey.get_original("time", "sleep"))
            else:
                output.close()
                event.call_from_thread(self.finalize)
        finally:
            self.rtmp.close()
            output.close()
            
        tlog("download rtmp finished")
        return
        
    def reinit_progress(self):
        self.chunk.file.init_progress(self.rtmp.approximate_size, self.rtmp.loaded)
        
    def finalize(self):
        self.chunk.file.set_infos(size=self.rtmp.loaded)
        with transaction:
            self.chunk.pos = self.rtmp.loaded
            self.chunk.end = self.rtmp.loaded
        
    def commit(self):
        if not self.rtmp:
            return
        if not self.rtmp.rtmp:
            self.finalize()
            return
        loaded = self.rtmp.loaded - self.last_index
        self.chunk.file.register_speed(loaded)
        self.last_index = self.rtmp.loaded
        if self.next_update > 1:
            self.chunk.file.set_progress(self.rtmp.loaded)
            #print "set progress?", self.rtmp.loaded
        self.chunk.pos = self.rtmp.loaded
        percent = int(self.rtmp.percent * 100)
        #print percent
        if percent >= self.next_update:
            self.chunk.file.set_infos(approx_size=int(self.rtmp.approximate_size))
            self.reinit_progress()
            self.next_update += 10