def loop(self): from xbmctorrent.utils import SafeDialogProgress has_resolved = False plugin.log.info("Starting torrent2http...") with closing(torrent2http.start(**self.torrent2http_options)) as t2h_instance: t2h = lambda cmd: url_get_json("http://%s/%s" % (t2h_instance.bind_address, cmd), with_immunicity=False) # Get currently selected item current_item = get_current_list_item() if plugin.id not in xbmc.getInfoLabel("ListItem.Path"): current_item = {} if not self._wait_t2h_startup(t2h): return plugin.log.info("Opening download dialog...") with closing(SafeDialogProgress(delay_create=0)) as dialog: dialog.create(plugin.name) plugin.log.info("Waiting for file resolution...") while not has_resolved: if xbmc.abortRequested or dialog.iscanceled(): return status = t2h("status") self.display_name = status["name"] if status["state"] < 3: dialog.update(int(status["progress"] * 100), *self._get_status_lines(status)) if status["state"] >= 3 and not has_resolved: # Downloading? files = t2h("ls")["file"] # files contains only one file which is downloading # plugin.log.debug(repr(files)) # sometimes files is empty if files is None or len(files) == 0: xbmc.sleep(TORRENT2HTTP_POLL) continue dialog.update(int(files[0]["buffer"] * 100.0), *self._get_status_lines(status)) if files[0]["buffer"] >= 1.0: plugin.log.info("Resolving to %s" % files[0]["url"]) has_resolved = True if not current_item.get("info") or not current_item["info"].get("title"): current_item["label"] = self.display_name current_item["path"] = files[0]["url"] plugin.set_resolved_url(current_item) break xbmc.sleep(TORRENT2HTTP_POLL) # We are now playing plugin.log.info("Now playing torrent...") with closing(OverlayText(w=OVERLAY_WIDTH, h=OVERLAY_HEIGHT, alignment=XBFONT_CENTER_X | XBFONT_CENTER_Y)) as overlay: with nested(self.attach(overlay.show, self.on_playback_paused), self.attach(overlay.hide, self.on_playback_resumed, self.on_playback_stopped)): while not xbmc.abortRequested and self.isPlaying(): overlay.text = "\n".join(self._get_status_lines(t2h("status"))) xbmc.sleep(TORRENT2HTTP_POLL) plugin.log.info("Closing Torrent player.")
def rutor_play(tid, pulsar): from copy import deepcopy from contextlib import closing from bencode import bencode, bdecode from urlparse import urljoin from xbmctorrent.magnet import generate_magnet from xbmctorrent.utils import first, SafeDialogProgress, get_quality_from_name, get_current_list_item from xbmctorrent.acestream import ace_supported with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Получение информации о раздаче...") torrent_url = urljoin(DOWNLOAD_URL, "download/%s" % tid) try: metadata = bdecode(url_get(torrent_url, headers=HEADERS)) except: import xbmcgui plugin.log.error("Unexpected error: %s" % sys.exc_info()[0]) xbmcgui.Dialog().ok(plugin.name, "Не удалось получить данные от сервера") return dialog.update(percent=100, line1="Готово") # Get currently selected item current_item = get_current_list_item() current_item.setdefault("stream_info", {}).update(get_quality_from_name(current_item['label'])) if "files" in metadata["info"] and ace_supported(): items = {} for index, info in enumerate(metadata["info"]["files"]): name = "/".join(info["path"]) if not _rutor_valid_file(name): continue items[index] = deepcopy(current_item) items[index].update({ "label" : name, "path" : plugin.url_for("torrent_play", url=torrent_url, index=index, name=name), "is_playable" : True }) items[index].setdefault("info",{}).update({ "title" : name, }) # start playback if torrent contains only one file if len(items) == 1: index, item = items.popitem() if not plugin.get_setting("force_ace", bool): item["path"] = plugin.url_for("play", uri=generate_magnet(metadata, item["label"])) plugin.play_video(item) yield item else: plugin.add_sort_method('label') for i in items.values(): yield i else: name = metadata["info"].get("name") or " / ".join(first(metadata["info"]["files"])["path"]) or "rutor.org" if plugin.get_setting("force_ace", bool) and ace_supported(): current_item["path"] = plugin.url_for("torrent_play", url=torrent_url, index=0, name=name) else: current_item["path"] = plugin.url_for(["play","play_with_pulsar"][int(pulsar)], uri=generate_magnet(metadata, name)) current_item["is_playable"] = True plugin.play_video(current_item) yield current_item
def rutracker_play(tid, pulsar): from copy import deepcopy from contextlib import closing from bencode import bencode, bdecode from urlparse import urljoin from xbmctorrent.magnet import generate_magnet from xbmctorrent.utils import first, SafeDialogProgress, get_quality_from_name, get_current_list_item from xbmctorrent.acestream import ace_supported with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Получение информации о раздаче...") torrent_url = urljoin("http://dl.rutracker.org/forum/", "dl.php?t=%s" % tid) try: plugin.log.debug("loading data from uri: " + torrent_url) params = {"t": tid} import os, xbmc, cookielib cookie = cookielib.Cookie(version=0, name='bb_dl', value=tid, port=None, port_specified=False, domain='.rutracker.org', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) data = url_get(torrent_url, headers=HEADERS, post=params, cookie=cookie) metadata = bdecode(data) plugin.log.debug("Metadata received " + str(metadata)) except: import xbmcgui plugin.log.error("Unexpected error: %s " % format_exc().split('\n')[-2]) xbmcgui.Dialog().ok(plugin.name, "Не удалось получить данные от сервера") return dialog.update(percent=100, line1="Готово") # Get currently selected item current_item = get_current_list_item() current_item.setdefault("stream_info", {}).update( get_quality_from_name(current_item['label'])) if "files" in metadata["info"] and ace_supported(): items = {} for index, info in enumerate(metadata["info"]["files"]): name = "/".join(info["path"]) if not _rutracker_valid_file(name): continue items[index] = deepcopy(current_item) items[index].update({ "label": name, "path": plugin.url_for("play_torrent_raw", raw=data, index=index, name=name), "is_playable": True }) items[index].setdefault("info", {}).update({ "title": name, }) # start playback if torrent contains only one file if len(items) == 1: index, item = items.popitem() if not plugin.get_setting("force_ace", bool): item["path"] = plugin.url_for("play", uri=generate_magnet( metadata, item["label"])) plugin.play_video(item) yield item else: plugin.add_sort_method('label') for i in items.values(): yield i else: name = metadata["info"].get("name") or " / ".join( first(metadata["info"]["files"])["path"]) or "rutor.org" if plugin.get_setting("force_ace", bool) and ace_supported(): current_item["path"] = plugin.url_for("play_torrent_raw", raw=data, index=0, name=name) else: current_item["path"] = plugin.url_for( ["play", "play_with_pulsar"][int(pulsar)], uri=generate_magnet(metadata, name)) current_item["is_playable"] = True plugin.play_video(current_item) yield current_item
def loop(self): from xbmctorrent.utils import SafeDialogProgress from xbmctorrent.player import OverlayText has_resolved = False plugin.log.info("Starting AceStream...") def safe_cast(val, to_type, default=None): try: return to_type(val) except ValueError: return default engine_params = { "filename": self.file_name, "host": plugin.get_setting("ace_host", str) or DEFAULT_ACESTREAM_HOST, "port": safe_cast(plugin.get_setting("ace_port"), int, DEFAULT_ACESTREAM_PORT), "keep_enc": plugin.get_setting("ace_keep_encripted", bool) } if plugin.get_setting("keep_files", bool): plugin.log.info("Will keep file after playback.") engine_params["dlpath"] = xbmc.validatePath(xbmc.translatePath(plugin.get_setting("dlpath") or "special://temp/" + plugin.id)) if engine_params["dlpath"] and "://" in engine_params["dlpath"]: # Translate smb:// url to UNC path on windows, very hackish if PLATFORM["os"] == "windows" and engine_params["dlpath"].lower().startswith("smb://"): engine_params["dlpath"] = engine_params["dlpath"].replace("smb:", "").replace("/", "\\") with closing(AceEngine(**engine_params)) as engine: plugin.log.info("Opening download dialog...") # Get currently selected item current_item = get_current_list_item() with closing(SafeDialogProgress(delay_create=0)) as dialog: dialog.create("AceStream Player") connected = engine.try_to_connect() n = 1 start = time.time() while (time.time() - start) < ACESTREAM_CONNECTION_TIMEOUT: if xbmc.abortRequested or dialog.iscanceled(): return dialog.update(0, self.file_name, "Попытка соединения с AceStream (%s)" % n) if not connected: plugin.log.info("Starting AceStream engine") if engine.start(): xbmc.sleep(ACESTREAM_CONNECTION_POLL) connected = engine.try_to_connect() continue else: if engine.is_ready(): break plugin.log.info("Engine is not ready") xbmc.sleep(ACESTREAM_CONNECTION_POLL) n = n + 1 if not engine.is_ready(): return dialog.update(0, self.file_name, "Соединение установлено. Запуск загрузки данных...") if not engine.play(self.torrent, self.index, is_raw=self.is_raw): return while not has_resolved: if xbmc.abortRequested or dialog.iscanceled(): return status = engine.get_status() if status["error"]: dialog.update(int(status["progress"]), *[self.file_name, STATE_STRS[status["status"]], status["error"]]) xbmc.sleep(PLAYING_EVENT_INTERVAL) return if status["state"] > 0 and status["status"]: dialog.update(int(status["progress"]), *self._get_status_lines(status)) if status["state"] >= 2 and status["status"] == "dl" and not has_resolved: # Downloading? if int(status["progress"]) >= 0: plugin.log.info("Resolving to %s" % status["url"]) has_resolved = True if not current_item["info"].get("title"): current_item["label"] = self.file_name current_item["path"] = status["url"] plugin.set_resolved_url(current_item) break xbmc.sleep(PLAYING_EVENT_INTERVAL) # We are now playing plugin.log.info("Now playing torrent...") with closing(OverlayText(w=OVERLAY_WIDTH, h=OVERLAY_HEIGHT, alignment=XBFONT_CENTER_X | XBFONT_CENTER_Y)) as overlay: with nested(self.attach(overlay.show, self.on_playback_paused), self.attach(overlay.hide, self.on_playback_resumed, self.on_playback_stopped), self.attach(engine.on_start, self.on_playback_started), self.attach(engine.on_pause, self.on_playback_paused), self.attach(engine.on_resume, self.on_playback_resumed), self.attach(engine.on_stop, self.on_playback_stopped), self.attach(engine.on_seek, self.on_playback_seek), self.attach(engine.on_end, self.on_playback_ended), self.attach(self.pause, engine.on_playback_paused), self.attach(self.play, engine.on_playback_resumed)): while not xbmc.abortRequested and self.isPlaying() and not engine.error: overlay.text = "\n".join(self._get_status_lines(engine.get_status())) xbmc.sleep(PLAYING_EVENT_INTERVAL) plugin.log.info("Closing AceStream player.")
def loop(self): from xbmctorrent.utils import SafeDialogProgress from xbmctorrent.player import OverlayText has_resolved = False plugin.log.info("Starting AceStream...") def safe_cast(val, to_type, default=None): try: return to_type(val) except ValueError: return default engine_params = { "filename": self.file_name, "host": plugin.get_setting("ace_host", str) or DEFAULT_ACESTREAM_HOST, "port": safe_cast(plugin.get_setting("ace_port"), int, DEFAULT_ACESTREAM_PORT), "keep_enc": plugin.get_setting("ace_keep_encripted", bool) } if plugin.get_setting("keep_files", bool): plugin.log.info("Will keep file after playback.") engine_params["dlpath"] = xbmc.validatePath( xbmc.translatePath( plugin.get_setting("dlpath") or "special://temp/" + plugin.id)) if engine_params["dlpath"] and "://" in engine_params["dlpath"]: # Translate smb:// url to UNC path on windows, very hackish if PLATFORM["os"] == "windows" and engine_params[ "dlpath"].lower().startswith("smb://"): engine_params["dlpath"] = engine_params["dlpath"].replace( "smb:", "").replace("/", "\\") with closing(AceEngine(**engine_params)) as engine: plugin.log.info("Opening download dialog...") # Get currently selected item current_item = get_current_list_item() with closing(SafeDialogProgress(delay_create=0)) as dialog: dialog.create("AceStream Player") connected = engine.try_to_connect() ready = False n = 1 start = time.time() while (time.time() - start) < ACESTREAM_CONNECTION_TIMEOUT: if xbmc.abortRequested or dialog.iscanceled(): return dialog.update(0, self.file_name, "Попытка соединения с AceStream (%s)" % n) if not connected: plugin.log.info("Starting AceStream engine") if engine.start(): xbmc.sleep(ACESTREAM_CONNECTION_POLL) connected = engine.try_to_connect() continue else: if engine.is_ready(): break plugin.log.info("Engine is not ready") xbmc.sleep(ACESTREAM_CONNECTION_POLL) n = n + 1 if not engine.is_ready(): return dialog.update( 0, self.file_name, "Соединение установлено. Запуск загрузки данных...") if not engine.play( self.torrent, self.index, is_raw=self.is_raw): return while not has_resolved: if xbmc.abortRequested or dialog.iscanceled(): return status = engine.get_status() if status["error"]: dialog.update( int(status["progress"]), *[ self.file_name, STATE_STRS[status["status"]], status["error"] ]) xbmc.sleep(PLAYING_EVENT_INTERVAL) return if status["state"] > 0 and status["status"]: dialog.update(int(status["progress"]), *self._get_status_lines(status)) if status["state"] >= 2 and status[ "status"] == "dl" and not has_resolved: # Downloading? if int(status["progress"]) >= 0: plugin.log.info("Resolving to %s" % status["url"]) has_resolved = True if not current_item["info"].get("title"): current_item["label"] = self.file_name current_item["path"] = status["url"] plugin.set_resolved_url(current_item) break xbmc.sleep(PLAYING_EVENT_INTERVAL) # We are now playing plugin.log.info("Now playing torrent...") with closing( OverlayText(w=OVERLAY_WIDTH, h=OVERLAY_HEIGHT, alignment=XBFONT_CENTER_X | XBFONT_CENTER_Y)) as overlay: with nested( self.attach(overlay.show, self.on_playback_paused), self.attach(overlay.hide, self.on_playback_resumed, self.on_playback_stopped), self.attach(engine.on_start, self.on_playback_started), self.attach(engine.on_pause, self.on_playback_paused), self.attach(engine.on_resume, self.on_playback_resumed), self.attach(engine.on_stop, self.on_playback_stopped), self.attach(engine.on_seek, self.on_playback_seek), self.attach(engine.on_end, self.on_playback_ended), self.attach(self.pause, engine.on_playback_paused), self.attach(self.play, engine.on_playback_resumed)): while not xbmc.abortRequested and self.isPlaying( ) and not engine.error: overlay.text = "\n".join( self._get_status_lines(engine.get_status())) xbmc.sleep(PLAYING_EVENT_INTERVAL) plugin.log.info("Closing AceStream player.")
def loop(self): from xbmctorrent.utils import SafeDialogProgress has_resolved = False plugin.log.info("Starting torrent2http...") with closing(torrent2http.start(**self.torrent2http_options)) as t2h_instance: t2h = lambda cmd: url_get_json("http://%s/%s" % (t2h_instance.bind_address, cmd), with_immunicity=False) # Get currently selected item current_item = get_current_list_item() if not self._wait_t2h_startup(t2h): return plugin.log.info("Opening download dialog...") with closing(SafeDialogProgress(delay_create=0)) as dialog: dialog.create(plugin.name) plugin.log.info("Waiting for file resolution...") while not has_resolved: if xbmc.abortRequested or dialog.iscanceled(): return status = t2h("status") self.display_name = status["name"] if status["state"] >= 0: dialog.update(int(status["progress"] * 100), *self._get_status_lines(status)) if status["state"] >= 3 and not has_resolved: # Downloading? files = t2h("ls")["files"] biggest_file = sorted(files, key=lambda x: x["size"])[-1] biggest_file["name"] = biggest_file["name"].encode("utf-8") percent_complete = float(biggest_file["complete_pieces"]) / float(biggest_file["total_pieces"]) * 100.0 if percent_complete >= MIN_COMPLETED_PIECES: plugin.log.info("Resolving to http://%s/files/%s" % (t2h_instance.bind_address, biggest_file["name"])) has_resolved = True url_name = "/".join(map(urllib.quote, biggest_file["name"].split("/"))) if not current_item["info"].get("title"): current_item["label"] = self.display_name current_item["path"] = "http://%s/files/%s" % (t2h_instance.bind_address, url_name) plugin.set_resolved_url(current_item) break xbmc.sleep(TORRENT2HTTP_POLL) # We are now playing plugin.log.info("Now playing torrent...") last_playing_event = 0 with closing(OverlayText(w=OVERLAY_WIDTH, h=OVERLAY_HEIGHT, alignment=XBFONT_CENTER_X | XBFONT_CENTER_Y)) as overlay: with nested(self.attach(overlay.show, self.on_playback_paused), self.attach(overlay.hide, self.on_playback_resumed, self.on_playback_stopped)): while not xbmc.abortRequested and self.isPlaying(): overlay.text = "\n".join(self._get_status_lines(t2h("status"))) now = time.time() if (now - last_playing_event) > PLAYING_EVENT_INTERVAL: track_event("video", "playing", self.display_name) last_playing_event = now xbmc.sleep(TORRENT2HTTP_POLL) plugin.log.info("Closing Torrent player.")
def rutor_play(tid, pulsar): from copy import deepcopy from contextlib import closing from bencode import bencode, bdecode from urlparse import urljoin from xbmctorrent.magnet import generate_magnet from xbmctorrent.utils import first, SafeDialogProgress, get_quality_from_name, get_current_list_item from xbmctorrent.acestream import ace_supported with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Получение информации о раздаче...") torrent_url = urljoin(DOWNLOAD_URL, "download/%s" % tid) try: metadata = bdecode(url_get(torrent_url, headers=HEADERS)) except: import xbmcgui plugin.log.error("Unexpected error: %s" % sys.exc_info()[0]) xbmcgui.Dialog().ok(plugin.name, "Не удалось получить данные от сервера") return dialog.update(percent=100, line1="Готово") # Get currently selected item current_item = get_current_list_item() current_item.setdefault("stream_info", {}).update( get_quality_from_name(current_item['label'])) if "files" in metadata["info"] and ace_supported(): items = {} for index, info in enumerate(metadata["info"]["files"]): name = "/".join(info["path"]) if not _rutor_valid_file(name): continue items[index] = deepcopy(current_item) items[index].update({ "label": name, "path": plugin.url_for("torrent_play", url=torrent_url, index=index, name=name), "is_playable": True }) items[index].setdefault("info", {}).update({ "title": name, }) # start playback if torrent contains only one file if len(items) == 1: index, item = items.popitem() if not plugin.get_setting("force_ace", bool): item["path"] = plugin.url_for("play", uri=generate_magnet( metadata, item["label"])) plugin.play_video(item) yield item else: plugin.add_sort_method('label') for i in items.values(): yield i else: name = metadata["info"].get("name") or " / ".join( first(metadata["info"]["files"])["path"]) or "rutor.org" if plugin.get_setting("force_ace", bool) and ace_supported(): current_item["path"] = plugin.url_for("torrent_play", url=torrent_url, index=0, name=name) else: current_item["path"] = plugin.url_for( ["play", "play_with_pulsar"][int(pulsar)], uri=generate_magnet(metadata, name)) current_item["is_playable"] = True plugin.play_video(current_item) yield current_item
def rutracker_play(tid, pulsar): from copy import deepcopy from contextlib import closing from bencode import bencode, bdecode from urlparse import urljoin from xbmctorrent.magnet import generate_magnet from xbmctorrent.utils import first, SafeDialogProgress, get_quality_from_name, get_current_list_item from xbmctorrent.acestream import ace_supported with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Получение информации о раздаче...") torrent_url = urljoin("http://dl.rutracker.org/forum/", "dl.php?t=%s" % tid) try: plugin.log.debug("loading data from uri: " + torrent_url) params = {"t": tid} import os, xbmc, cookielib cookie = cookielib.Cookie(version=0, name='bb_dl', value=tid, port=None, port_specified=False, domain='.rutracker.org', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) data = url_get(torrent_url, headers=HEADERS, post=params, cookie=cookie) metadata = bdecode(data) plugin.log.debug("Metadata received " + str(metadata)) except: import xbmcgui plugin.log.error("Unexpected error: %s " % format_exc().split('\n')[-2]) xbmcgui.Dialog().ok(plugin.name, "Не удалось получить данные от сервера") return dialog.update(percent=100, line1="Готово") # Get currently selected item current_item = get_current_list_item() current_item.setdefault("stream_info", {}).update(get_quality_from_name(current_item['label'])) if "files" in metadata["info"] and ace_supported(): items = {} for index, info in enumerate(metadata["info"]["files"]): name = "/".join(info["path"]) if not _rutracker_valid_file(name): continue items[index] = deepcopy(current_item) items[index].update({ "label": name, "path": plugin.url_for("play_torrent_raw", raw=data, index=index, name=name), "is_playable": True }) items[index].setdefault("info", {}).update({ "title": name, }) # start playback if torrent contains only one file if len(items) == 1: index, item = items.popitem() if not plugin.get_setting("force_ace", bool): item["path"] = plugin.url_for("play", uri=generate_magnet(metadata, item["label"])) plugin.play_video(item) yield item else: plugin.add_sort_method('label') for i in items.values(): yield i else: name = metadata["info"].get("name") or " / ".join(first(metadata["info"]["files"])["path"]) or "rutor.org" if plugin.get_setting("force_ace", bool) and ace_supported(): current_item["path"] = plugin.url_for("play_torrent_raw", raw=data, index=0, name=name) else: current_item["path"] = plugin.url_for(["play","play_with_pulsar"][int(pulsar)], uri=generate_magnet(metadata, name)) current_item["is_playable"] = True plugin.play_video(current_item) yield current_item