diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8192bf8..04efc65 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,13 @@ - + - + + + @@ -51,10 +53,21 @@ - - + + - + + + + + + + + + + + + @@ -64,19 +77,28 @@ - + + + + + + + + + + @@ -84,7 +106,6 @@ - @@ -101,17 +122,30 @@ + + + + + + + + + + + + - + - - + + + @@ -120,8 +154,6 @@ - - @@ -131,7 +163,6 @@ - @@ -144,7 +175,7 @@ - + @@ -194,57 +225,50 @@ - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -292,14 +316,11 @@ @@ -411,6 +435,46 @@ - + - + @@ -929,7 +993,6 @@ - @@ -954,43 +1017,16 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1271,39 +1307,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1312,16 +1315,6 @@ - - - - - - - - - - @@ -1340,22 +1333,41 @@ + + + + + + + + + + - + + + + + + + + + + @@ -1363,7 +1375,6 @@ - @@ -1372,7 +1383,7 @@ - + @@ -1400,54 +1411,133 @@ - + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Anteoloader.py b/Anteoloader.py index 2ea4578..aaf8761 100644 --- a/Anteoloader.py +++ b/Anteoloader.py @@ -24,27 +24,67 @@ import hashlib import re from StringIO import StringIO import gzip -import sys -from contextlib import closing import xbmc import xbmcgui import xbmcvfs -import xbmcplugin import Localization from functions import file_encode, isSubtitle, DownloadDB, log, debug, is_writable, unquote + + +import os +import urllib +import json +import sys +from contextlib import contextmanager, closing, nested + + +from functions import calculate, showMessage, clearStorage, DownloadDB, get_ids_video, log, debug + from torrent2http import State, Engine, MediaType -class Anteoloader: +ROOT = sys.modules["__main__"].__root__ +RESOURCES_PATH = os.path.join(ROOT, 'resources') +TORRENT2HTTP_TIMEOUT = 20 +TORRENT2HTTP_POLL = 1000 +PLAYING_EVENT_INTERVAL = 60 +MIN_COMPLETED_PIECES = 0.5 + +WINDOW_FULLSCREEN_VIDEO = 12005 + +XBFONT_LEFT = 0x00000000 +XBFONT_RIGHT = 0x00000001 +XBFONT_CENTER_X = 0x00000002 +XBFONT_CENTER_Y = 0x00000004 +XBFONT_TRUNCATED = 0x00000008 +XBFONT_JUSTIFY = 0x00000010 + +STATE_STRS = [ + 'Queued', + 'Checking', + 'Downloading metadata', + 'Downloading', + 'Finished', + 'Seeding', + 'Allocating', + 'Allocating file & Checking resume' +] + +VIEWPORT_WIDTH = 1920.0 +VIEWPORT_HEIGHT = 1088.0 +OVERLAY_WIDTH = int(VIEWPORT_WIDTH * 0.7) # 70% size +OVERLAY_HEIGHT = 150 + +ENCRYPTION_SETTINGS = { + "Forced": 0, + "Enabled": 1, + "Disabled": 2, +} + +class AnteoLoader: magnetLink = None - startPart = 0 - endPart = 0 - partOffset = 0 - torrentHandle = None - session = None engine = None - downloadThread = None - threadComplete = False + torrentFile = None def __init__(self, storageDirectory='', torrentFile='', torrentFilesDirectory='torrents'): self.storageDirectory = storageDirectory @@ -57,15 +97,10 @@ class Anteoloader: sys.exit(1) #pre settings - self.pre_buffer_bytes = 15*1024*1024 - print torrentFile - torrentFile=unquote(torrentFile) - print torrentFile - print str(xbmcvfs.exists(torrentFile)) - print str(os.path.exists(torrentFile)) - - - self.engine = Engine(uri=torrentFile) + if xbmcvfs.exists(torrentFile): + self.torrentFile = "file:///"+torrentFile.replace('\\','//').replace('////','//') + elif re.match("^magnet\:.+$", torrentFile): + self.magnetLink = torrentFile def __exit__(self): log('on __exit__') @@ -73,17 +108,22 @@ class Anteoloader: self.engine.close() log('__exit__ worked!') + def localize(self, string): + try: + return Localization.localize(string) + except: + return string - def getContentList(self, media_types=None): + def getContentList(self): + self.engine = Engine(uri=self.torrentFile) files = [] filelist = [] with closing(self.engine): self.engine.start() - if media_types is None: - media_types=[MediaType.VIDEO, MediaType.AUDIO, MediaType.SUBTITLES, MediaType.UNKNOWN] + #media_types=[MediaType.VIDEO, MediaType.AUDIO, MediaType.SUBTITLES, MediaType.UNKNOWN] while not files and not xbmc.abortRequested: - files = self.engine.list(media_types) + files = self.engine.list() self.engine.check_torrent_error() xbmc.sleep(200) @@ -91,26 +131,128 @@ class Anteoloader: stringdata = {"title": fs.name, "size": fs.size, "ind": fs.index, 'offset': fs.offset} filelist.append(stringdata) + return filelist + def saveTorrent(self, torrentUrl): + if not xbmcvfs.exists(torrentUrl): + if re.match("^magnet\:.+$", torrentUrl): + self.magnetLink = torrentUrl + self.magnetToTorrent(torrentUrl) + self.magnetLink = None + return self.torrentFile + else: + if not xbmcvfs.exists(self.torrentFilesPath): + xbmcvfs.mkdirs(self.torrentFilesPath) + torrentFile = self.torrentFilesPath + self.md5( + torrentUrl) + '.torrent' + try: + if not re.match("^http\:.+$", torrentUrl): + content = xbmcvfs.File(torrentUrl, "rb").read() + else: + request = urllib2.Request(torrentUrl) + request.add_header('Referer', torrentUrl) + request.add_header('Accept-encoding', 'gzip') + result = urllib2.urlopen(request) + if result.info().get('Content-Encoding') == 'gzip': + buf = StringIO(result.read()) + f = gzip.GzipFile(fileobj=buf) + content = f.read() + else: + content = result.read() - def stream(self, params): + localFile = xbmcvfs.File(torrentFile, "w+b") + localFile.write(content) + localFile.close() + except Exception, e: + print 'Unable to save torrent file from "' + torrentUrl + '" to "' + torrentFile + '" in Torrent::saveTorrent' + '. Exception: ' + str( + e) + return + else: + torrentFile = torrentUrl + if xbmcvfs.exists(torrentFile): + self.torrentFile = "file:///"+torrentFile.replace('\\','//').replace('////','//') + return self.torrentFile + + def md5(self, string): + hasher = hashlib.md5() + try: + hasher.update(string) + except: + hasher.update(string.encode('utf-8', 'ignore')) + return hasher.hexdigest() + + def magnetToTorrent(self, magnet): + self.torrentFile = magnet + +class AnteoPlayer(xbmc.Player): + __plugin__ = sys.modules["__main__"].__plugin__ + __settings__ = sys.modules["__main__"].__settings__ + ROOT = sys.modules["__main__"].__root__ # .decode('utf-8').encode(sys.getfilesystemencoding()) + USERAGENT = "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0" + torrentFilesDirectory = 'torrents' + debug = __settings__.getSetting('debug') == 'true' + subs_dl = __settings__.getSetting('subs_dl') == 'true' + seeding = __settings__.getSetting('keep_seeding') == 'true' and __settings__.getSetting('keep_files') == '1' + seeding_status = False + seeding_run = False + ids_video = None + episodeId = None + basename = '' + + def __init__(self, userStorageDirectory, torrentUrl, params={}): + self.userStorageDirectory = userStorageDirectory + self.torrentUrl = torrentUrl + xbmc.Player.__init__(self) + log("[TorrentPlayer] Initalized") self.params = params self.get = self.params.get - self.contentId = int(self.get("url")) if self.get("url") else 0 - #with closing(self.engine): - self.engine.start(self.contentId) + self.contentId = int(self.get("url")) + self.torrent = AnteoLoader(self.userStorageDirectory, self.torrentUrl, self.torrentFilesDirectory) + try: + if self.get("url2"): + self.ids_video = urllib.unquote_plus(self.get("url2")).split(',') + else: + self.ids_video = self.get_ids() + except: + pass + self.init() + self.setup_engine() + with closing(self.engine): + self.engine.start(self.contentId) + ready = self.buffer() + if ready: + self.stream() - ready = self.buffer() - if ready: - file_status = self.engine.file_status(self.contentId) - listitem = xbmcgui.ListItem('xxxx') - playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) - playlist.clear() - playlist.add(file_status.url, listitem) - xbmc.Player().play(playlist) - while not xbmc.abortRequested and xbmc.Player().isPlaying(): - xbmc.sleep(500) - xbmc.Player().stop() + + def __exit__(self): + log('on __exit__') + if self.engine: + self.engine.close() + log('__exit__ worked!') + + def init(self): + self.next_dl = True if self.__settings__.getSetting('next_dl') == 'true' and self.ids_video else False + log('[AnteoPlayer]: init - ' + str(self.next_dl)) + self.next_contentId = False + self.display_name = '' + self.downloadedSize = 0 + self.dialog = xbmcgui.Dialog() + self.on_playback_started = [] + self.on_playback_resumed = [] + self.on_playback_paused = [] + self.on_playback_stopped = [] + if xbmcvfs.exists(self.torrentUrl): + self.torrentUrl = "file:///"+str(self.torrentUrl).replace('\\','//').replace('////','//') + + def setup_engine(self): + encryption = True if self.__settings__.getSetting('encryption') == 'true' else False + + upload_limit = self.__settings__.getSetting("upload_limit") if self.__settings__.getSetting( + "upload_limit") != "" else 0 + download_limit = self.__settings__.getSetting("download_limit") if self.__settings__.getSetting( + "download_limit") != "" else 0 + self.pre_buffer_bytes = 15*1024*1024 + self.engine = Engine(uri=self.torrentUrl) def buffer(self): ready = False @@ -178,60 +320,19 @@ class Anteoloader: progressBar.close() return ready + def stream(self): + file_status = self.engine.file_status(self.contentId) + listitem = xbmcgui.ListItem('xxxx') + playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) + playlist.clear() + playlist.add(file_status.url, listitem) + xbmc.Player().play(playlist) + while not xbmc.abortRequested and xbmc.Player().isPlaying(): + xbmc.sleep(500) + xbmc.Player().stop() + def localize(self, string): try: return Localization.localize(string) except: - return string - - - def saveTorrent(self, torrentUrl): - if not xbmcvfs.exists(torrentUrl): - if re.match("^magnet\:.+$", torrentUrl): - self.magnetLink = torrentUrl - self.magnetToTorrent(torrentUrl) - self.magnetLink = None - return self.torrentFile - else: - if not xbmcvfs.exists(self.torrentFilesPath): - xbmcvfs.mkdirs(self.torrentFilesPath) - torrentFile = self.torrentFilesPath + self.md5( - torrentUrl) + '.torrent' - try: - if not re.match("^http\:.+$", torrentUrl): - content = xbmcvfs.File(torrentUrl, "rb").read() - else: - request = urllib2.Request(torrentUrl) - request.add_header('Referer', torrentUrl) - request.add_header('Accept-encoding', 'gzip') - result = urllib2.urlopen(request) - if result.info().get('Content-Encoding') == 'gzip': - buf = StringIO(result.read()) - f = gzip.GzipFile(fileobj=buf) - content = f.read() - else: - content = result.read() - - localFile = xbmcvfs.File(torrentFile, "w+b") - localFile.write(content) - localFile.close() - except Exception, e: - print 'Unable to save torrent file from "' + torrentUrl + '" to "' + torrentFile + '" in Torrent::saveTorrent' + '. Exception: ' + str( - e) - return - else: - torrentFile = torrentUrl - if xbmcvfs.exists(torrentFile): - self.torrentFile = "file:///"+torrentFile.replace('\\','//').replace('////','//') - return self.torrentFile - - def md5(self, string): - hasher = hashlib.md5() - try: - hasher.update(string) - except: - hasher.update(string.encode('utf-8', 'ignore')) - return hasher.hexdigest() - - def magnetToTorrent(self, magnet): - self.torrentFile = magnet \ No newline at end of file + return string \ No newline at end of file diff --git a/Core.py b/Core.py index bbabdd4..b916b6e 100644 --- a/Core.py +++ b/Core.py @@ -45,7 +45,6 @@ class Core: def __init__(self): print '!!!!!!!!!!!!!!!!!! BORN '+self.__class__.__name__ - print str(self.ROOT) if len(self.userStorageDirectory) == 0: download_dir = get_download_dir() else: @@ -234,7 +233,7 @@ class Core: lockView('wide') def test(self, params={}): - from Anteoloader import Anteoloader + from Anteoloader import AnteoLoader torrentUrl='D:\\ntest.torrent' params['url']='0' if not xbmcvfs.exists(torrentUrl): @@ -242,13 +241,14 @@ class Core: torrentUrl = action.browse(1, self.localize('Choose .torrent in video library'), 'video', '.torrent') if torrentUrl and xbmcvfs.exists(torrentUrl): if 0 != len(torrentUrl): - self.Downloader = Anteoloader(self.userStorageDirectory, torrentUrl) + self.Downloader = Downloader.Torrent(self.userStorageDirectory, torrentUrl) else: print self.__plugin__ + " Unexpected access to method Anteoloader() without torrent content" if self.Downloader: - self.Downloader.stream() + x=self.Downloader.getContentList() + print str(x) xbmc.sleep(1000) - self.Downloader.__exit__() + #self.Downloader.__exit__() def DownloadStatus(self, params={}): db = DownloadDB() @@ -972,7 +972,7 @@ class Core: def drawItem(self, title, action, link='', image='', isFolder=True, contextMenu=None, replaceMenu=True, action2='', info={}): listitem = xbmcgui.ListItem(title, iconImage=image, thumbnailImage=image) - log('[drawItem]:'+str((title, action, image, isFolder, contextMenu, replaceMenu, action2, info))) + #log('[drawItem]:'+str((title, action, image, isFolder, contextMenu, replaceMenu, action2, info))) if not info: info = {"Title": title, "plot": title} if isinstance(link, dict): link_url = '' @@ -1209,8 +1209,8 @@ class Core: def torrentPlayer(self, params={}): get = params.get - url = unquote(get("url"),None) - tdir = unquote(get("url2"),None) + url = unquote(get("url"), None) + tdir = unquote(get("url2"), None) if not url: action = xbmcgui.Dialog() @@ -1294,9 +1294,9 @@ class Core: else: print self.__plugin__ + " Unexpected access to method playTorrent() without torrent content" elif self.torrent_player == '2': - from BTClientPlayer import BTClientPlayer + from Anteoloader import AnteoPlayer if 0 != len(torrentUrl): - self.Player = BTClientPlayer(userStorageDirectory=self.userStorageDirectory, torrentUrl=torrentUrl, params=params) + self.Player = AnteoPlayer(userStorageDirectory=self.userStorageDirectory, torrentUrl=torrentUrl, params=params) else: print self.__plugin__ + " Unexpected access to method playTorrent() without torrent content" elif self.torrent_player == '1': diff --git a/Downloader.py b/Downloader.py index e1702d8..502a2c2 100644 --- a/Downloader.py +++ b/Downloader.py @@ -38,7 +38,7 @@ class Torrent(): self.player = AceStream.AceStream(storageDirectory, torrentFile, torrentFilesDirectory) elif self.player == 'anteo': - self.player = Anteoloader.Anteoloader(storageDirectory, torrentFile, torrentFilesDirectory) + self.player = Anteoloader.AnteoLoader(storageDirectory, torrentFile, torrentFilesDirectory) def __exit__(self): self.player.__exit__() diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index bfd2d39..8546fc5 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -43,7 +43,7 @@ Delete files Save files Ask to save - BTclient (python-libtorrent via http) + Torrent2HTTP (python-libtorrent via http) Auto-unblocking proxy None Anti-zapret diff --git a/resources/language/Hebrew/strings.xml b/resources/language/Hebrew/strings.xml index d936cc2..fb0adcc 100644 --- a/resources/language/Hebrew/strings.xml +++ b/resources/language/Hebrew/strings.xml @@ -43,7 +43,7 @@ מחיקת קבצים שמור קבצים שאל לשמירה - BTclient (python-libtorrent via http) + Torrent2HTTP (python-libtorrent via http) ביטול אוטומטי של חסימת proxy ללא Anti-zapret diff --git a/resources/language/Russian/strings.xml b/resources/language/Russian/strings.xml index 1b4d968..5c4a919 100644 --- a/resources/language/Russian/strings.xml +++ b/resources/language/Russian/strings.xml @@ -43,7 +43,7 @@ Удалять файлы Сохранять файлы Спросить о сохранении - BTclient (python-libtorrent по http) + Torrent2HTTP (python-libtorrent по http) Антизапрет (прокси) Не использовать Anti-zapret