def search(query, **kwargs): from kmediatorrent.utils import url_get_json kwargs["query"] = query return url_get_json("%s/search/movie" % BASE_URL, params=kwargs, headers=HEADERS, with_immunicity=False)
def get(imdb_id): from kmediatorrent.caching import shelf with shelf("com.imdb.%s" % imdb_id) as movie: if not movie: try: import urllib2 from kmediatorrent.utils import url_get_json movie.update(url_get_json("%s/movie/%s" % (BASE_URL, imdb_id), params={"api_key": API_KEY, "append_to_response": "credits"}, headers=HEADERS, with_immunicity=False) or {}) except urllib2.HTTPError: pass return dict(movie)
def get(imdb_id): from kmediatorrent.caching import shelf with shelf("com.imdb.%s" % imdb_id) as movie: if not movie: try: import urllib2 from kmediatorrent.utils import url_get_json movie.update( url_get_json("%s/movie/%s" % (BASE_URL, imdb_id), params={ "api_key": API_KEY, "append_to_response": "credits" }, headers=HEADERS, with_immunicity=False) or {}) except urllib2.HTTPError: pass return dict(movie)
def yify_show_data(callback): import xbmc import xbmcgui from contextlib import nested, closing from itertools import izip, chain from concurrent import futures from kmediatorrent import tmdb from kmediatorrent.utils import url_get_json, terminating, SafeDialogProgress plugin.set_content("movies") args = dict((k, v[0]) for k, v in plugin.request.args.items()) current_page = int(args["set"]) limit = int(args["limit"]) with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Fetching movie information...", line2="", line3="") try: search_result = url_get_json("%s/api/list.json" % BASE_URL, params=args, headers=HEADERS) except: plugin.notify("Unable to connect to %s." % BASE_URL) raise movies = search_result.get("MovieList") or [] if not movies: return state = {"done": 0} def on_movie(future): data = future.result() state["done"] += 1 dialog.update( percent=int(state["done"] * 100.0 / len(movies)), line2=data.get("title") or data.get("MovieTitleClean") or "", ) with futures.ThreadPoolExecutor(max_workers=2) as pool_tmdb: tmdb_list = [pool_tmdb.submit(tmdb.get, movie["ImdbCode"]) for movie in movies] [future.add_done_callback(on_movie) for future in tmdb_list] while not all(job.done() for job in tmdb_list): if dialog.iscanceled(): return xbmc.sleep(100) tmdb_list = map(lambda job: job.result(), tmdb_list) for movie, tmdb_meta in izip(movies, tmdb_list): if tmdb_meta: item = tmdb.get_list_item(tmdb_meta) if args.get("quality") == "all" and movie["Quality"] != "720p": item["label"] = "%s (%s)" % (item["label"], movie["Quality"]) item.update({ "path": plugin.url_for("play", uri=movie["TorrentMagnetUrl"]), "is_playable": True, }) item.setdefault("info", {}).update({ "count": movie["MovieID"], "genre": "%s (%s S:%s P:%s)" % (item["info"]["genre"], movie["Size"], movie["TorrentSeeds"], movie["TorrentPeers"]), "plot_outline": tmdb_meta["overview"], "video_codec": "h264", }) width = 1920 height = 1080 if movie["Quality"] == "720p": width = 1280 height = 720 item.setdefault("stream_info", {}).update({ "video": { "codec": "h264", "width": width, "height": height, }, "audio": { "codec": "aac", }, }) yield item if current_page < (int(search_result["MovieCount"]) / limit): next_args = args.copy() next_args["set"] = int(next_args["set"]) + 1 yield { "label": ">> Next page", "path": plugin.url_for(callback, **next_args), }
def tmdb_config(): from kmediatorrent.utils import url_get_json return url_get_json("%s/configuration" % BASE_URL, params={"api_key": API_KEY}, headers=HEADERS, with_immunicity=False)
def loop(self): from kmediatorrent.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) 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"] global buffer_percentage global extension_message global max_buffer_size extension = self.getFileExtension(self.display_name) BUFFERSIZE = self.getBufferSize(extension_message) if status[ "state"] >= 0 and buffer_percentage < 100 and BUFFERSIZE != max_buffer_size and extension_message == "": dialog.update(int(buffer_percentage), *self._get_status_lines(status, "")) #buffering elif status[ "state"] >= 0 and buffer_percentage >= 100 or BUFFERSIZE == max_buffer_size or extension_message != "": dialog.update(int(status["progress"] * 100), *self._get_status_lines( status, extension_message)) #done buffering if status[ "state"] >= 3 and not has_resolved: # Downloading? files = t2h("ls")["files"] progress = float(t2h("status")["progress"]) biggest_file = sorted(files, key=lambda x: x["size"])[-1] biggest_file["name"] = biggest_file["name"].encode( "utf-8") # Estimate the video size using the biggest file size # and the progress (progress is: <pcnt downloaded> / 100.0) bytes_complete = float(biggest_file["size"]) * progress mb_complete = bytes_complete / 1024.0 / 1024.0 buffer_percentage = (mb_complete / float( plugin.get_setting("min_download_size"))) * 100 if buffer_percentage >= 100 and BUFFERSIZE != max_buffer_size: extension_message = self.getExtensionMessage( extension) if extension_message == "File can not fast start. The whole file must download.": BUFFERSIZE = max_buffer_size if mb_complete > BUFFERSIZE or bytes_complete >= biggest_file[ "size"]: extension_message = "" dialog.update( int(status["progress"] * 100), *self._get_status_lines( status, extension_message)) plugin.log.info("Resolving to http://%s/files/%s" % (t2h_instance.bind_address, biggest_file["name"])) has_resolved = True item = { "path": "http://%s/files/%s" % (t2h_instance.bind_address, urllib.quote(biggest_file["name"])), } if not xbmc.getInfoLabel("ListItem.Title"): item["label"] = self.display_name plugin.set_resolved_url(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"), extension_message)) 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 loop(self): from kmediatorrent.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) 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"] global buffer_percentage global extension_message global max_buffer_size extension = self.getFileExtension(self.display_name) BUFFERSIZE = self.getBufferSize(extension_message) if status["state"] >= 0 and buffer_percentage < 100 and BUFFERSIZE != max_buffer_size and extension_message == "": dialog.update(int(buffer_percentage), *self._get_status_lines(status, "")) #buffering elif status["state"] >= 0 and buffer_percentage >= 100 or BUFFERSIZE == max_buffer_size or extension_message != "": dialog.update(int(status["progress"] * 100), *self._get_status_lines(status, extension_message))#done buffering if status["state"] >= 3 and not has_resolved: # Downloading? files = t2h("ls")["files"] progress = float(t2h("status")["progress"]) biggest_file = sorted(files, key=lambda x: x["size"])[-1] biggest_file["name"] = biggest_file["name"].encode("utf-8") # Estimate the video size using the biggest file size # and the progress (progress is: <pcnt downloaded> / 100.0) bytes_complete = float(biggest_file["size"]) * progress mb_complete = bytes_complete / 1024.0 / 1024.0 buffer_percentage = (mb_complete / float(plugin.get_setting("min_download_size"))) * 100 if buffer_percentage >= 100 and BUFFERSIZE != max_buffer_size: extension_message = self.getExtensionMessage(extension) if extension_message == "File can not fast start. The whole file must download.": BUFFERSIZE = max_buffer_size if mb_complete > BUFFERSIZE or bytes_complete >= biggest_file["size"]: extension_message = "" dialog.update(int(status["progress"] * 100), *self._get_status_lines(status, extension_message)) plugin.log.info("Resolving to http://%s/files/%s" % (t2h_instance.bind_address, biggest_file["name"])) has_resolved = True item = { "path": "http://%s/files/%s" % (t2h_instance.bind_address, urllib.quote(biggest_file["name"])), } if not xbmc.getInfoLabel("ListItem.Title"): item["label"] = self.display_name plugin.set_resolved_url(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"), extension_message)) 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 yify_show_data(callback): import xbmc import xbmcgui from contextlib import nested, closing from itertools import izip, chain from concurrent import futures from kmediatorrent import tmdb from kmediatorrent.utils import url_get_json, terminating, SafeDialogProgress plugin.set_content("movies") args = dict((k, v[0]) for k, v in plugin.request.args.items()) current_page = int(args["page"]) limit = int(args["limit"]) with closing(SafeDialogProgress(delay_close=0)) as dialog: dialog.create(plugin.name) dialog.update(percent=0, line1="Fetching movie information...", line2="", line3="") try: search_result = url_get_json("%s/api/v2/list_movies.json" % BASE_URL, params=args, headers=HEADERS) except: plugin.notify("Unable to connect to %s." % BASE_URL) raise movies = search_result.get("data").get("movies") or [] if not movies: return state = {"done": 0} def on_movie(future): data = future.result() state["done"] += 1 dialog.update( percent=int(state["done"] * 100.0 / len(movies)), line2=data.get("title") or data.get("MovieTitleClean") or "", ) with futures.ThreadPoolExecutor(max_workers=2) as pool_tmdb: tmdb_list = [pool_tmdb.submit(tmdb.get, movie["imdb_code"]) for movie in movies] [future.add_done_callback(on_movie) for future in tmdb_list] while not all(job.done() for job in tmdb_list): if dialog.iscanceled(): return xbmc.sleep(100) tmdb_list = map(lambda job: job.result(), tmdb_list) for movie, tmdb_meta in izip(movies, tmdb_list): for tor in movie.get("torrents"): if tor["quality"] == args.get("quality") or args.get("quality") == "all": if tmdb_meta: item = tmdb.get_list_item(tmdb_meta) if tor["quality"] != "720p": item["label"] = "%s (%s)" % (item["label"], tor["quality"]) item.update({ "path": plugin.url_for("play", uri=tor["url"]), "is_playable": True, }) item.setdefault("info", {}).update({ "count": movie["id"], "genre": "%s (%s S:%s P:%s)" % (item["info"]["genre"], tor["size"], tor["seeds"], tor["peers"]), "plot_outline": tmdb_meta["overview"], "video_codec": "h264", }) width = 1920 height = 1080 if tor["quality"] == "720p": width = 1280 height = 720 item.setdefault("stream_info", {}).update({ "video": { "codec": "h264", "width": width, "height": height, }, "audio": { "codec": "aac", }, }) yield item if current_page < (int(search_result["data"]["movie_count"]) / limit): next_args = args.copy() next_args["page"] = int(next_args["page"]) + 1 yield { "label": ">> Next page", "path": plugin.url_for(callback, **next_args), }