def run(self): progress = xbmcgui.DialogProgressBG() with closing(progress): progress.create(lang(40163)) for src, dst in self.files.iteritems(): progress.update(0, message=src) copying_thread = FileCopyingThread(src, dst, self.delete) copying_thread.start() while copying_thread.is_alive(): sleep(250) progress.update(copying_thread.progress()) if self.on_finish: self.on_finish()
def list(self, torrent): """ :type torrent: Torrent """ self.engine.uri = torrent.url files = [] try: with closing(self.engine) as t2h: t2h.start() while not files: files = t2h.list(media_types=[MediaType.VIDEO]) t2h.check_torrent_error() sleep(self.SLEEP_DELAY) except Error as e: raise self._convert_engine_error(e) return [TorrentFile(path=f.name, length=f.size, md5sum=None, index=f.index) for f in files]
def play(self, player, torrent, list_item=None, file_id=None): """ :type list_item: dict :type torrent: Torrent :type player: AbstractPlayer """ list_item = list_item or {} file_status = status = None subtitles = None if self.engine.success: try: with closing(self.engine): self.log.info("Starting TorrServe engine...") self.engine.uri = torrent.url if len(torrent.files) < 2: file_id = 0 else: file_id -= 1 self.engine.start(file_id) ready = False if self.pre_buffer_bytes: with closing(self.buffering_progress): self.log.info("Start prebuffering...") self.buffering_progress.open() state = None while not self._aborted(): sleep(self.SLEEP_DELAY) status = self.engine.status() if file_id is None: files = self.engine.list_files() if files is None: continue if not files: raise TorrServeStreamError( 33050, "No playable files detected") file_id = files[file_id].index file_status = files[file_id] self.log.info("Detected video file: %s", file_status) continue else: file_status = self.engine.file_status( file_id) if not file_status: continue if state is None: state = TorrentStatus.PREBUFFERING self.engine.start_preload( file_status.Preload) if status.TorrentStatus == State.TORRENT_WORKING: self.buffering_progress.size = self.pre_buffer_bytes * 1024 * 1024 if status.PreloadedBytes >= self.pre_buffer_bytes * 1024 * 1024: ready = True break else: self.buffering_progress.size = file_status.Size state = self._convert_state( status.TorrentStatus) self.buffering_progress.name = status.Name self.buffering_progress.update_status( state, status.LoadedSize, status.DownloadSpeed / 1024, status.UploadSpeed / 1024, status.ActivePeers, status.ConnectedSeeders) else: ready = True if ready: self.log.info("Starting playback...") # noinspection PyDeprecation with nested( closing(self.playing_progress), player.attached(player.PLAYBACK_PAUSED, self.playing_progress.open), player.attached(player.PLAYBACK_RESUMED, self.playing_progress.close)): list_item.setdefault('label', status.Name) file_status = self.engine.file_status(file_id) list_item['path'] = self.engine.make_url( file_status.Link) self.playing_progress.name = status.Name self.playing_progress.size = file_status.Size player.play(list_item, None) start = time.time() while not self._aborted() and ( player.is_playing() or time.time() - start < self.playback_start_timeout): sleep(self.SLEEP_DELAY) status = self.engine.status() file_status = self.engine.file_status(file_id) state = self._convert_state( status.TorrentStatus) self.playing_progress.update_status( state, status.LoadedSize, status.DownloadSpeed / 1024, status.UploadSpeed / 1024, status.ActivePeers, status.ConnectedSeeders) player.get_percent() # handling PLAYBACK_STOPPED and PLAYBACK_ENDED events sleep(1000) except Error as err: raise self._convert_engine_error(err) if status and file_status and status.TorrentStatus == State.TORRENT_CLOSED: return [] else: raise TorrServeStreamError(33040, "No Connection")
def play(self, player, torrent, list_item=None, file_id=None): """ :type list_item: dict :type torrent: Torrent :type player: AbstractPlayer """ list_item = list_item or {} self.engine.on_playback_resumed = player.play self.engine.on_playback_paused = player.pause self.engine.on_poll = self._poll_engine list_item.setdefault("label", torrent.name) try: with closing(self.engine) as engine: with closing(self.buffering_progress) as progress: progress.open() self.log.info("Starting AceStream engine...") progress.update_status(TorrentStatus.STARTING_ENGINE) engine.connect() self.log.info("Loading transport file...") progress.name = torrent.name progress.update_status(TorrentStatus.DOWNLOADING_METADATA) files = engine.load_data(torrent.data) if file_id is not None: if file_id not in files: raise AceStreamError(33048, "Invalid file index specified (%d)" % file_id) else: if not files: raise AceStreamError(33050, "No playable files detected") file_id = files.iterkeys().next() self.log.info("Start prebuffering...") engine.play_data(torrent.data, [file_id]) while True: status = engine.get_status() if status.url: list_item["path"] = status.url break state = self._convert_state(status) update_status = [ state, status.download, status.down_speed, status.up_speed, 0, status.peers, status.progress, ] progress.update_status(*update_status) self._poll_engine(self.POLL_DELAY) with closing(self.playing_progress) as progress: # noinspection PyDeprecation with nested( player.attached(player.PLAYBACK_PAUSED, self.playing_progress.open), player.attached(player.PLAYBACK_RESUMED, self.playing_progress.close), player.attached(player.PLAYBACK_STARTED, self.engine.on_start), player.attached(player.PLAYBACK_PAUSED, self.engine.on_pause), player.attached(player.PLAYBACK_RESUMED, self.engine.on_resume), player.attached(player.PLAYBACK_STOPPED, self.engine.on_stop), player.attached(player.PLAYBACK_SEEK, self.engine.on_seek), ): self.log.info("Starting playback...") player.play(list_item) progress.name = torrent.name progress.size = torrent.files[file_id].length start = time.time() while player.is_playing() or time.time() - start < self.playback_start_timeout: status = engine.get_status() state = self._convert_state(status) update_status = [ state, int(round(progress.size * status.progress / 100.0)), status.down_speed, status.up_speed, 0, status.peers, status.progress, ] progress.update_status(*update_status) player.get_percent() self._poll_engine(self.POLL_DELAY) # handling PLAYBACK_STOPPED and PLAYBACK_ENDED events sleep(1000) except AbortError: self.log.info("Playback aborted.") except Error as err: raise self._convert_engine_error(err) if file_id in self.engine.saved_files: return [self.engine.saved_files[file_id]] return []
def _poll_engine(self, delay): sleep(int(delay * 1000)) if self._aborted(): raise AbortError()
def play(self, player, torrent, list_item=None, file_id=None): """ :type list_item: dict :type torrent: Torrent :type player: AbstractPlayer """ list_item = list_item or {} file_status = status = None subtitles = None try: with closing(self.engine): self.log.info("Starting torrent2http engine...") self.engine.uri = torrent.url resume_file = hashlib.md5(torrent.url).hexdigest() + ".resume" self.engine.resume_file = os.path.join(self.engine.download_path, resume_file) self.engine.start(file_id or 0) ready = False if self.pre_buffer_bytes: with closing(self.buffering_progress): self.log.info("Start prebuffering...") self.buffering_progress.open() while not self._aborted(): sleep(self.SLEEP_DELAY) status = self.engine.status() self.engine.check_torrent_error(status) if file_id is None: files = self.engine.list(media_types=[MediaType.VIDEO]) if files is None: continue if not files: raise Torrent2HttpStreamError(33050, "No playable files detected") file_id = files[0].index file_status = files[0] self.log.info("Detected video file: %s", file_status) sub_files = self.engine.list(media_types=[MediaType.SUBTITLES]) if sub_files: self.log.info("Detected subtitles: %s", sub_files[0]) subtitles = sub_files[0] else: file_status = self.engine.file_status(file_id) if not file_status: continue if status.state == State.DOWNLOADING: state = TorrentStatus.PREBUFFERING self.buffering_progress.size = self.pre_buffer_bytes if file_status.download >= self.pre_buffer_bytes: ready = True break elif status.state in [State.FINISHED, State.SEEDING]: ready = True break else: self.buffering_progress.size = file_status.size state = self._convert_state(status.state) self.buffering_progress.name = status.name self.buffering_progress.update_status(state, file_status.download, status.download_rate, status.upload_rate, status.num_seeds, status.num_peers) else: while not self._aborted(): sleep(self.SLEEP_DELAY) status = self.engine.status() self.engine.check_torrent_error(status) if status.state in [State.DOWNLOADING, State.FINISHED, State.SEEDING]: ready = True break if ready: self.log.info("Starting playback...") # noinspection PyDeprecation with nested(closing(self.playing_progress), player.attached(player.PLAYBACK_PAUSED, self.playing_progress.open), player.attached(player.PLAYBACK_RESUMED, self.playing_progress.close)): list_item.setdefault('label', status.name) file_status = self.engine.file_status(file_id) list_item['path'] = file_status.url self.playing_progress.name = status.name self.playing_progress.size = file_status.size player.play(list_item, subtitles.url if subtitles else None) start = time.time() while not self._aborted() and (player.is_playing() or time.time() - start < self.playback_start_timeout): sleep(self.SLEEP_DELAY) status = self.engine.status() file_status = self.engine.file_status(file_id) state = self._convert_state(status.state) self.playing_progress.update_status(state, file_status.download, status.download_rate, status.upload_rate, status.num_seeds, status.num_peers) player.get_percent() # handling PLAYBACK_STOPPED and PLAYBACK_ENDED events sleep(1000) except Error as err: raise self._convert_engine_error(err) if status and file_status and status.state in [State.FINISHED, State.SEEDING]: files = [file_status.save_path] if subtitles and os.path.exists(subtitles.save_path): files.append(subtitles.save_path) return files return []
def play(self, player, torrent, list_item=None, file_id=None): """ :type list_item: dict :type torrent: Torrent :type player: AbstractPlayer """ list_item = list_item or {} self.engine.on_playback_resumed = player.play self.engine.on_playback_paused = player.pause self.engine.on_poll = self._poll_engine list_item.setdefault('label', torrent.name) try: with closing(self.engine) as engine: with closing(self.buffering_progress) as progress: progress.open() self.log.info("Starting AceStream engine...") progress.update_status(TorrentStatus.STARTING_ENGINE) engine.connect() self.log.info("Loading transport file...") progress.name = torrent.name progress.update_status(TorrentStatus.DOWNLOADING_METADATA) files = engine.load_data(torrent.data) if file_id is not None: if file_id not in files: raise AceStreamError(33048, "Invalid file index specified (%d)" % file_id) else: if not files: raise AceStreamError(33050, "No playable files detected") file_id = files.iterkeys().next() self.log.info("Start prebuffering...") engine.play_data(torrent.data, [file_id]) while True: status = engine.get_status() if status.url: list_item['path'] = status.url break state = self._convert_state(status) update_status = [state, status.download, status.down_speed, status.up_speed, 0, status.peers, status.progress] progress.update_status(*update_status) self._poll_engine(self.POLL_DELAY) with closing(self.playing_progress) as progress: # noinspection PyDeprecation with nested(player.attached(player.PLAYBACK_PAUSED, self.playing_progress.open), player.attached(player.PLAYBACK_RESUMED, self.playing_progress.close), player.attached(player.PLAYBACK_STARTED, self.engine.on_start), player.attached(player.PLAYBACK_PAUSED, self.engine.on_pause), player.attached(player.PLAYBACK_RESUMED, self.engine.on_resume), player.attached(player.PLAYBACK_STOPPED, self.engine.on_stop), player.attached(player.PLAYBACK_SEEK, self.engine.on_seek)): self.log.info("Starting playback...") player.play(list_item) progress.name = torrent.name progress.size = torrent.files[file_id].length start = time.time() while player.is_playing() or time.time() - start < self.playback_start_timeout: status = engine.get_status() state = self._convert_state(status) update_status = [state, int(round(progress.size * status.progress / 100.0)), status.down_speed, status.up_speed, 0, status.peers, status.progress] progress.update_status(*update_status) player.get_percent() self._poll_engine(self.POLL_DELAY) # handling PLAYBACK_STOPPED and PLAYBACK_ENDED events sleep(1000) except AbortError: self.log.info("Playback aborted.") except Error as err: raise self._convert_engine_error(err) if file_id in self.engine.saved_files: return [self.engine.saved_files[file_id]] return []