Merge branch 'development' into Flask

# Conflicts:
#	bazarr.py
#	bazarr/main.py
#	views/menu.tpl
#	views/providers.tpl
#	views/settings.tpl
#	views/settings_general.tpl
#	views/settings_radarr.tpl
#	views/settings_subtitles.tpl
pull/884/head
Louis Vézina 5 years ago
commit 091f9270fb

@ -45,8 +45,10 @@ If you need something that is not already part of Bazarr, feel free to create a
* Argenteam
* Assrt
* BetaSeries
* BSPlayer
* GreekSubtitles
* Hosszupuska
* LegendasDivx
* LegendasTV
* Napiprojekt
* Napisy24
@ -65,6 +67,7 @@ If you need something that is not already part of Bazarr, feel free to create a
* SubZ
* Supersubtitles
* Titlovi
* Titrari.ro
* TVSubtitles
* XSubs
* Zimuku

@ -8,6 +8,7 @@ import subprocess as sp
import time
import os
import sys
import signal
from bazarr.get_args import args
@ -31,15 +32,97 @@ check_python_version()
dir_name = os.path.dirname(__file__)
def start_bazarr():
class ProcessRegistry:
def register(self, process):
pass
def unregister(self, process):
pass
class DaemonStatus(ProcessRegistry):
def __init__(self):
self.__should_stop = False
self.__processes = set()
def register(self, process):
self.__processes.add(process)
def unregister(self, process):
self.__processes.remove(process)
'''
Waits all the provided processes for the specified amount of time in seconds.
'''
@staticmethod
def __wait_for_processes(processes, timeout):
reference_ts = time.time()
elapsed = 0
remaining_processes = list(processes)
while elapsed < timeout and len(remaining_processes) > 0:
remaining_time = timeout - elapsed
for ep in list(remaining_processes):
if ep.poll() is not None:
remaining_processes.remove(ep)
else:
if remaining_time > 0:
if PY3:
try:
ep.wait(remaining_time)
remaining_processes.remove(ep)
except sp.TimeoutExpired:
pass
else:
'''
In python 2 there is no such thing as some mechanism to wait with a timeout.
'''
time.sleep(1)
elapsed = time.time() - reference_ts
remaining_time = timeout - elapsed
return remaining_processes
'''
Sends to every single of the specified processes the given signal and (if live_processes is not None) append to it processes which are still alive.
'''
@staticmethod
def __send_signal(processes, signal_no, live_processes=None):
for ep in processes:
if ep.poll() is None:
if live_processes is not None:
live_processes.append(ep)
try:
ep.send_signal(signal_no)
except Exception as e:
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (signal_no, ep.pid, e))
return live_processes
'''
Flags this instance as should stop and terminates as smoothly as possible children processes.
'''
def stop(self):
self.__should_stop = True
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
DaemonStatus.__send_signal(live_processes, signal.SIGTERM)
def should_stop(self):
return self.__should_stop
def start_bazarr(process_registry=ProcessRegistry()):
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]
ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
process_registry.register(ep)
print("Bazarr starting...")
try:
while True:
line = ep.stdout.readline()
if line == '' or not line:
# Process ended so let's unregister it
process_registry.unregister(ep)
break
sys.stdout.buffer.write(line)
sys.stdout.flush()
@ -62,7 +145,7 @@ if __name__ == '__main__':
pass
def daemon():
def daemon(bazarr_runner = lambda: start_bazarr()):
if os.path.exists(stopfile):
try:
os.remove(stopfile)
@ -78,12 +161,30 @@ if __name__ == '__main__':
except:
print('Unable to delete restart file.')
else:
start_bazarr()
bazarr_runner()
start_bazarr()
bazarr_runner = lambda: start_bazarr()
# Keep the script running forever.
while True:
daemon()
should_stop = lambda: False
if PY3:
daemonStatus = DaemonStatus()
def shutdown():
# indicates that everything should stop
daemonStatus.stop()
# emulate a Ctrl C command on itself (bypasses the signal thing but, then, emulates the "Ctrl+C break")
os.kill(os.getpid(), signal.SIGINT)
signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())
should_stop = lambda: daemonStatus.should_stop()
bazarr_runner = lambda: start_bazarr(daemonStatus)
bazarr_runner()
# Keep the script running forever until stop is requested through term or keyboard interrupt
while not should_stop():
daemon(bazarr_runner)
time.sleep(1)

@ -101,6 +101,10 @@ defaults = {
'password': '',
'random_agents': 'True'
},
'legendasdivx': {
'username': '',
'password': ''
},
'legendastv': {
'username': '',
'password': ''

@ -38,8 +38,8 @@ PROVIDER_THROTTLE_MAP = {
}
}
PROVIDERS_FORCED_OFF = ["addic7ed", "tvsubtitles", "legendastv", "napiprojekt", "shooter", "hosszupuska",
"supersubtitles", "titlovi", "argenteam", "assrt", "subscene"]
PROVIDERS_FORCED_OFF = ["addic7ed", "tvsubtitles", "legendasdivx", "legendastv", "napiprojekt", "shooter", "hosszupuska",
"supersubtitles", "titlovi", "titrari", "argenteam", "assrt", "subscene"]
throttle_count = {}
@ -114,6 +114,9 @@ def get_providers_auth():
'password': settings.subscene.password,
'only_foreign': False, # fixme
},
'legendasdivx': {'username': settings.legendasdivx.username,
'password': settings.legendasdivx.password,
},
'legendastv': {'username': settings.legendastv.username,
'password': settings.legendastv.password,
},

@ -9,6 +9,7 @@ os.environ["BAZARR_VERSION"] = bazarr_version
import gc
import sys
import libs
import io
import six
from six.moves import zip
@ -193,11 +194,11 @@ def shutdown():
else:
database.close()
try:
stop_file = open(os.path.join(args.config_dir, "bazarr.stop"), "w")
stop_file = io.open(os.path.join(args.config_dir, "bazarr.stop"), "w", encoding='UTF-8')
except Exception as e:
logging.error('BAZARR Cannot create bazarr.stop file.')
else:
stop_file.write('')
stop_file.write(six.text_type(''))
stop_file.close()
sys.exit(0)
return ''
@ -213,12 +214,12 @@ def restart():
else:
database.close()
try:
restart_file = open(os.path.join(args.config_dir, "bazarr.restart"), "w")
restart_file = io.open(os.path.join(args.config_dir, "bazarr.restart"), "w", encoding='UTF-8')
except Exception as e:
logging.error('BAZARR Cannot create bazarr.restart file.')
else:
logging.info('Bazarr is being restarted...')
restart_file.write('')
restart_file.write(six.text_type(''))
restart_file.close()
sys.exit(0)
return ''
@ -379,6 +380,8 @@ def save_wizard():
settings.addic7ed.password = request.form.get('settings_addic7ed_password') or ''
settings.addic7ed.random_agents = text_type(settings_addic7ed_random_agents) or ''
settings.assrt.token = request.form.get('settings_assrt_token') or ''
settings.legendasdivx.username = request.form.get('settings_legendasdivx_username') or ''
settings.legendasdivx.password = request.form.get('settings_legendasdivx_password') or ''
settings.legendastv.username = request.form.get('settings_legendastv_username') or ''
settings.legendastv.password = request.form.get('settings_legendastv_password') or ''
settings.opensubtitles.username = request.form.get('settings_opensubtitles_username') or ''
@ -1108,6 +1111,8 @@ def save_settings():
settings.addic7ed.password = request.form.get('settings_addic7ed_password')
settings.addic7ed.random_agents = text_type(settings_addic7ed_random_agents)
settings.assrt.token = request.form.get('settings_assrt_token')
settings.legendasdivx.username = request.form.get('settings_legendasdivx_username')
settings.legendasdivx.password = request.form.get('settings_legendasdivx_password')
settings.legendastv.username = request.form.get('settings_legendastv_username')
settings.legendastv.password = request.form.get('settings_legendastv_password')
settings.opensubtitles.username = request.form.get('settings_opensubtitles_username')
@ -1279,7 +1284,7 @@ def system():
throttled_providers = list_throttled_providers()
try:
with open(os.path.join(args.config_dir, 'config', 'releases.txt'), 'r') as f:
with io.open(os.path.join(args.config_dir, 'config', 'releases.txt'), 'r', encoding='UTF-8') as f:
releases = ast.literal_eval(f.read())
except Exception as e:
releases = []
@ -1305,7 +1310,7 @@ def system():
def get_logs():
logs = []
with open(os.path.join(args.config_dir, 'log', 'bazarr.log')) as file:
with io.open(os.path.join(args.config_dir, 'log', 'bazarr.log'), encoding='UTF-8') as file:
for line in file.readlines():
lin = []
lin = line.split('|')

@ -543,6 +543,10 @@ def scan_video(path, dont_use_actual_file=False, hints=None, providers=None, ski
if video.size > 10485760:
logger.debug('Size is %d', video.size)
osub_hash = None
if "bsplayer" in providers:
video.hashes['bsplayer'] = osub_hash = hash_opensubtitles(hash_path)
if "opensubtitles" in providers:
video.hashes['opensubtitles'] = osub_hash = hash_opensubtitles(hash_path)

@ -0,0 +1,235 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import io
import os
from requests import Session
from guessit import guessit
from subliminal_patch.providers import Provider
from subliminal_patch.subtitle import Subtitle
from subliminal.utils import sanitize_release_group
from subliminal.subtitle import guess_matches
from subzero.language import Language
import gzip
import random
from time import sleep
from xml.etree import ElementTree
logger = logging.getLogger(__name__)
class BSPlayerSubtitle(Subtitle):
"""BSPlayer Subtitle."""
provider_name = 'bsplayer'
def __init__(self, language, filename, subtype, video, link):
super(BSPlayerSubtitle, self).__init__(language)
self.language = language
self.filename = filename
self.page_link = link
self.subtype = subtype
self.video = video
@property
def id(self):
return self.page_link
@property
def release_info(self):
return self.filename
def get_matches(self, video):
matches = set()
video_filename = video.name
video_filename = os.path.basename(video_filename)
video_filename, _ = os.path.splitext(video_filename)
video_filename = sanitize_release_group(video_filename)
subtitle_filename = self.filename
subtitle_filename = os.path.basename(subtitle_filename)
subtitle_filename, _ = os.path.splitext(subtitle_filename)
subtitle_filename = sanitize_release_group(subtitle_filename)
matches |= guess_matches(video, guessit(self.filename))
matches.add(id(self))
matches.add('hash')
return matches
class BSPlayerProvider(Provider):
"""BSPlayer Provider."""
languages = {Language('por', 'BR')} | {Language(l) for l in [
'ara', 'bul', 'ces', 'dan', 'deu', 'ell', 'eng', 'fin', 'fra', 'hun', 'ita', 'jpn', 'kor', 'nld', 'pol', 'por',
'ron', 'rus', 'spa', 'swe', 'tur', 'ukr', 'zho'
]}
SEARCH_THROTTLE = 8
# batantly based on kodi's bsplayer plugin
# also took from BSPlayer-Subtitles-Downloader
def __init__(self):
self.initialize()
def initialize(self):
self.session = Session()
self.search_url = self.get_sub_domain()
self.token = None
self.login()
def terminate(self):
self.session.close()
self.logout()
def api_request(self, func_name='logIn', params='', tries=5):
headers = {
'User-Agent': 'BSPlayer/2.x (1022.12360)',
'Content-Type': 'text/xml; charset=utf-8',
'Connection': 'close',
'SOAPAction': '"http://api.bsplayer-subtitles.com/v1.php#{func_name}"'.format(func_name=func_name)
}
data = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="{search_url}">'
'<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
'<ns1:{func_name}>{params}</ns1:{func_name}></SOAP-ENV:Body></SOAP-ENV:Envelope>'
).format(search_url=self.search_url, func_name=func_name, params=params)
logger.info('Sending request: %s.' % func_name)
for i in iter(range(tries)):
try:
self.session.headers.update(headers.items())
res = self.session.post(self.search_url, data)
return ElementTree.fromstring(res.text)
### with requests
# res = requests.post(
# url=self.search_url,
# data=data,
# headers=headers
# )
# return ElementTree.fromstring(res.text)
except Exception as ex:
logger.info("ERROR: %s." % ex)
if func_name == 'logIn':
self.search_url = self.get_sub_domain()
sleep(1)
logger.info('ERROR: Too many tries (%d)...' % tries)
raise Exception('Too many tries...')
def login(self):
# If already logged in
if self.token:
return True
root = self.api_request(
func_name='logIn',
params=('<username></username>'
'<password></password>'
'<AppID>BSPlayer v2.67</AppID>')
)
res = root.find('.//return')
if res.find('status').text == 'OK':
self.token = res.find('data').text
logger.info("Logged In Successfully.")
return True
return False
def logout(self):
# If already logged out / not logged in
if not self.token:
return True
root = self.api_request(
func_name='logOut',
params='<handle>{token}</handle>'.format(token=self.token)
)
res = root.find('.//return')
self.token = None
if res.find('status').text == 'OK':
logger.info("Logged Out Successfully.")
return True
return False
def query(self, video, video_hash, language):
if not self.login():
return []
if isinstance(language, (tuple, list, set)):
# language_ids = ",".join(language)
# language_ids = 'spa'
language_ids = ','.join(sorted(l.opensubtitles for l in language))
if video.imdb_id is None:
imdbId = '*'
else:
imdbId = video.imdb_id
sleep(self.SEARCH_THROTTLE)
root = self.api_request(
func_name='searchSubtitles',
params=(
'<handle>{token}</handle>'
'<movieHash>{movie_hash}</movieHash>'
'<movieSize>{movie_size}</movieSize>'
'<languageId>{language_ids}</languageId>'
'<imdbId>{imdbId}</imdbId>'
).format(token=self.token, movie_hash=video_hash,
movie_size=video.size, language_ids=language_ids, imdbId=imdbId)
)
res = root.find('.//return/result')
if res.find('status').text != 'OK':
return []
items = root.findall('.//return/data/item')
subtitles = []
if items:
logger.info("Subtitles Found.")
for item in items:
subID=item.find('subID').text
subDownloadLink=item.find('subDownloadLink').text
subLang= Language.fromopensubtitles(item.find('subLang').text)
subName=item.find('subName').text
subFormat=item.find('subFormat').text
subtitles.append(
BSPlayerSubtitle(subLang,subName, subFormat, video, subDownloadLink)
)
return subtitles
def list_subtitles(self, video, languages):
return self.query(video, video.hashes['bsplayer'], languages)
def get_sub_domain(self):
# s1-9, s101-109
SUB_DOMAINS = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9',
's101', 's102', 's103', 's104', 's105', 's106', 's107', 's108', 's109']
API_URL_TEMPLATE = "http://{sub_domain}.api.bsplayer-subtitles.com/v1.php"
sub_domains_end = len(SUB_DOMAINS) - 1
return API_URL_TEMPLATE.format(sub_domain=SUB_DOMAINS[random.randint(0, sub_domains_end)])
def download_subtitle(self, subtitle):
session = Session()
_addheaders = {
'User-Agent': 'Mozilla/4.0 (compatible; Synapse)'
}
session.headers.update(_addheaders)
res = session.get(subtitle.page_link)
if res:
if res.text == '500':
raise ValueError('Error 500 on server')
with gzip.GzipFile(fileobj=io.BytesIO(res.content)) as gf:
subtitle.content = gf.read()
subtitle.normalize()
return subtitle
raise ValueError('Problems conecting to the server')

@ -0,0 +1,307 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import io
import os
import rarfile
import zipfile
from requests import Session
from guessit import guessit
from subliminal_patch.exceptions import ParseResponseError
from subliminal_patch.providers import Provider
from subliminal.providers import ParserBeautifulSoup
from subliminal_patch.subtitle import Subtitle
from subliminal.video import Episode
from subliminal.subtitle import SUBTITLE_EXTENSIONS, fix_line_ending,guess_matches
from subzero.language import Language
logger = logging.getLogger(__name__)
class LegendasdivxSubtitle(Subtitle):
"""Legendasdivx Subtitle."""
provider_name = 'legendasdivx'
def __init__(self, language, video, data):
super(LegendasdivxSubtitle, self).__init__(language)
self.language = language
self.page_link = data['link']
self.hits=data['hits']
self.exact_match=data['exact_match']
self.description=data['description'].lower()
self.video = video
self.videoname =data['videoname']
@property
def id(self):
return self.page_link
@property
def release_info(self):
return self.description
def get_matches(self, video):
matches = set()
if self.videoname.lower() in self.description:
matches.update(['title'])
matches.update(['season'])
matches.update(['episode'])
# episode
if video.title and video.title.lower() in self.description:
matches.update(['title'])
if video.year and '{:04d}'.format(video.year) in self.description:
matches.update(['year'])
if isinstance(video, Episode):
# already matched in search query
if video.season and 's{:02d}'.format(video.season) in self.description:
matches.update(['season'])
if video.episode and 'e{:02d}'.format(video.episode) in self.description:
matches.update(['episode'])
if video.episode and video.season and video.series:
if '{}.s{:02d}e{:02d}'.format(video.series.lower(),video.season,video.episode) in self.description:
matches.update(['series'])
matches.update(['season'])
matches.update(['episode'])
if '{} s{:02d}e{:02d}'.format(video.series.lower(),video.season,video.episode) in self.description:
matches.update(['series'])
matches.update(['season'])
matches.update(['episode'])
# release_group
if video.release_group and video.release_group.lower() in self.description:
matches.update(['release_group'])
# resolution
if video.resolution and video.resolution.lower() in self.description:
matches.update(['resolution'])
# format
formats = []
if video.format:
formats = [video.format.lower()]
if formats[0] == "web-dl":
formats.append("webdl")
formats.append("webrip")
formats.append("web ")
for frmt in formats:
if frmt.lower() in self.description:
matches.update(['format'])
break
# video_codec
if video.video_codec:
video_codecs = [video.video_codec.lower()]
if video_codecs[0] == "h264":
formats.append("x264")
elif video_codecs[0] == "h265":
formats.append("x265")
for vc in formats:
if vc.lower() in self.description:
matches.update(['video_codec'])
break
matches |= guess_matches(video, guessit(self.description))
return matches
class LegendasdivxProvider(Provider):
"""Legendasdivx Provider."""
languages = {Language('por', 'BR')} | {Language('por')}
SEARCH_THROTTLE = 8
site = 'https://www.legendasdivx.pt'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin': 'https://www.legendasdivx.pt',
'Referer': 'https://www.legendasdivx.pt',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'
}
loginpage = site + '/forum/ucp.php?mode=login'
searchurl = site + '/modules.php?name=Downloads&file=jz&d_op=search&op=_jz00&query={query}'
language_list = list(languages)
def __init__(self, username, password):
self.username = username
self.password = password
def initialize(self):
self.session = Session()
self.login()
def terminate(self):
self.logout()
self.session.close()
def login(self):
logger.info('Logging in')
self.headers['Referer'] = self.site + '/index.php'
self.session.headers.update(self.headers.items())
res = self.session.get(self.loginpage)
bsoup = ParserBeautifulSoup(res.content, ['lxml'])
_allinputs = bsoup.findAll('input')
fields = {}
for field in _allinputs:
fields[field.get('name')] = field.get('value')
fields['username'] = self.username
fields['password'] = self.password
fields['autologin'] = 'on'
fields['viewonline'] = 'on'
self.headers['Referer'] = self.loginpage
self.session.headers.update(self.headers.items())
res = self.session.post(self.loginpage, fields)
try:
logger.debug('Got session id %s' %
self.session.cookies.get_dict()['PHPSESSID'])
except KeyError as e:
logger.error(repr(e))
logger.error("Didn't get session id, check your credentials")
return False
except Exception as e:
logger.error(repr(e))
logger.error('uncached error #legendasdivx #AA')
return False
return True
def logout(self):
# need to figure this out
return True
def query(self, video, language):
try:
logger.debug('Got session id %s' %
self.session.cookies.get_dict()['PHPSESSID'])
except Exception as e:
self.login()
return []
language_ids = '0'
if isinstance(language, (tuple, list, set)):
if len(language) == 1:
language_ids = ','.join(sorted(l.opensubtitles for l in language))
if language_ids == 'por':
language_ids = '&form_cat=28'
else:
language_ids = '&form_cat=29'
querytext = video.name
querytext = os.path.basename(querytext)
querytext, _ = os.path.splitext(querytext)
videoname = querytext
querytext = querytext.lower()
querytext = querytext.replace(
".", "+").replace("[", "").replace("]", "")
if language_ids != '0':
querytext = querytext + language_ids
self.headers['Referer'] = self.site + '/index.php'
self.session.headers.update(self.headers.items())
res = self.session.get(self.searchurl.format(query=querytext))
# form_cat=28 = br
# form_cat=29 = pt
if "A legenda não foi encontrada" in res.text:
logger.warning('%s not found', querytext)
return []
bsoup = ParserBeautifulSoup(res.content, ['html.parser'])
_allsubs = bsoup.findAll("div", {"class": "sub_box"})
subtitles = []
lang = Language.fromopensubtitles("pob")
for _subbox in _allsubs:
hits=0
for th in _subbox.findAll("th", {"class": "color2"}):
if th.string == 'Hits:':
hits = int(th.parent.find("td").string)
if th.string == 'Idioma:':
lang = th.parent.find("td").find ("img").get ('src')
if 'brazil' in lang:
lang = Language.fromopensubtitles('pob')
else:
lang = Language.fromopensubtitles('por')
description = _subbox.find("td", {"class": "td_desc brd_up"})
download = _subbox.find("a", {"class": "sub_download"})
try:
# sometimes BSoup just doesn't get the link
logger.debug(download.get('href'))
except Exception as e:
logger.warning('skipping subbox on %s' % self.searchurl.format(query=querytext))
continue
exact_match = False
if video.name.lower() in description.get_text().lower():
exact_match = True
data = {'link': self.site + '/modules.php' + download.get('href'),
'exact_match': exact_match,
'hits': hits,
'videoname': videoname,
'description': description.get_text() }
subtitles.append(
LegendasdivxSubtitle(lang, video, data)
)
return subtitles
def list_subtitles(self, video, languages):
return self.query(video, languages)
def download_subtitle(self, subtitle):
res = self.session.get(subtitle.page_link)
if res:
if res.text == '500':
raise ValueError('Error 500 on server')
archive = self._get_archive(res.content)
# extract the subtitle
subtitle_content = self._get_subtitle_from_archive(archive)
subtitle.content = fix_line_ending(subtitle_content)
subtitle.normalize()
return subtitle
raise ValueError('Problems conecting to the server')
def _get_archive(self, content):
# open the archive
# stole^H^H^H^H^H inspired from subvix provider
archive_stream = io.BytesIO(content)
if rarfile.is_rarfile(archive_stream):
logger.debug('Identified rar archive')
archive = rarfile.RarFile(archive_stream)
elif zipfile.is_zipfile(archive_stream):
logger.debug('Identified zip archive')
archive = zipfile.ZipFile(archive_stream)
else:
# raise ParseResponseError('Unsupported compressed format')
raise Exception('Unsupported compressed format')
return archive
def _get_subtitle_from_archive(self, archive):
# some files have a non subtitle with .txt extension
_tmp = list(SUBTITLE_EXTENSIONS)
_tmp.remove('.txt')
_subtitle_extensions = tuple(_tmp)
for name in archive.namelist():
# discard hidden files
if os.path.split(name)[-1].startswith('.'):
continue
# discard non-subtitle files
if not name.lower().endswith(_subtitle_extensions):
continue
logger.debug("returning from archive: %s" % name)
return archive.read(name)
raise ParseResponseError('Can not find the subtitle in the compressed file')

@ -35,6 +35,10 @@ class SubdivxSubtitle(Subtitle):
def id(self):
return self.page_link
@property
def release_info(self):
return self.description
def get_matches(self, video):
matches = set()

@ -84,7 +84,7 @@ class Subs4FreeProvider(Provider):
def initialize(self):
self.session = Session()
self.session.headers['User-Agent'] = 'Subliminal/{}'.format(__short_version__)
self.session.headers['User-Agent'] = os.environ.get("SZ_USER_AGENT", "Sub-Zero/2")
def terminate(self):
self.session.close()

@ -0,0 +1,229 @@
# coding=utf-8
from __future__ import absolute_import
import io
import logging
import re
from subliminal import __short_version__
import rarfile
from zipfile import ZipFile, is_zipfile
from rarfile import RarFile, is_rarfile
from subliminal_patch.providers import Provider
from subliminal_patch.providers.mixins import ProviderSubtitleArchiveMixin
from subliminal_patch.subtitle import Subtitle
from subliminal_patch.utils import sanitize, fix_inconsistent_naming as _fix_inconsistent_naming
from subliminal.exceptions import ProviderError
from subliminal.providers import ParserBeautifulSoup
from subliminal.video import Episode, Movie
from subzero.language import Language
# parsing regex definitions
title_re = re.compile(r'(?P<title>(?:.+(?= [Aa][Kk][Aa] ))|.+)(?:(?:.+)(?P<altitle>(?<= [Aa][Kk][Aa] ).+))?')
def fix_inconsistent_naming(title):
"""Fix titles with inconsistent naming using dictionary and sanitize them.
:param str title: original title.
:return: new title.
:rtype: str
"""
return _fix_inconsistent_naming(title, {"DC's Legends of Tomorrow": "Legends of Tomorrow",
"Marvel's Jessica Jones": "Jessica Jones"})
logger = logging.getLogger(__name__)
# Configure :mod:`rarfile` to use the same path separator as :mod:`zipfile`
rarfile.PATH_SEP = '/'
class TitrariSubtitle(Subtitle):
provider_name = 'titrari'
def __init__(self, language, download_link, sid, releases, title, imdb_id, year=None, download_count=None, comments=None):
super(TitrariSubtitle, self).__init__(language)
self.sid = sid
self.title = title
self.imdb_id = imdb_id
self.download_link = download_link
self.year = year
self.download_count = download_count
self.releases = self.release_info = releases
self.comments = comments
@property
def id(self):
return self.sid
def __str__(self):
return self.title + "(" + str(self.year) + ")" + " -> " + self.download_link
def __repr__(self):
return self.title + "(" + str(self.year) + ")"
def get_matches(self, video):
matches = set()
if isinstance(video, Movie):
# title
if video.title and sanitize(self.title) == fix_inconsistent_naming(video.title):
matches.add('title')
if video.year and self.year == video.year:
matches.add('year')
if video.imdb_id and self.imdb_id == video.imdb_id:
matches.add('imdb_id')
if video.release_group and video.release_group in self.comments:
matches.add('release_group')
if video.resolution and video.resolution.lower() in self.comments:
matches.add('resolution')
self.matches = matches
return matches
class TitrariProvider(Provider, ProviderSubtitleArchiveMixin):
subtitle_class = TitrariSubtitle
languages = {Language(l) for l in ['ron', 'eng']}
languages.update(set(Language.rebuild(l, forced=True) for l in languages))
api_url = 'https://www.titrari.ro/'
query_advanced_search = 'cautareavansata'
def __init__(self):
self.session = None
def initialize(self):
self.session = Session()
self.session.headers['User-Agent'] = 'Subliminal/{}'.format(__short_version__)
def terminate(self):
self.session.close()
def query(self, languages=None, title=None, imdb_id=None, video=None):
subtitles = []
params = self.getQueryParams(imdb_id, title)
search_response = self.session.get(self.api_url, params=params, timeout=15)
search_response.raise_for_status()
if not search_response.content:
logger.debug('[#### Provider: titrari.ro] No data returned from provider')
return []
soup = ParserBeautifulSoup(search_response.content.decode('utf-8', 'ignore'), ['lxml', 'html.parser'])
# loop over subtitle cells
rows = soup.select('td[rowspan=\'5\']')
for index, row in enumerate(rows):
result_anchor_el = row.select_one('a')
# Download link
href = result_anchor_el.get('href')
download_link = self.api_url + href
fullTitle = row.parent.find("h1").find("a").text
#Get title
try:
title = fullTitle.split("(")[0]
except:
logger.error("[#### Provider: titrari.ro] Error parsing title.")
# Get downloads count
try:
downloads = int(row.parent.parent.select("span")[index].text[12:])
except:
logger.error("[#### Provider: titrari.ro] Error parsing downloads.")
# Get year
try:
year = int(fullTitle.split("(")[1].split(")")[0])
except:
year = None
logger.error("[#### Provider: titrari.ro] Error parsing year.")
# Get imdbId
sub_imdb_id = self.getImdbIdFromSubtitle(row)
try:
comments = row.parent.parent.find_all("td", class_=re.compile("comment"))[index*2+1].text
except:
logger.error("Error parsing comments.")
subtitle = self.subtitle_class(next(iter(languages)), download_link, index, None, title, sub_imdb_id, year, downloads, comments)
logger.debug('[#### Provider: titrari.ro] Found subtitle %r', str(subtitle))
subtitles.append(subtitle)
ordered_subs = self.order(subtitles, video)
return ordered_subs
def order(self, subtitles, video):
logger.debug("[#### Provider: titrari.ro] Sorting by download count...")
sorted_subs = sorted(subtitles, key=lambda s: s.download_count, reverse=True)
return sorted_subs
def getImdbIdFromSubtitle(self, row):
try:
imdbId = row.parent.parent.find_all(src=re.compile("imdb"))[0].parent.get('href').split("tt")[-1]
except:
logger.error("[#### Provider: titrari.ro] Error parsing imdbId.")
if imdbId is not None:
return "tt" + imdbId
else:
return None
def getQueryParams(self, imdb_id, title):
queryParams = {
'page': self.query_advanced_search,
'z8': '1'
}
if imdb_id is not None:
queryParams["z5"] = imdb_id
elif title is not None:
queryParams["z7"] = title
return queryParams
def list_subtitles(self, video, languages):
title = fix_inconsistent_naming(video.title)
imdb_id = None
try:
imdb_id = video.imdb_id[2:]
except:
logger.error("[#### Provider: titrari.ro] Error parsing video.imdb_id.")
return [s for s in
self.query(languages, title, imdb_id, video)]
def download_subtitle(self, subtitle):
r = self.session.get(subtitle.download_link, headers={'Referer': self.api_url}, timeout=10)
r.raise_for_status()
# open the archive
archive_stream = io.BytesIO(r.content)
if is_rarfile(archive_stream):
logger.debug('[#### Provider: titrari.ro] Archive identified as rar')
archive = RarFile(archive_stream)
elif is_zipfile(archive_stream):
logger.debug('[#### Provider: titrari.ro] Archive identified as zip')
archive = ZipFile(archive_stream)
else:
subtitle.content = r.content
if subtitle.is_valid():
return
subtitle.content = None
raise ProviderError('[#### Provider: titrari.ro] Unidentified archive type')
subtitle.content = self.get_subtitle_from_archive(subtitle, archive)

@ -244,7 +244,7 @@ class TitulkyProvider(Provider):
for sub in subs:
page_link = '%s%s' % (self.server_url, sub.a.get('href').encode('utf-8'))
title = sub.find_all('td')[0:1]
title = [x.text.encode('utf-8') for x in title]
title = [x.text for x in title]
version = sub.find(class_="fixedTip")
if version is None:
version = ""
@ -316,13 +316,12 @@ class TitulkyProvider(Provider):
elif 'Limit vyčerpán' in r.text:
raise DownloadLimitExceeded
soup = ParserBeautifulSoup(r.text.decode('utf-8', 'ignore'), ['lxml', 'html.parser'])
soup = ParserBeautifulSoup(r.text, ['lxml', 'html.parser'])
# links = soup.find("a", {"id": "downlink"}).find_all('a')
link = soup.find(id="downlink")
# TODO: add settings for choice
url = link.get('href')
url = self.dn_url + url
url = self.dn_url + link.get('href')
time.sleep(0.5)
r = self.session.get(url, headers={'Referer': subtitle.download_link},
timeout=30)

@ -358,6 +358,15 @@ class ModifiedSubtitle(Subtitle):
id = None
MERGED_FORMATS = {
"TV": ("HDTV", "SDTV", "AHDTV", "UHDTV"),
"Air": ("SATRip", "DVB", "PPV"),
"Disk": ("DVD", "HD-DVD", "BluRay")
}
MERGED_FORMATS_REV = dict((v.lower(), k.lower()) for k in MERGED_FORMATS for v in MERGED_FORMATS[k])
def guess_matches(video, guess, partial=False):
"""Get matches between a `video` and a `guess`.
@ -386,12 +395,15 @@ def guess_matches(video, guess, partial=False):
for title in titles:
if sanitize(title) in (sanitize(name) for name in [video.series] + video.alternative_series):
matches.add('series')
# title
if video.title and 'episode_title' in guess and sanitize(guess['episode_title']) == sanitize(video.title):
matches.add('title')
# season
if video.season and 'season' in guess and guess['season'] == video.season:
matches.add('season')
# episode
# Currently we only have single-ep support (guessit returns a multi-ep as a list with int values)
# Most providers only support single-ep, so make sure it contains only 1 episode
@ -401,12 +413,15 @@ def guess_matches(video, guess, partial=False):
episode = min(episode_guess) if episode_guess and isinstance(episode_guess, list) else episode_guess
if episode == video.episode:
matches.add('episode')
# year
if video.year and 'year' in guess and guess['year'] == video.year:
matches.add('year')
# count "no year" as an information
if not partial and video.original_series and 'year' not in guess:
matches.add('year')
elif isinstance(video, Movie):
# year
if video.year and 'year' in guess and guess['year'] == video.year:
@ -440,21 +455,25 @@ def guess_matches(video, guess, partial=False):
formats = [formats]
if video.format:
video_format = video.format
if video_format in ("HDTV", "SDTV", "TV"):
video_format = "TV"
logger.debug("Treating HDTV/SDTV the same")
video_format = video.format.lower()
_video_gen_format = MERGED_FORMATS_REV.get(video_format)
if _video_gen_format:
logger.debug("Treating %s as %s the same", video_format, _video_gen_format)
for frmt in formats:
if frmt in ("HDTV", "SDTV"):
frmt = "TV"
_guess_gen_frmt = MERGED_FORMATS_REV.get(frmt.lower())
if frmt.lower() == video_format.lower():
if _guess_gen_frmt == _video_gen_format:
matches.add('format')
break
if "release_group" in matches and "format" not in matches:
logger.info("Release group matched but format didn't. Remnoving release group match.")
matches.remove("release_group")
# video_codec
if video.video_codec and 'video_codec' in guess and guess['video_codec'] == video.video_codec:
matches.add('video_codec')
# audio_codec
if video.audio_codec and 'audio_codec' in guess and guess['audio_codec'] == video.audio_codec:
matches.add('audio_codec')

@ -30,6 +30,7 @@ repl_map = {
"rum": "ro",
"slo": "sk",
"tib": "bo",
"ron": "ro",
}
ALPHA2_LIST = list(set(filter(lambda x: x, map(lambda x: x.alpha2, LANGUAGE_MATRIX)))) + list(repl_map.values())

@ -84,10 +84,11 @@ def _get_localzone(_root='/'):
if not etctz:
continue
tz = pytz.timezone(etctz.replace(' ', '_'))
if _root == '/':
# Disabling this offset valdation due to issue with some timezone: https://github.com/regebro/tzlocal/issues/80
# if _root == '/':
# We are using a file in etc to name the timezone.
# Verify that the timezone specified there is actually used:
utils.assert_tz_offset(tz)
# utils.assert_tz_offset(tz)
return tz
except IOError:

Loading…
Cancel
Save