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