plugin.video.torrenter3/resources/lib/player.py

77 lines
3.3 KiB
Python

import xbmc, xbmcgui
from .utils import get_engine, localize
class TorrentPlayer(xbmc.Player) :
pyrrent_engine = None
loop = None
paused = False
def onPlayBackEnded(self):
self.pyrrent_engine.close()
self.loop.stopped = True
xbmc.Player().stop()
def onPlayBackPaused(self):
self.pyrrent_engine.pause()
self.paused = True
def onPlayBackResumed(self):
self.paused = False
self.pyrrent_engine.resume()
def onPlayBackStopped(self):
self.pyrrent_engine.close()
self.loop.stopped = True
xbmc.Player().stop()
def play(self, engine, f_index):
self.pyrrent_engine = engine
self.pyrrent_engine.start()
self.pyrrent_engine.activate_file(f_index)
monitor = xbmc.Monitor()
pw = xbmcgui.DialogProgress()
pw.create(localize(33055), line1='0 Kbit/s')
f_size = self.pyrrent_engine.file_status(f_index).size
EXPECTED_KBYTES = f_size / 3 / 1024 / 1024
while True:
xbmc.sleep(500)
if monitor.abortRequested() or pw.iscanceled():
pw.close()
self.pyrrent_engine.close()
xbmc.Player().stop()
self.loop.stopped = True
return False
status = self.pyrrent_engine.status()
d_rate = status.download_rate
# xbmc.log('*** DRATE: {}'.format(d_rate), level=xbmc.LOGNOTICE)
perc = d_rate / EXPECTED_KBYTES * 100
if perc > 100: perc = 100
pw.update(perc, line1=' {} / {} KB/s'.format(int(d_rate), int(EXPECTED_KBYTES)))
if perc == 100:
pw.close()
break
fstat = self.pyrrent_engine.file_status(f_index)
listitem = xbmcgui.ListItem('.'.join(fstat.name.split('/')[-1].split('.')[:-1])[:-1], path=fstat.url)
xbmc.Player.play(self, fstat.url, listitem)
class VideoLoop(object):
stopped = False
def __init__(self, torr_fp):
self.e = get_engine(torr_fp)
def start(self, f_index):
self.statinfo = xbmcgui.Dialog()
self.mediaPlayer = TorrentPlayer()
self.mediaPlayer.loop = self
self.mediaPlayer.play(self.e, f_index)
while not self.stopped:
# sometime busydialog is showing in infinite loop. kick it.
if xbmc.getCondVisibility('Window.IsVisible(busydialog)'):
xbmc.executebuiltin("Dialog.Close(busydialog)")
status = self.e.status()
f_status = self.e.file_status(f_index)
if self.mediaPlayer.paused:
self.statinfo.notification('[{}]{}.'.format(('|' * (int(f_status.progress * 100) / 2)).ljust(50, '.'), ' ' * 100),
'S: {} DL: {} KB/s UL: {} KB/s'.format(status.num_seeds,
status.download_rate,
status.upload_rate),
time=2, sound=False
)
xbmc.sleep(1000)