download by libtorrent

pull/1/head
DiMartinoXBMC 2015-01-13 17:24:53 +03:00
parent 47dbc66be4
commit fcb7947ec5
6 changed files with 238 additions and 663 deletions

File diff suppressed because it is too large Load Diff

35
Core.py
View File

@ -309,7 +309,17 @@ class Core:
if action2 == 'delete': if action2 == 'delete':
db.delete(addtime) db.delete(addtime)
xbmc.executebuiltin('Container.Refresh') xbmc.executebuiltin('Container.Refresh')
showMessage(self.localize('Download Status'), self.localize('Deleted! It will not stop download')) showMessage(self.localize('Download Status'), self.localize('Deleted and Stopped!'))
if action2 == 'pause':
db.update_status(addtime, 'pause')
xbmc.executebuiltin('Container.Refresh')
showMessage(self.localize('Download Status'), self.localize('Paused!'))
if action2 == 'unpause':
db.update_status(addtime, 'downloading')
xbmc.executebuiltin('Container.Refresh')
showMessage(self.localize('Download Status'), self.localize('Unpaused!'))
if action2 == 'clear': if action2 == 'clear':
db.clear() db.clear()
@ -319,23 +329,30 @@ class Core:
items = db.get_all() items = db.get_all()
if items: if items:
ListString = 'XBMC.RunPlugin(%s)' % (sys.argv[0] + '?action=DownloadStatus&action2=%s&%s=%s') ListString = 'XBMC.RunPlugin(%s)' % (sys.argv[0] + '?action=DownloadStatus&action2=%s&%s=%s')
for addtime, title, path, type, info in items: for addtime, title, path, type, info, status in items:
jsoninfo=json.loads(urllib.unquote_plus(info)) jsoninfo=json.loads(urllib.unquote_plus(info))
progress=int(jsoninfo.get('progress')) progress=int(jsoninfo.get('progress'))
title = '[%d%%] %s' % (progress, title) if status=='pause': status_sign='[||]'
if progress<100: else:status_sign='[>]'
if jsoninfo.get('seeds')!=None and jsoninfo.get('peers')!=None and \ title = '[%d%%]%s %s' % (progress, status_sign, title)
if jsoninfo.get('seeds')!=None and jsoninfo.get('peers')!=None and \
jsoninfo.get('download')!=None and jsoninfo.get('upload')!=None: jsoninfo.get('download')!=None and jsoninfo.get('upload')!=None:
d,u=int(jsoninfo['download']/ 1000000), int(jsoninfo['upload'] / 1000000) d,u=int(jsoninfo['download']/ 1000000), int(jsoninfo['upload'] / 1000000)
s,p=str(jsoninfo['seeds']),str(jsoninfo['peers']) s,p=str(jsoninfo['seeds']),str(jsoninfo['peers'])
title='%s [S/L %s/%s][D/U %s/%s (MB/s)]' %(title,s,p,d,u) title='%s [S/L %s/%s][D/U %s/%s (MB/s)]' %(title,s,p,d,u)
link={'action2':'notfinished'} if status!='pause':
contextMenu=[((self.localize('Delete from %s') % self.localize('Download Status'), ListString % ('delete', 'addtime', str(addtime))))] contextMenu=[((self.localize('Delete and Stop'), ListString % ('delete', 'addtime', str(addtime)))),
self.drawItem(title, 'DownloadStatus', link, image='', contextMenu=contextMenu, replaceMenu=True) ((self.localize('Pause'), ListString % ('pause', 'addtime', str(addtime)))),]
else: else:
contextMenu=[((self.localize('Delete from %s') % self.localize('Download Status'), ListString % ('delete', 'addtime', str(addtime))))] contextMenu=[((self.localize('Delete and Stop'), ListString % ('delete', 'addtime', str(addtime)))),
((self.localize('Unpause'), ListString % ('unpause', 'addtime', str(addtime)))),]
if progress==100 or progress>30 and type=='file':
link={'action2':'play', 'type':type, 'path':path.encode('utf-8')} link={'action2':'play', 'type':type, 'path':path.encode('utf-8')}
self.drawItem('[B]%s[/B]' % title, 'DownloadStatus', link, image='', contextMenu=contextMenu, replaceMenu=False, isFolder=type=='folder') self.drawItem('[B]%s[/B]' % title, 'DownloadStatus', link, image='', contextMenu=contextMenu, replaceMenu=False, isFolder=type=='folder')
else:
link={'action2':'notfinished'}
self.drawItem(title, 'DownloadStatus', link, image='', contextMenu=contextMenu, replaceMenu=False)
view_style('DownloadStatus') view_style('DownloadStatus')
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=True) xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=True)

View File

@ -249,6 +249,7 @@ class Libtorrent:
def downloadProcess(self, contentId): def downloadProcess(self, contentId):
self.startSession() self.startSession()
self.paused=False
db=DownloadDB() db=DownloadDB()
ContentList=self.getContentList() ContentList=self.getContentList()
if len(ContentList)==1 or contentId: if len(ContentList)==1 or contentId:
@ -261,7 +262,7 @@ class Libtorrent:
path=os.path.join(self.storageDirectory, title) path=os.path.join(self.storageDirectory, title)
type='folder' type='folder'
add=db.add(title, path, type, {'progress':0}) add=db.add(title, path, type, {'progress':0}, 'downloading')
if add: if add:
if None!=contentId: if None!=contentId:
self.continueSession(int(contentId), Offset=0, seeding=False) self.continueSession(int(contentId), Offset=0, seeding=False)
@ -270,19 +271,31 @@ class Libtorrent:
self.torrentHandle.piece_priority(i, 6) self.torrentHandle.piece_priority(i, 6)
thread.start_new_thread(self.downloadLoop, (title,)) thread.start_new_thread(self.downloadLoop, (title,))
def downloadLoop(self, title): def downloadLoop(self, title):
iterator=0 i=1
db=DownloadDB() db=DownloadDB()
while iterator<100: while db.get(title):
xbmc.sleep(1000) xbmc.sleep(1000*i)
status = self.torrentHandle.status() status=db.get_status(title)
if not self.paused:
if status=='pause':
i=10
self.paused=True
self.session.pause()
else:
if status!='pause':
i=1
self.paused=False
self.session.resume()
s = self.torrentHandle.status()
info={} info={}
info['upload']=status.upload_payload_rate info['upload']=s.upload_payload_rate
info['download']=status.download_payload_rate info['download']=s.download_payload_rate
info['peers']=status.num_peers info['peers']=s.num_peers
info['seeds']=status.num_seeds info['seeds']=s.num_seeds
iterator = int(status.progress * 100) iterator = int(s.progress * 100)
if iterator==100 and status!='seeding':
db.update_status(str(db.get(title)), 'seeding')
info['progress']=iterator info['progress']=iterator
db.update(title, info) db.update(title, info)
self.debug() self.debug()

View File

@ -203,7 +203,12 @@ dictionary = {
'Download via Libtorrent':'Скачать Libtorrent\'ом', 'Download via Libtorrent':'Скачать Libtorrent\'ом',
'Download Status':'Статус Загрузки', 'Download Status':'Статус Загрузки',
'Download has not finished yet':'Загрука не завершена', 'Download has not finished yet':'Загрука не завершена',
'Deleted! It will not stop download':'Удалено! Это не остановит загрузку', 'Deleted and Stopped!':'Загрузка удалена и остановлена!',
'Unpaused!':'Возобновлено!',
'Paused!':'Остановлено!',
'Delete and Stop':'Удалить и Остановить',
'Unpause':'Возобновить',
'Pause':'Пауза',
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.torrenter" name="Torrenter" version="2.0.9" provider-name="vadim.skorba, DiMartino"> <addon id="plugin.video.torrenter" name="Torrenter" version="2.0.10" provider-name="vadim.skorba, DiMartino">
<requires> <requires>
<import addon="xbmc.python" version="2.1.0"/> <import addon="xbmc.python" version="2.1.0"/>
<import addon="script.module.libtorrent"/> <import addon="script.module.libtorrent"/>

View File

@ -1423,25 +1423,40 @@ def delete_russian(ok=False, action='delete'):
return False return False
class DownloadDB: class DownloadDB:
def __init__(self, version=1.1): def __init__(self, version=1.2):
self.name = 'download.db3' self.name = 'download.db3'
self.version = version self.version = version
def get_all(self): def get_all(self):
self._connect() self._connect()
self.cur.execute('select addtime, title, path, type, jsoninfo from downloads order by addtime DESC') self.cur.execute('select addtime, title, path, type, jsoninfo, status from downloads order by addtime DESC')
x = self.cur.fetchall() x = self.cur.fetchall()
self._close() self._close()
return x if x else None return x if x else None
def get(self, title): def get(self, title):
try:
title = title.decode('utf-8')
except:
pass
self._connect() self._connect()
self.cur.execute('select addtime from downloads where title="' + title + '"') self.cur.execute('select addtime from downloads where title="' + title + '"')
x = self.cur.fetchone() x = self.cur.fetchone()
self._close() self._close()
return x[0] if x else None return x[0] if x else None
def add(self, title, path, type, info): def get_status(self, title):
try:
title = title.decode('utf-8')
except:
pass
self._connect()
self.cur.execute('select status from downloads where title="' + title + '"')
x = self.cur.fetchone()
self._close()
return x[0] if x else None
def add(self, title, path, type, info, status):
try: try:
title = title.decode('utf-8') title = title.decode('utf-8')
except: except:
@ -1452,8 +1467,8 @@ class DownloadDB:
pass pass
if not self.get(title): if not self.get(title):
self._connect() self._connect()
self.cur.execute('insert into downloads(addtime, title, path, type, jsoninfo)' self.cur.execute('insert into downloads(addtime, title, path, type, jsoninfo, status)'
' values(?,?,?,?,?)', (int(time.time()), title, path, type, json.dumps(info))) ' values(?,?,?,?,?,?)', (int(time.time()), title, path, type, json.dumps(info), status))
self.db.commit() self.db.commit()
self._close() self._close()
return True return True
@ -1470,6 +1485,12 @@ class DownloadDB:
self.db.commit() self.db.commit()
self._close() self._close()
def update_status(self, addtime, status):
self._connect()
self.cur.execute('UPDATE downloads SET status = "' + status + '" where addtime="' + addtime+'"')
self.db.commit()
self._close()
def delete(self, addtime): def delete(self, addtime):
self._connect() self._connect()
self.cur.execute('delete from downloads where addtime="' + addtime + '"') self.cur.execute('delete from downloads where addtime="' + addtime + '"')
@ -1518,7 +1539,7 @@ class DownloadDB:
cur.execute('pragma auto_vacuum=1') cur.execute('pragma auto_vacuum=1')
cur.execute('create table db_ver(version real)') cur.execute('create table db_ver(version real)')
cur.execute( cur.execute(
'create table downloads(addtime integer PRIMARY KEY, title varchar(32), path varchar(32), type varchar(32), jsoninfo varchar(32))') 'create table downloads(addtime integer PRIMARY KEY, title varchar(32), path varchar(32), type varchar(32), jsoninfo varchar(32), status varchar(32))')
cur.execute('insert into db_ver(version) values(?)', (self.version, )) cur.execute('insert into db_ver(version) values(?)', (self.version, ))
self.db.commit() self.db.commit()
cur.close() cur.close()