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

87 lines
3.1 KiB
Python
Raw Normal View History

2019-05-04 22:38:06 +03:00
import xbmc, xbmcgui
from .utils import get_engine, localize
2022-03-14 09:16:22 +03:00
class TorrentPlayer(xbmc.Player):
2022-03-20 00:15:29 +03:00
gorrent_engine = None
2022-03-14 09:16:22 +03:00
loop = None
paused = False
def onPlayBackEnded(self):
2022-03-20 00:15:29 +03:00
self.gorrent_engine.close()
2022-03-14 09:16:22 +03:00
self.loop.stopped = True
xbmc.Player().stop()
def onPlayBackPaused(self):
2022-03-20 00:15:29 +03:00
self.gorrent_engine.pause()
2022-03-14 09:16:22 +03:00
self.paused = True
def onPlayBackResumed(self):
self.paused = False
2022-03-20 00:15:29 +03:00
self.gorrent_engine.resume()
2022-03-14 09:16:22 +03:00
def onPlayBackStopped(self):
2022-03-20 00:15:29 +03:00
self.gorrent_engine.close()
2022-03-14 09:16:22 +03:00
self.loop.stopped = True
xbmc.Player().stop()
def play(self, engine, f_index):
2022-03-20 00:15:29 +03:00
self.gorrent_engine = engine
self.gorrent_engine.start(f_index)
2022-03-14 09:16:22 +03:00
monitor = xbmc.Monitor()
pw = xbmcgui.DialogProgress()
pw.create(localize(33055), '0 Kbit/s')
2022-03-20 00:15:29 +03:00
f_size = self.gorrent_engine.file_status(f_index).size
2022-03-14 09:16:22 +03:00
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()
2022-03-20 00:15:29 +03:00
self.gorrent_engine.close()
2022-03-14 09:16:22 +03:00
xbmc.Player().stop()
self.loop.stopped = True
return False
2022-03-20 00:15:29 +03:00
status = self.gorrent_engine.status()
2022-03-14 09:16:22 +03:00
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
2022-03-20 00:15:29 +03:00
fstat = self.gorrent_engine.file_status(f_index)
2022-03-14 09:16:22 +03:00
listitem = xbmcgui.ListItem('.'.join(fstat.name.split('/')[-1].split('.')[:-1])[:-1], path=fstat.url)
xbmc.Player.play(self, fstat.url, listitem)
2019-05-04 22:38:06 +03:00
class VideoLoop(object):
stopped = False
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def __init__(self, torr_fp):
self.e = get_engine(torr_fp)
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def start(self, f_index):
self.statinfo = xbmcgui.Dialog()
2019-05-04 22:38:06 +03:00
self.mediaPlayer = TorrentPlayer()
self.mediaPlayer.loop = self
self.mediaPlayer.play(self.e, f_index)
while not self.stopped:
2019-05-05 10:16:32 +03:00
# sometime busydialog is showing in infinite loop. kick it.
if xbmc.getCondVisibility('Window.IsVisible(busydialog)'):
xbmc.executebuiltin("Dialog.Close(busydialog)")
2019-05-05 10:16:32 +03:00
status = self.e.status()
f_status = self.e.file_status(f_index)
if self.mediaPlayer.paused:
2022-03-14 09:16:22 +03:00
self.statinfo.notification(
# '[{}]{}.'.format(('|' * (int(f_status.progress * 100) // 2)).ljust(50, '.'), ' ' * 100),
2022-03-20 00:15:29 +03:00
'[{}]'.format(('|' * (f_status.progress // 2)).ljust(50, '.')),
2022-03-14 09:16:22 +03:00
'S: {} DL: {} KB/s UL: {} KB/s'.format(status.num_seeds,
status.download_rate,
status.upload_rate),
time=2, sound=False
)
2019-05-04 22:38:06 +03:00
xbmc.sleep(1000)