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

87 lines
3.1 KiB
Python

import xbmc, xbmcgui
from .utils import get_engine, localize
class TorrentPlayer(xbmc.Player):
gorrent_engine = None
loop = None
paused = False
def onPlayBackEnded(self):
self.gorrent_engine.close()
self.loop.stopped = True
xbmc.Player().stop()
def onPlayBackPaused(self):
self.gorrent_engine.pause()
self.paused = True
def onPlayBackResumed(self):
self.paused = False
self.gorrent_engine.resume()
def onPlayBackStopped(self):
self.gorrent_engine.close()
self.loop.stopped = True
xbmc.Player().stop()
def play(self, engine, f_index):
self.gorrent_engine = engine
self.gorrent_engine.start(f_index)
monitor = xbmc.Monitor()
pw = xbmcgui.DialogProgress()
pw.create(localize(33055), '0 Kbit/s')
f_size = self.gorrent_engine.file_status(f_index).size
EXPECTED_KBYTES = f_size // 3 // 1024 // 1024
if EXPECTED_KBYTES > 768:
EXPECTED_KBYTES = 768
while True:
xbmc.sleep(500)
if monitor.abortRequested() or pw.iscanceled():
pw.close()
self.gorrent_engine.close()
xbmc.Player().stop()
self.loop.stopped = True
return False
status = self.gorrent_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, ' {} / {} KB/s'.format(int(d_rate), int(EXPECTED_KBYTES)))
if perc == 100:
pw.close()
break
fstat = self.gorrent_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),
'[{}]'.format(('|' * (f_status.progress // 2)).ljust(50, '.')),
'S: {} DL: {} KB/s UL: {} KB/s'.format(status.num_seeds,
status.download_rate,
status.upload_rate),
time=2, sound=False
)
xbmc.sleep(1000)