plugin.video.torrenter/AceStream.py

154 lines
5.5 KiB
Python
Raw 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
http://forum.kodi.tv/showthread.php?tid=214366
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
import gzip
2015-06-23 23:00:27 +03:00
from functions import file_decode, file_encode
2015-12-21 18:15:21 +03:00
from functions import magnet_alert, log, debug
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()
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:
2015-01-20 00:46:39 +03:00
if not re.match("^http\:.+$", torrentUrl):
2015-01-09 14:11:21 +03:00
content = xbmcvfs.File(file_decode(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:
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
def magnetToTorrent(self, magnet):
2015-12-21 18:15:21 +03:00
try:
from Libtorrent import Libtorrent
torrent = Libtorrent(self.storageDirectory, magnet)
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()