2019-05-04 22:38:06 +03:00
|
|
|
import xbmc, xbmcgui
|
|
|
|
from .utils import get_engine, localize
|
|
|
|
|
|
|
|
class TorrentPlayer(xbmc.Player) :
|
|
|
|
pyrrent_engine = None
|
|
|
|
loop = None
|
2019-05-05 11:46:05 +03:00
|
|
|
paused = False
|
2019-05-04 22:38:06 +03:00
|
|
|
def onPlayBackEnded(self):
|
|
|
|
self.pyrrent_engine.close()
|
|
|
|
self.loop.stopped = True
|
|
|
|
xbmc.Player().stop()
|
|
|
|
def onPlayBackPaused(self):
|
|
|
|
self.pyrrent_engine.pause()
|
2019-05-05 11:46:05 +03:00
|
|
|
self.paused = True
|
2019-05-04 22:38:06 +03:00
|
|
|
def onPlayBackResumed(self):
|
2019-05-05 11:46:05 +03:00
|
|
|
self.paused = False
|
|
|
|
self.pyrrent_engine.resume()
|
2019-05-04 22:38:06 +03:00
|
|
|
|
|
|
|
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')
|
2019-05-05 10:16:32 +03:00
|
|
|
f_size = self.pyrrent_engine.file_status(f_index).size
|
|
|
|
EXPECTED_KBYTES = f_size / 3 / 1024 / 1024
|
2019-05-05 13:35:16 +03:00
|
|
|
if EXPECTED_KBYTES > 768:
|
|
|
|
EXPECTED_KBYTES = 768
|
2019-05-04 22:38:06 +03:00
|
|
|
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
|
2019-05-05 10:16:32 +03:00
|
|
|
# xbmc.log('*** DRATE: {}'.format(d_rate), level=xbmc.LOGNOTICE)
|
|
|
|
perc = d_rate / EXPECTED_KBYTES * 100
|
2019-05-04 22:38:06 +03:00
|
|
|
if perc > 100: perc = 100
|
2019-05-05 10:16:32 +03:00
|
|
|
pw.update(perc, line1=' {} / {} KB/s'.format(int(d_rate), int(EXPECTED_KBYTES)))
|
2019-05-04 22:38:06 +03:00
|
|
|
if perc == 100:
|
|
|
|
pw.close()
|
|
|
|
break
|
|
|
|
fstat = self.pyrrent_engine.file_status(f_index)
|
2019-05-05 11:46:05 +03:00
|
|
|
listitem = xbmcgui.ListItem('.'.join(fstat.name.split('/')[-1].split('.')[:-1])[:-1], path=fstat.url)
|
2019-05-04 22:38:06 +03:00
|
|
|
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):
|
2019-05-05 11:46:05 +03:00
|
|
|
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)'):
|
2019-05-05 11:46:05 +03:00
|
|
|
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)
|
2019-05-05 11:46:05 +03:00
|
|
|
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
|
|
|
|
)
|
2019-05-04 22:38:06 +03:00
|
|
|
xbmc.sleep(1000)
|
2019-05-05 11:46:05 +03:00
|
|
|
|