From 3de1a28a97c29cf87701a7ed0c335e3dbf5e78ec Mon Sep 17 00:00:00 2001 From: DiMartinoXBMC Date: Sun, 6 Mar 2016 11:04:51 +0300 Subject: [PATCH] python-libtorrent resume_data --- Core.py | 2 +- Libtorrent.py | 67 ++++++++++++++++++++++++++++++++++++++++++--------- Player.py | 9 ++++++- functions.py | 2 +- 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Core.py b/Core.py index ae3b8b6..52e08fe 100644 --- a/Core.py +++ b/Core.py @@ -548,7 +548,7 @@ class Core: clDimgray = '[COLOR FF696969]%s[/COLOR]' title = title + chr(10) + clDimgray % '(%s)' % foldername.encode('utf-8') - if self.torrent_player == '2' and seek > 0: + if self.torrent_player != '1' and seek > 0: seek_text = '%02d:%02d:%02d' % ((seek / (60*60)), (seek / 60) % 60, seek % 60) contextMenu = [(self.localize('Play (from %s)') % seek_text, ListString % ('WatchedHistory', 'playwithseek', 'addtime', str(addtime))), (self.localize('Play (from start)'), ListString % ('WatchedHistory', 'playnoseek', 'addtime', str(addtime))),] diff --git a/Libtorrent.py b/Libtorrent.py index cef814d..1ee5c3d 100644 --- a/Libtorrent.py +++ b/Libtorrent.py @@ -433,8 +433,19 @@ class Libtorrent: if self.save_resume_data: log('loading resume data') torrent_info['resume_data']=self.save_resume_data + else: + resume_file=self.resume_data_path() + if xbmcvfs.exists(resume_file): + log('loading resume data from file'+resume_file) + try: + resumDataFile=xbmcvfs.File(resume_file,'rb') + self.save_resume_data=resumDataFile.read() + resumDataFile.close() + torrent_info['resume_data']=self.save_resume_data + + except: + log(' Failed to load resume data from file'+ resume_file) self.torrentHandle = self.session.add_torrent(torrent_info) - self.torrentHandle.set_sequential_download(True) self.torrentHandle.set_max_connections(60) self.torrentHandle.set_max_uploads(-1) @@ -443,6 +454,7 @@ class Libtorrent: def stopSession(self): for i in range(self.torrentFileInfo.num_pieces()): self.torrentHandle.piece_priority(i, 0) + self.resume_data() def continueSession(self, contentId=0, Offset=0, seeding=False, isMP4=False): self.piece_length = self.torrentFileInfo.piece_length() @@ -479,17 +491,44 @@ class Libtorrent: self.session.stop_dht() def resume_data(self): - self.torrentHandle.save_resume_data() - received=False - while not received: - self.session.wait_for_alert(1000) - a = self.session.pop_alert() - log('[save_resume_data]: ['+str(type(a))+'] the alert '+str(a)+' is received') - if type(a) == self.lt.save_resume_data_alert: - received = True - debug('[save_resume_data]: '+str(dir(a))) - self.save_resume_data=self.lt.bencode(a.resume_data) - log('[save_resume_data]: the torrent resume data are saved') + self.session.pause() + self.save_resume_data=None + + try: + if not self.torrentHandle.is_valid(): + return + status = self.torrentHandle.status() + if not status.has_metadata: + return + if not status.need_save_resume: + return + + log('[save_resume_data]: waiting for alert...') + self.torrentHandle.save_resume_data() + received=False + while not received: + self.session.wait_for_alert(1000) + a = self.session.pop_alert() + log('[save_resume_data]: ['+str(type(a))+'] the alert '+str(a)+' is received') + if type(a) == self.lt.save_resume_data_alert: + received = True + debug('[save_resume_data]: '+str(dir(a))) + self.save_resume_data=self.lt.bencode(a.resume_data) + log('[save_resume_data]: the torrent resume data are received') + try: + resumeFileHandler = xbmcvfs.File(self.resume_data_path(), "w+b") + resumeFileHandler.write(self.save_resume_data) + resumeFileHandler.close() + log('[save_resume_data]: the torrent resume data to file' + self.resume_data_path()) + except: + log('[save_resume_data]: failed to save the torrent resume data to file') + elif type(a) == self.lt.save_resume_data_failed_alert: + received = True + log('[save_resume_data]: save_resume_data() failed') + log('[save_resume_data]: done.') + + finally: + self.session.resume() def debug(self): #try: @@ -564,3 +603,7 @@ class Libtorrent: log("'%s':'%s'," % (attr, getattr(obj, attr))) except: pass + + def resume_data_path(self): + path=self.torrentFile + ".resume_data" + return path diff --git a/Player.py b/Player.py index 1c85aed..bc6ed87 100644 --- a/Player.py +++ b/Player.py @@ -148,6 +148,9 @@ class TorrentPlayer(xbmc.Player): self.params = params self.get = self.params.get self.contentId = int(self.get("url")) + if self.get("seek"): + self.seek = int(self.get("seek")) + log('[TorrentPlayer] Seek='+str(self.seek)) self.torrent = Downloader.Torrent(self.userStorageDirectory, self.torrentUrl, self.torrentFilesDirectory).player try: if self.get("url2"): @@ -347,13 +350,17 @@ class TorrentPlayer(xbmc.Player): xbmc.sleep(2000) # very important, do not edit this, podavan i = 0 - while not xbmc.abortRequested or not self.isPlaying() or i < 50: + while not xbmc.abortRequested and not self.isPlaying() and i < 50: xbmc.sleep(200) i += 1 log('[TorrentPlayer]: self.isPlaying() = %s, i = %d, xbmc.abortRequested - %s' % (str(self.isPlaying()), i, str(xbmc.abortRequested))) if not self.isPlaying() or xbmc.abortRequested: return False + if self.seek > 0: + log('[TorrentPlayer]: seekTime - '+str(self.seek)) + self.seekTime(self.seek) + return True def setup_subs(self, label, path): diff --git a/functions.py b/functions.py index 6f0be69..4228fbe 100644 --- a/functions.py +++ b/functions.py @@ -1766,7 +1766,7 @@ def first_run_242(): # Localization.localize('Torrent2HTTP enabled! Can be changed in Settings.')) def seeking_warning(seek): - if __settings__.getSetting('torrent_player')=='2': + if __settings__.getSetting('torrent_player')!='1': seek_point = '%02d:%02d:%02d' % ((seek / (60*60)), (seek / 60) % 60, seek % 60) yes=xbmcgui.Dialog().yesno('< %s >' % (Localization.localize('Seeking')), Localization.localize('Would you like to resume from %s?') % seek_point,)