Torrent Download:
- Supporto Serie ed episodi
This commit is contained in:
+98
-37
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os, sys
|
||||
import re, os, sys
|
||||
|
||||
# from builtins import str
|
||||
from builtins import range
|
||||
@@ -14,7 +14,7 @@ if PY3:
|
||||
else:
|
||||
import urllib
|
||||
|
||||
import time, requests, xbmc, xbmcgui, xbmcaddon
|
||||
import time, requests, xbmc, xbmcgui, xbmcaddon, shutil
|
||||
from core import filetools, jsontools
|
||||
from core.support import dbg, log, match
|
||||
# from core import httptools
|
||||
@@ -22,10 +22,11 @@ from core.support import dbg, log, match
|
||||
from platformcode import config, platformtools
|
||||
from threading import Thread, currentThread
|
||||
from torrentool.api import Torrent
|
||||
from lib.guessit import guessit
|
||||
|
||||
elementum_setting = xbmcaddon.Addon(id='plugin.video.elementum')
|
||||
elementum_host = 'http://127.0.0.1:' + elementum_setting.getSetting('remote_port') + '/torrents/'
|
||||
extensions_list = ['aaf', '3gp', 'asf', 'avi', 'flv', 'mpeg', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mpg', 'mpe', 'mp4', 'ogg', 'wmv']
|
||||
extensions_list = ['.aaf', '.3gp', '.asf', '.avi', '.flv', '.mpeg', '.m1v', '.m2v', '.m4v', '.mkv', '.mov', '.mpg', '.mpe', '.mp4', '.ogg', '.wmv']
|
||||
|
||||
|
||||
# Returns an array of possible video url's from the page_url
|
||||
@@ -116,9 +117,13 @@ def elementum_monitor():
|
||||
while not filetools.isfile(filetools.join(elementum_setting.getSetting('torrents_path'), TorrentName + '.torrent')):
|
||||
time.sleep(1)
|
||||
else:
|
||||
data = match(elementum_host, alfa_s=True).data
|
||||
log('Watch')
|
||||
try:
|
||||
data = requests.get(elementum_host).json()
|
||||
except:
|
||||
data = ''
|
||||
if data:
|
||||
json = jsontools.load(data)['items']
|
||||
json = data['items']
|
||||
|
||||
for it in json:
|
||||
Partial = float(match(it['label'], patron=r'(\d+\.\d+)%').match)
|
||||
@@ -128,11 +133,8 @@ def elementum_monitor():
|
||||
update_download_info(Partial, Title, TorrentName, File, Json)
|
||||
partials.append(Partial)
|
||||
|
||||
result = 0
|
||||
for p in partials:
|
||||
result += p
|
||||
|
||||
if result / len(partials) == 100:
|
||||
partials.sort()
|
||||
if len(partials) > 0 and partials[0] == 100:
|
||||
unset_elementum()
|
||||
|
||||
time.sleep(1)
|
||||
@@ -156,55 +158,114 @@ def update_download_info(Partial, Title, TorrentName, File, Json):
|
||||
tpath = filetools.join(xbmc.translatePath(elementum_setting.getSetting('torrents_path')), TorrentName)
|
||||
if Json['downloadSize'] == 0:
|
||||
size = Torrent.from_file(tpath + '.torrent').total_size
|
||||
jsontools.update_node(size, File, 'downloadSize', path)
|
||||
jsontools.update_node(size, File, 'downloadSize', path, silent=True)
|
||||
if Json['downloadFilename'] != dlpath and 'backupFilename' not in Json:
|
||||
jsontools.update_node(Json['downloadFilename'], File, 'backupFilename', path)
|
||||
jsontools.update_node(dlpath, File, 'downloadFilename', path)
|
||||
jsontools.update_node(Json['downloadFilename'], File, 'backupFilename', path, silent=True)
|
||||
jsontools.update_node(dlpath, File, 'downloadFilename', path, silent=True)
|
||||
if Json['downloadProgress'] != Partial and Partial != 0:
|
||||
jsontools.update_node(Partial, File, 'downloadProgress', path)
|
||||
jsontools.update_node(4, File, 'downloadStatus', path)
|
||||
jsontools.update_node(Partial, File, 'downloadProgress', path, silent=True)
|
||||
jsontools.update_node(4, File, 'downloadStatus', path, silent=True)
|
||||
if Partial == 100:
|
||||
jsontools.update_node(Json['downloadSize'], File, 'downloadCompleted', path)
|
||||
jsontools.update_node(2, File, 'downloadStatus', path)
|
||||
jsontools.update_node(Json['downloadSize'], File, 'downloadCompleted', path, silent=True)
|
||||
jsontools.update_node(2, File, 'downloadStatus', path, silent=True)
|
||||
requests.get(elementum_host + 'pause/' + TorrentName)
|
||||
filetools.remove(tpath + '.torrent')
|
||||
time.sleep(5)
|
||||
time.sleep(1)
|
||||
rename(TorrentName, path)
|
||||
# requests.get(elementum_host + 'delete/' + TorrentName + '?files=false')
|
||||
|
||||
|
||||
|
||||
def rename(TorrentName, Path):
|
||||
File, Json = find_file(TorrentName)
|
||||
path = Json['downloadFilename']
|
||||
if filetools.isdir(path):
|
||||
extension = ''
|
||||
files = filetools.listdir(path)
|
||||
sep = '/' if path.lower().startswith("smb://") else os.sep
|
||||
oldName = path.split(sep)[-1]
|
||||
newName = Json['backupFilename']
|
||||
for f in files:
|
||||
ext = f.split('.')[-1]
|
||||
if ext in extensions_list: extension = '.' + ext
|
||||
filetools.rename(filetools.join(path, f), f.replace(oldName, newName))
|
||||
filetools.rename(path, newName)
|
||||
jsontools.update_node(filetools.join(newName,newName + extension), File, 'downloadFilename', Path)
|
||||
if Json['infoLabels']['mediatype'] == 'movie':
|
||||
if filetools.isdir(path):
|
||||
extension = ''
|
||||
files = filetools.listdir(path)
|
||||
sep = '/' if path.lower().startswith("smb://") else os.sep
|
||||
oldName = path.split(sep)[-1]
|
||||
newName = Json['backupFilename']
|
||||
for f in files:
|
||||
ext = os.path.splitext(f)[-1]
|
||||
if ext in extensions_list: extension = ext
|
||||
filetools.rename(filetools.join(path, f), f.replace(oldName, newName))
|
||||
filetools.rename(path, newName)
|
||||
jsontools.update_node(filetools.join(newName,newName + extension), File, 'downloadFilename', Path)
|
||||
|
||||
else:
|
||||
oldName = filetools.split(path)[-1]
|
||||
newName = Json['backupFilename'] + os.path.splitext(oldName)[-1]
|
||||
filetools.rename(path, newName)
|
||||
jsontools.update_node(newName, File, 'downloadFilename', Path)
|
||||
else:
|
||||
oldName = filetools.split(path)[-1]
|
||||
newName = Json['backupFilename'] + '.' + oldName.split('.')[-1]
|
||||
filetools.rename(path, newName)
|
||||
jsontools.update_node(newName, File, 'downloadFilename', Path)
|
||||
sep = '/' if path.lower().startswith("smb://") else os.sep
|
||||
FolderName = Json['backupFilename'].split(sep)[0]
|
||||
Title = re.sub(r'(\s*\[[^\]]+\])', '', FolderName)
|
||||
if filetools.isdir(path):
|
||||
files = filetools.listdir(path)
|
||||
file_dict = {}
|
||||
for f in files:
|
||||
title = process_filename(f, Title, ext=False)
|
||||
ext = os.path.splitext(f)[-1]
|
||||
name = os.path.splitext(f)[0]
|
||||
if title not in file_dict and ext in extensions_list:
|
||||
file_dict[title] = name
|
||||
|
||||
for title, name in file_dict.items():
|
||||
for f in files:
|
||||
if name in f:
|
||||
# log('Name:',name,'Title:',title)
|
||||
filetools.rename(filetools.join(path, f), f.replace(name, title))
|
||||
|
||||
filetools.rename(path, FolderName)
|
||||
jsontools.update_node(FolderName, File, 'downloadFilename', Path)
|
||||
else:
|
||||
filename = filetools.split(path)[-1]
|
||||
title = process_filename(filename, Title)
|
||||
NewFolder = filetools.join(config.get_setting('downloadpath'), FolderName)
|
||||
if not filetools.isdir(NewFolder):
|
||||
filetools.mkdir(NewFolder)
|
||||
from_folder = filetools.join(config.get_setting('downloadpath'), filename)
|
||||
to_folder = filetools.join(FolderName, title)
|
||||
filetools.move(from_folder, to_folder)
|
||||
jsontools.update_node(filetools.join(config.get_setting('downloadpath'),to_folder), File, 'downloadFilename', Path)
|
||||
|
||||
def process_filename(filename, Title, ext=True):
|
||||
extension = os.path.splitext(filename)[-1]
|
||||
parsedTitle = guessit(filename)
|
||||
t = parsedTitle.get('title', '')
|
||||
episode = ''
|
||||
s = ' - '
|
||||
if parsedTitle.get('episode') and parsedTitle.get('season'):
|
||||
if type(parsedTitle.get('season')) == list:
|
||||
episode += str(parsedTitle.get('season')[0]) + '-' + str(parsedTitle.get('season')[-1])
|
||||
else:
|
||||
episode += str(parsedTitle.get('season'))
|
||||
|
||||
if type(parsedTitle.get('episode')) == list:
|
||||
episode += 'x' + str(parsedTitle.get('episode')[0]).zfill(2) + '-' + str(parsedTitle.get('episode')[-1]).zfill(2)
|
||||
else:
|
||||
episode += 'x' + str(parsedTitle.get('episode')).zfill(2)
|
||||
elif parsedTitle.get('season') and type(parsedTitle.get('season')) == list:
|
||||
episode += s + config.get_localized_string(30140) + " " +str(parsedTitle.get('season')[0]) + '-' + str(parsedTitle.get('season')[-1])
|
||||
elif parsedTitle.get('season'):
|
||||
episode += s + config.get_localized_string(60027) % str(parsedTitle.get('season'))
|
||||
if parsedTitle.get('episode_title'):
|
||||
episode += s + parsedTitle.get('episode_title')
|
||||
title = (t if t else Title) + s + episode + (extension if ext else '')
|
||||
return title
|
||||
|
||||
def unset_elementum():
|
||||
log('UNSET Elementum')
|
||||
Sleep = False
|
||||
if config.get_setting('elementumtype'):
|
||||
if config.get_setting('elementumtype') and config.get_setting('elementumtype') != elementum_setting.gerSetting('download_storage'):
|
||||
elementum_setting.setSetting('download_storage', str(config.get_setting('elementumtype')))
|
||||
Sleep = True
|
||||
if config.get_setting('elementumdl'):
|
||||
if config.get_setting('elementumdl') and config.get_setting('elementumdl') != elementum_setting.gerSetting('download_path'):
|
||||
elementum_setting.setSetting('download_path', config.get_setting('elementumdl'))
|
||||
Sleep = True
|
||||
if Sleep:
|
||||
time.sleep(3)
|
||||
time.sleep(1)
|
||||
|
||||
########################## ELEMENTUM MONITOR TEST ##########################
|
||||
|
||||
Reference in New Issue
Block a user