plugin.video.torrenter/AceStream.py

171 lines
6.2 KiB
Python
Raw Permalink Normal View History

2015-01-09 14:11:21 +03:00
# -*- coding: utf-8 -*-
'''
2015-06-30 18:08:57 +03:00
Torrenter v2 plugin for XBMC/Kodi
Copyright (C) 2012-2015 Vadim Skorba v1 - DiMartino v2
2016-02-13 20:33:16 +03:00
https://forums.tvaddons.ag/addon-releases/29224-torrenter-v2.html
2015-06-30 18:08:57 +03:00
2015-01-09 14:11:21 +03:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import urllib2
import urllib
import hashlib
import re
import base64
from StringIO import StringIO
2016-12-13 19:12:14 +03:00
import zlib
2015-01-09 14:11:21 +03:00
2015-06-23 23:00:27 +03:00
from functions import file_decode, file_encode
2017-01-06 00:22:10 +03:00
from functions import magnet_alert, log, loadsw_onstop
2015-01-09 14:11:21 +03:00
import xbmcvfs
class AceStream:
try:
fpath = os.path.expanduser("~")
pfile = os.path.join(fpath, 'AppData\Roaming\ACEStream\engine', 'acestream.port')
gf = open(pfile, 'r')
aceport = int(gf.read())
gf.close()
2015-12-21 18:15:21 +03:00
log('[AceStream]: aceport - '+str(aceport))
2015-01-09 14:11:21 +03:00
except:
aceport = 62062
torrentFile = None
magnetLink = None
storageDirectory = ''
torrentFilesDirectory = 'torrents'
startPart = 0
endPart = 0
partOffset = 0
torrentHandle = None
session = None
downloadThread = None
threadComplete = False
lt = None
def __init__(self, storageDirectory='', torrentFile='', torrentFilesDirectory='torrents'):
try:
from ASCore import TSengine as tsengine
2015-12-21 18:15:21 +03:00
log('Imported TSengine from ASCore')
2015-01-09 14:11:21 +03:00
except Exception, e:
2015-12-21 18:15:21 +03:00
log('Error importing TSengine from ASCore. Exception: ' + str(e))
2015-01-09 14:11:21 +03:00
return
self.TSplayer = tsengine()
del tsengine
self.torrentFilesDirectory = torrentFilesDirectory
self.storageDirectory = storageDirectory
2015-06-23 23:00:27 +03:00
_path = os.path.join(self.storageDirectory, self.torrentFilesDirectory) + os.sep
2015-12-21 18:15:21 +03:00
if re.match("^magnet\:.+$", torrentFile):
torrentFile = self.magnetToTorrent(torrentFile)
2015-01-09 14:11:21 +03:00
if not xbmcvfs.exists(_path):
xbmcvfs.mkdirs(_path)
if xbmcvfs.exists(torrentFile):
self.torrentFile = torrentFile
content = xbmcvfs.File(torrentFile, "rb").read()
self.torrentFileInfo = self.TSplayer.load_torrent(base64.b64encode(content), 'RAW')
def __exit__(self):
self.TSplayer.end()
2017-01-06 00:22:10 +03:00
loadsw_onstop() # Reload Search Window
2015-01-09 14:11:21 +03:00
def play_url_ind(self, ind, label, icon):
self.TSplayer.play_url_ind(int(ind), label, str(icon), '')
def saveTorrent(self, torrentUrl):
if re.match("^magnet\:.+$", torrentUrl):
2015-12-21 18:15:21 +03:00
torrentFile = self.magnetToTorrent(torrentUrl)
content = xbmcvfs.File(file_decode(torrentFile), "rb").read()
2015-01-09 14:11:21 +03:00
else:
torrentFile = self.storageDirectory + os.sep + self.torrentFilesDirectory + os.sep + self.md5(
torrentUrl) + '.torrent'
try:
2016-12-13 19:12:14 +03:00
if not re.match("^[htps]+?://.+$|^://.+$", torrentUrl):
log('xbmcvfs.File for %s' % torrentUrl)
content = xbmcvfs.File(torrentUrl, "rb").read()
2015-01-09 14:11:21 +03:00
else:
2016-12-13 19:12:14 +03:00
log('request for %s' % torrentUrl)
content = self.makeRequest(torrentUrl)
2015-01-09 14:11:21 +03:00
localFile = xbmcvfs.File(torrentFile, "w+b")
localFile.write(content)
localFile.close()
except Exception, e:
2015-12-21 18:15:21 +03:00
log('Unable to save torrent file from "' + torrentUrl + '" to "' + torrentFile +
'" in Torrent::saveTorrent' + '. Exception: ' + str(e))
2015-01-09 14:11:21 +03:00
return
2015-12-21 18:15:21 +03:00
if xbmcvfs.exists(torrentFile):
self.torrentFile = torrentFile
self.torrentFileInfo = self.TSplayer.load_torrent(base64.b64encode(content), 'RAW')
return self.torrentFile
2015-01-09 14:11:21 +03:00
2016-12-13 19:12:14 +03:00
def makeRequest(self, torrentUrl):
torrentUrl = re.sub('^://', 'http://', torrentUrl)
x = re.search("://(.+?)/|://(.+?)$", torrentUrl)
if x:
baseurl = x.group(1) if x.group(1) else x.group(2)
else:
baseurl =''
headers = [('User-Agent',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36'),
('Referer', 'http://%s/' % baseurl), ('Accept-encoding', 'gzip'), ]
opener = urllib2.build_opener()
opener.addheaders = headers
result = opener.open(torrentUrl)
if result.info().get('Content-Encoding') == 'gzip':
buf = StringIO(result.read())
decomp = zlib.decompressobj(16 + zlib.MAX_WBITS)
content = decomp.decompress(buf.getvalue())
else:
content = result.read()
return content
2015-01-09 14:11:21 +03:00
def magnetToTorrent(self, magnet):
2015-12-21 18:15:21 +03:00
try:
2016-03-21 19:36:44 +03:00
from SkorbaLoader import SkorbaLoader
torrent = SkorbaLoader(self.storageDirectory, magnet)
2015-12-21 18:15:21 +03:00
torrent.magnetToTorrent(magnet)
self.torrentFile = torrent.torrentFile
log('[AceStream][magnetToTorrent]: self.torrentFile '+str(self.torrentFile))
return self.torrentFile
except:
magnet_alert()
exit()
2015-01-09 14:11:21 +03:00
def getFilePath(self, contentId=0):
fileList = self.getContentList()
for i in fileList:
if i['ind'] == contentId:
2015-06-23 23:00:27 +03:00
return os.path.join(file_encode(self.storageDirectory), i['title'])
2015-01-09 14:11:21 +03:00
def getContentList(self):
filelist = []
for k, v in self.TSplayer.files.iteritems():
stringdata = {"title": urllib.unquote_plus(k), "ind": int(v)}
filelist.append(stringdata)
return filelist
def md5(self, string):
hasher = hashlib.md5()
try:
hasher.update(string)
except:
hasher.update(string.encode('utf-8', 'ignore'))
2015-06-23 23:00:27 +03:00
return hasher.hexdigest()