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

33
Core.py
View File

@ -309,7 +309,17 @@ class Core:
if action2 == 'delete':
db.delete(addtime)
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':
db.clear()
@ -319,23 +329,30 @@ class Core:
items = db.get_all()
if items:
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))
progress=int(jsoninfo.get('progress'))
title = '[%d%%] %s' % (progress, title)
if progress<100:
if status=='pause': status_sign='[||]'
else:status_sign='[>]'
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:
d,u=int(jsoninfo['download']/ 1000000), int(jsoninfo['upload'] / 1000000)
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)
link={'action2':'notfinished'}
contextMenu=[((self.localize('Delete from %s') % self.localize('Download Status'), ListString % ('delete', 'addtime', str(addtime))))]
self.drawItem(title, 'DownloadStatus', link, image='', contextMenu=contextMenu, replaceMenu=True)
if status!='pause':
contextMenu=[((self.localize('Delete and Stop'), ListString % ('delete', 'addtime', str(addtime)))),
((self.localize('Pause'), ListString % ('pause', 'addtime', str(addtime)))),]
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')}
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')
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=True)

View File

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

View File

@ -203,7 +203,12 @@ dictionary = {
'Download via Libtorrent':'Скачать Libtorrent\'ом',
'Download Status':'Статус Загрузки',
'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"?>
<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>
<import addon="xbmc.python" version="2.1.0"/>
<import addon="script.module.libtorrent"/>

View File

@ -1423,25 +1423,40 @@ def delete_russian(ok=False, action='delete'):
return False
class DownloadDB:
def __init__(self, version=1.1):
def __init__(self, version=1.2):
self.name = 'download.db3'
self.version = version
def get_all(self):
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()
self._close()
return x if x else None
def get(self, title):
try:
title = title.decode('utf-8')
except:
pass
self._connect()
self.cur.execute('select addtime 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):
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:
title = title.decode('utf-8')
except:
@ -1452,8 +1467,8 @@ class DownloadDB:
pass
if not self.get(title):
self._connect()
self.cur.execute('insert into downloads(addtime, title, path, type, jsoninfo)'
' values(?,?,?,?,?)', (int(time.time()), title, path, type, json.dumps(info)))
self.cur.execute('insert into downloads(addtime, title, path, type, jsoninfo, status)'
' values(?,?,?,?,?,?)', (int(time.time()), title, path, type, json.dumps(info), status))
self.db.commit()
self._close()
return True
@ -1470,6 +1485,12 @@ class DownloadDB:
self.db.commit()
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):
self._connect()
self.cur.execute('delete from downloads where addtime="' + addtime + '"')
@ -1518,7 +1539,7 @@ class DownloadDB:
cur.execute('pragma auto_vacuum=1')
cur.execute('create table db_ver(version real)')
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, ))
self.db.commit()
cur.close()