python-libtorrent resume_data

pull/1/head
DiMartinoXBMC 2016-03-06 11:04:51 +03:00
parent 63bf62b223
commit 3de1a28a97
4 changed files with 65 additions and 15 deletions

View File

@ -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))),]

View File

@ -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

View File

@ -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):

View File

@ -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,)