You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
331 lines
11 KiB
331 lines
11 KiB
5 years ago
|
# coding=utf-8
|
||
|
|
||
|
from __future__ import absolute_import
|
||
3 years ago
|
import os
|
||
5 years ago
|
import io
|
||
|
import logging
|
||
|
import re
|
||
|
import rarfile
|
||
4 years ago
|
from random import randint
|
||
5 years ago
|
|
||
|
from zipfile import ZipFile, is_zipfile
|
||
|
from rarfile import RarFile, is_rarfile
|
||
4 years ago
|
from guessit import guessit
|
||
5 years ago
|
from subliminal_patch.providers import Provider
|
||
|
from subliminal_patch.providers.mixins import ProviderSubtitleArchiveMixin
|
||
4 years ago
|
from subliminal_patch.subtitle import Subtitle, guess_matches
|
||
5 years ago
|
from subliminal_patch.utils import sanitize, fix_inconsistent_naming as _fix_inconsistent_naming
|
||
4 years ago
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
||
5 years ago
|
from subliminal.exceptions import ProviderError
|
||
|
from subliminal.providers import ParserBeautifulSoup
|
||
|
from subliminal.video import Episode, Movie
|
||
3 years ago
|
from subliminal.subtitle import SUBTITLE_EXTENSIONS
|
||
5 years ago
|
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__)
|
||
|
|
||
3 years ago
|
|
||
5 years ago
|
class TitrariSubtitle(Subtitle):
|
||
|
|
||
|
provider_name = 'titrari'
|
||
|
|
||
3 years ago
|
def __init__(self, language, download_link, sid, comments, title, imdb_id, page_link, uploader, year=None,
|
||
|
download_count=None, is_episode=False, desired_episode=None):
|
||
5 years ago
|
super(TitrariSubtitle, self).__init__(language)
|
||
|
self.sid = sid
|
||
|
self.title = title
|
||
|
self.imdb_id = imdb_id
|
||
|
self.download_link = download_link
|
||
3 years ago
|
self.page_link = page_link
|
||
|
self.uploader = uploader
|
||
5 years ago
|
self.year = year
|
||
|
self.download_count = download_count
|
||
3 years ago
|
self.comments = self.releases = self.release_info = comments
|
||
3 years ago
|
self.matches = None
|
||
|
self.is_episode = is_episode
|
||
|
self.desired_episode = desired_episode
|
||
5 years ago
|
|
||
|
@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()
|
||
|
|
||
3 years ago
|
if video.year and self.year == video.year:
|
||
|
matches.add('year')
|
||
|
|
||
|
if video.release_group and video.release_group in self.comments:
|
||
|
matches.add('release_group')
|
||
|
|
||
5 years ago
|
if isinstance(video, Movie):
|
||
|
# title
|
||
|
if video.title and sanitize(self.title) == fix_inconsistent_naming(video.title):
|
||
|
matches.add('title')
|
||
|
|
||
3 years ago
|
# imdb
|
||
5 years ago
|
if video.imdb_id and self.imdb_id == video.imdb_id:
|
||
|
matches.add('imdb_id')
|
||
|
|
||
3 years ago
|
# guess match others
|
||
4 years ago
|
matches |= guess_matches(video, guessit(self.comments, {"type": "movie"}))
|
||
3 years ago
|
|
||
3 years ago
|
else:
|
||
|
# title
|
||
3 years ago
|
seasonless_title = re.sub(r'\s-\sSezonul\s\d+$', '', self.title.rstrip())
|
||
|
if video.series and fix_inconsistent_naming(video.series) == sanitize(seasonless_title):
|
||
3 years ago
|
matches.add('series')
|
||
|
|
||
3 years ago
|
# imdb
|
||
3 years ago
|
if video.series_imdb_id and self.imdb_id == video.series_imdb_id:
|
||
|
matches.add('imdb_id')
|
||
|
|
||
3 years ago
|
# season
|
||
3 years ago
|
if f"Sezonul {video.season}" in self.title:
|
||
|
matches.add('season')
|
||
|
|
||
3 years ago
|
# episode
|
||
|
if {"imdb_id", "season"}.issubset(matches):
|
||
|
matches.add('episode')
|
||
|
|
||
|
# guess match others
|
||
3 years ago
|
matches |= guess_matches(video, guessit(self.comments, {"type": "episode"}))
|
||
5 years ago
|
|
||
|
self.matches = matches
|
||
|
|
||
|
return matches
|
||
|
|
||
|
|
||
|
class TitrariProvider(Provider, ProviderSubtitleArchiveMixin):
|
||
|
subtitle_class = TitrariSubtitle
|
||
3 years ago
|
languages = {Language(lang) for lang in ['ron', 'eng']}
|
||
|
languages.update(set(Language.rebuild(lang, forced=True) for lang in languages))
|
||
2 years ago
|
languages.update(set(Language.rebuild(lang, hi=True) for lang in languages))
|
||
3 years ago
|
video_types = (Episode, Movie)
|
||
5 years ago
|
api_url = 'https://www.titrari.ro/'
|
||
2 years ago
|
# query_advanced_search = 'cautarepreaavansata'
|
||
9 months ago
|
query_advanced_search = "maicauta"
|
||
5 years ago
|
|
||
|
def __init__(self):
|
||
|
self.session = None
|
||
|
|
||
|
def initialize(self):
|
||
|
self.session = Session()
|
||
3 years ago
|
# Hardcoding the UA to bypass the 30s throttle that titrari.ro uses for IP/UA pair
|
||
3 years ago
|
self.session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, ' \
|
||
|
'like Gecko) Chrome/93.0.4535.2 Safari/537.36'
|
||
3 years ago
|
# self.session.headers['User-Agent'] = AGENT_LIST[randint(0, len(AGENT_LIST) - 1)]
|
||
5 years ago
|
|
||
|
def terminate(self):
|
||
|
self.session.close()
|
||
|
|
||
3 years ago
|
def query(self, language=None, title=None, imdb_id=None, video=None):
|
||
5 years ago
|
subtitles = []
|
||
|
|
||
3 years ago
|
params = self.getQueryParams(imdb_id, title, language)
|
||
5 years ago
|
|
||
|
search_response = self.session.get(self.api_url, params=params, timeout=15)
|
||
|
search_response.raise_for_status()
|
||
|
|
||
|
if not search_response.content:
|
||
3 years ago
|
logger.debug('No data returned from provider')
|
||
5 years ago
|
return []
|
||
|
|
||
|
soup = ParserBeautifulSoup(search_response.content.decode('utf-8', 'ignore'), ['lxml', 'html.parser'])
|
||
|
|
||
|
# loop over subtitle cells
|
||
3 years ago
|
rows = soup.select('td[rowspan="5"]')
|
||
5 years ago
|
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
|
||
|
|
||
3 years ago
|
fullTitle = row.parent.select('h1 a')[0].text
|
||
5 years ago
|
|
||
3 years ago
|
# Get title
|
||
5 years ago
|
try:
|
||
|
title = fullTitle.split("(")[0]
|
||
|
except:
|
||
3 years ago
|
logger.error("Error parsing title")
|
||
5 years ago
|
|
||
|
# Get downloads count
|
||
3 years ago
|
downloads = 0
|
||
5 years ago
|
try:
|
||
3 years ago
|
downloads = int(row.parent.parent.select('span')[index].text[12:])
|
||
5 years ago
|
except:
|
||
3 years ago
|
logger.error("Error parsing downloads")
|
||
5 years ago
|
|
||
|
# Get year
|
||
|
try:
|
||
|
year = int(fullTitle.split("(")[1].split(")")[0])
|
||
|
except:
|
||
|
year = None
|
||
3 years ago
|
logger.error("Error parsing year")
|
||
5 years ago
|
|
||
|
# Get imdbId
|
||
|
sub_imdb_id = self.getImdbIdFromSubtitle(row)
|
||
|
|
||
3 years ago
|
comments = ''
|
||
5 years ago
|
try:
|
||
3 years ago
|
comments = row.parent.parent.select('.comment')[1].text
|
||
5 years ago
|
except:
|
||
3 years ago
|
logger.error("Error parsing comments")
|
||
|
|
||
|
# Get page_link
|
||
|
try:
|
||
|
page_link = self.api_url + row.parent.select('h1 a')[0].get('href')
|
||
|
except:
|
||
|
logger.error("Error parsing page_link")
|
||
|
|
||
|
# Get uploader
|
||
|
try:
|
||
|
uploader = row.parent.select('td.row1.stanga a')[-1].text
|
||
|
except:
|
||
|
logger.error("Error parsing uploader")
|
||
5 years ago
|
|
||
3 years ago
|
episode_number = video.episode if isinstance(video, Episode) else None
|
||
3 years ago
|
subtitle = self.subtitle_class(language, download_link, index, comments, title, sub_imdb_id, page_link, uploader,
|
||
3 years ago
|
year, downloads, isinstance(video, Episode), episode_number)
|
||
3 years ago
|
logger.debug('Found subtitle %r', str(subtitle))
|
||
5 years ago
|
subtitles.append(subtitle)
|
||
|
|
||
3 years ago
|
ordered_subs = self.order(subtitles)
|
||
5 years ago
|
|
||
|
return ordered_subs
|
||
|
|
||
3 years ago
|
@staticmethod
|
||
|
def order(subtitles):
|
||
3 years ago
|
logger.debug("Sorting by download count...")
|
||
5 years ago
|
sorted_subs = sorted(subtitles, key=lambda s: s.download_count, reverse=True)
|
||
|
return sorted_subs
|
||
|
|
||
3 years ago
|
@staticmethod
|
||
|
def getImdbIdFromSubtitle(row):
|
||
|
imdbId = None
|
||
5 years ago
|
try:
|
||
|
imdbId = row.parent.parent.find_all(src=re.compile("imdb"))[0].parent.get('href').split("tt")[-1]
|
||
|
except:
|
||
3 years ago
|
logger.error("Error parsing imdb id")
|
||
5 years ago
|
if imdbId is not None:
|
||
|
return "tt" + imdbId
|
||
|
else:
|
||
|
return None
|
||
|
|
||
3 years ago
|
# titrari.ro seems to require all parameters now
|
||
|
# z2 = comment (empty)
|
||
|
# z3 = fps (-1: any, 0: N/A, 1: 23.97 FPS etc.)
|
||
|
# z4 = CD count (-1: any)
|
||
|
# z5 = imdb_id (empty or integer)
|
||
|
# z6 = sort order (0: unsorted, 1: by date, 2: by name)
|
||
|
# z7 = title (empty or string)
|
||
|
# z8 = language (-1: all, 1: ron, 2: eng)
|
||
|
# z9 = genre (All: all, Action: action etc.)
|
||
|
# z11 = type (0: any, 1: movie, 2: series)
|
||
3 years ago
|
def getQueryParams(self, imdb_id, title, language):
|
||
5 years ago
|
queryParams = {
|
||
|
'page': self.query_advanced_search,
|
||
3 years ago
|
'z7': '',
|
||
|
'z2': '',
|
||
|
'z5': '',
|
||
|
'z3': '-1',
|
||
|
'z4': '-1',
|
||
|
'z8': '-1',
|
||
|
'z9': 'All',
|
||
|
'z11': '0',
|
||
|
'z6': '0'
|
||
5 years ago
|
}
|
||
|
if imdb_id is not None:
|
||
|
queryParams["z5"] = imdb_id
|
||
|
elif title is not None:
|
||
|
queryParams["z7"] = title
|
||
|
|
||
3 years ago
|
if language == 'ro':
|
||
|
queryParams["z8"] = '1'
|
||
|
elif language == 'en':
|
||
|
queryParams["z8"] = '2'
|
||
|
|
||
5 years ago
|
return queryParams
|
||
|
|
||
|
def list_subtitles(self, video, languages):
|
||
|
title = fix_inconsistent_naming(video.title)
|
||
5 years ago
|
imdb_id = None
|
||
|
try:
|
||
3 years ago
|
if isinstance(video, Episode):
|
||
|
imdb_id = video.series_imdb_id[2:]
|
||
|
else:
|
||
|
imdb_id = video.imdb_id[2:]
|
||
5 years ago
|
except:
|
||
3 years ago
|
logger.error('Error parsing imdb_id from video object {}'.format(str(video)))
|
||
5 years ago
|
|
||
3 years ago
|
subtitles = [s for lang in languages for s in
|
||
|
self.query(lang, title, imdb_id, video)]
|
||
3 years ago
|
return subtitles
|
||
5 years ago
|
|
||
|
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):
|
||
3 years ago
|
logger.debug('Archive identified as RAR')
|
||
5 years ago
|
archive = RarFile(archive_stream)
|
||
|
elif is_zipfile(archive_stream):
|
||
3 years ago
|
logger.debug('Archive identified as ZIP')
|
||
5 years ago
|
archive = ZipFile(archive_stream)
|
||
|
else:
|
||
|
subtitle.content = r.content
|
||
|
if subtitle.is_valid():
|
||
|
return
|
||
|
subtitle.content = None
|
||
|
|
||
3 years ago
|
raise ProviderError('Unidentified archive type')
|
||
5 years ago
|
|
||
3 years ago
|
if subtitle.is_episode:
|
||
|
subtitle.content = self._get_subtitle_from_archive(subtitle, archive)
|
||
|
else:
|
||
|
subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
|
||
|
|
||
3 years ago
|
@staticmethod
|
||
|
def _get_subtitle_from_archive(subtitle, archive):
|
||
3 years ago
|
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
|
||
|
|
||
|
_guess = guessit(name)
|
||
|
if subtitle.desired_episode == _guess['episode']:
|
||
|
return archive.read(name)
|
||
|
|
||
|
return None
|
||
3 years ago
|
|
||
|
# vim: set expandtab ts=4 sw=4:
|