From fcb7947ec58bd85145d2caaa8723fde9038b1246 Mon Sep 17 00:00:00 2001 From: DiMartinoXBMC Date: Tue, 13 Jan 2015 17:24:53 +0300 Subject: [PATCH] download by libtorrent --- .idea/workspace.xml | 789 +++++++++----------------------------------- Core.py | 35 +- Libtorrent.py | 35 +- Localization.py | 7 +- addon.xml | 2 +- functions.py | 33 +- 6 files changed, 238 insertions(+), 663 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0ed5017..1009b55 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,22 +1,11 @@ - - - - - - - + - - - - - @@ -52,35 +41,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -88,20 +51,45 @@ - + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -110,38 +98,18 @@ - + - - - - - - - - - - - - - - - - - - - - - + - - + + @@ -151,24 +119,22 @@ - + - - + + - + - + - - - + @@ -176,7 +142,7 @@ - + @@ -186,7 +152,7 @@ - + @@ -196,128 +162,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -347,16 +194,16 @@ @@ -634,7 +481,11 @@ 1420996631774 1420996631774 - diff --git a/Core.py b/Core.py index 69c0772..feddc5e 100644 --- a/Core.py +++ b/Core.py @@ -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 jsoninfo.get('seeds')!=None and jsoninfo.get('peers')!=None and \ + 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) diff --git a/Libtorrent.py b/Libtorrent.py index d4b9cbc..4b16af5 100644 --- a/Libtorrent.py +++ b/Libtorrent.py @@ -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() diff --git a/Localization.py b/Localization.py index 646a3f7..01bafc8 100644 --- a/Localization.py +++ b/Localization.py @@ -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':'Пауза', } } diff --git a/addon.xml b/addon.xml index 1b0ed29..ca01081 100644 --- a/addon.xml +++ b/addon.xml @@ -1,5 +1,5 @@  - + diff --git a/functions.py b/functions.py index 41698b3..fa85cbb 100644 --- a/functions.py +++ b/functions.py @@ -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()