import xbmc import xbmcgui import requests from xbmctorrent import plugin, torrent2http from xbmctorrent.ga import track_event class TorrentPlayer(xbmc.Player): # def __init__(self, *args, **kwargs): # super(TorrentPlayer).__init__(self, *args, **kwargs) def init(self, magnet_uri): track_event("torrent_player", "start") self.magnet_uri = magnet_uri self.torrent2http = torrent2http.start(magnet=magnet_uri) track_event("download", "start", magnet_uri) xbmc.sleep(1000) self.dialog = xbmcgui.DialogProgress() self.dialog_hidden = True self.status = {} self.is_playing = False self.is_paused = False self.is_stopped = False return self def get_title(self): return xbmc.getInfoLabel("Player.Title") def show_dialog(self): if self.dialog_hidden: self.dialog_hidden = False self.dialog.create(plugin.name) def hide_dialog(self): if not self.dialog_hidden: self.dialog_hidden = True self.dialog.close() def onPlayBackStarted(self): self.is_playing = True self.is_paused = False self.is_stopped = False self.hide_dialog() plugin.log.info(self.get_title()) track_event("video", "play", self.get_title()) def onPlayBackResumed(self): self.onPlayBackStarted() def onPlayBackPaused(self): self.is_playing = False self.is_paused = True self.is_stopped = False track_event("video", "pause", self.get_title()) def onPlayBackStopped(self): self.is_playing = False self.is_paused = False self.is_stopped = True track_event("video", "stop", self.get_title()) def loop(self): state_str = ['Queued', 'Checking', 'Downloading metadata', \ 'Downloading', 'Finished', 'Seeding', 'Allocating', "Allocating file & Checking resume"] has_resolved = False self.show_dialog() last_status = None while not (xbmc.abortRequested or self.dialog.iscanceled() or self.is_stopped): status = requests.get("http://%s/status" % self.torrent2http.get_bind_address()).json() if status["state"] != last_status: track_event("torrent_player", state_str[status["state"]], self.magnet_uri) last_status = status["state"] # if self.is_paused: # self.show_dialog() if not self.dialog_hidden: if status["state"] >= 0: self.dialog.update( int(status["progress"] * 100), state_str[status["state"]], "%.2f%% D:%.2fkb/s U:%.2fkb/s" % (status["progress"] * 100, status["download_rate"], status["upload_rate"]), "S:%d P:%d" % (status["num_seeds"], status["num_peers"]) ) if status["total_pieces"] > 0 and not has_resolved: percentage = float(status["max_piece"]) / float(status["total_pieces"]) * 100.0 if percentage >= 0.5: # we can start playback at 0.5% has_resolved = True plugin.set_resolved_url({ "path": "http://%s/file" % self.torrent2http.get_bind_address(), "is_playable": True, }) xbmc.sleep(500) self.hide_dialog() self.torrent2http.shutdown() track_event("torrent_player", "shutdown")