2020-03-19 05:42:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import logging
|
2020-05-24 10:02:34 +00:00
|
|
|
import re
|
2020-03-19 05:42:08 +00:00
|
|
|
import io
|
|
|
|
import os
|
2020-05-24 10:02:34 +00:00
|
|
|
import codecs
|
2021-08-23 23:58:59 +00:00
|
|
|
import time
|
2020-05-24 10:02:34 +00:00
|
|
|
from hashlib import sha1
|
2020-03-19 05:42:08 +00:00
|
|
|
from random import randint
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from zipfile import ZipFile, is_zipfile
|
|
|
|
from rarfile import RarFile, is_rarfile
|
|
|
|
from requests import Session
|
|
|
|
from guessit import guessit
|
2020-05-24 10:02:34 +00:00
|
|
|
from dogpile.cache.api import NO_VALUE
|
2020-03-19 05:42:08 +00:00
|
|
|
from subliminal_patch.providers import Provider
|
2021-06-11 02:55:58 +00:00
|
|
|
from subliminal_patch.subtitle import Subtitle, guess_matches
|
2021-07-17 13:52:37 +00:00
|
|
|
from subliminal_patch.utils import sanitize, fix_inconsistent_naming
|
2020-03-19 05:42:08 +00:00
|
|
|
from subliminal.video import Episode, Movie
|
|
|
|
from subliminal.subtitle import fix_line_ending
|
2020-05-24 10:02:34 +00:00
|
|
|
from subliminal.cache import region
|
2020-03-19 05:42:08 +00:00
|
|
|
from subzero.language import Language
|
|
|
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-05-31 16:01:53 +00:00
|
|
|
|
2021-07-17 13:52:37 +00:00
|
|
|
def fix_tv_naming(title):
|
|
|
|
"""Fix TV show titles with inconsistent naming using dictionary, but do not sanitize them.
|
|
|
|
:param str title: original title.
|
|
|
|
:return: new title.
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
return fix_inconsistent_naming(title, {"Superman & Lois": "Superman and Lois",
|
|
|
|
}, True)
|
|
|
|
|
|
|
|
|
|
|
|
def fix_movie_naming(title):
|
|
|
|
return fix_inconsistent_naming(title, {
|
2023-06-12 19:24:40 +00:00
|
|
|
}, True)
|
2021-07-17 13:52:37 +00:00
|
|
|
|
|
|
|
|
2020-03-19 05:42:08 +00:00
|
|
|
class YavkaNetSubtitle(Subtitle):
|
|
|
|
"""YavkaNet Subtitle."""
|
|
|
|
provider_name = 'yavkanet'
|
|
|
|
|
2023-06-12 19:24:40 +00:00
|
|
|
def __init__(self, language, filename, type, video, link, fps, subs_id_name, subs_id_value):
|
2020-05-31 16:01:53 +00:00
|
|
|
super(YavkaNetSubtitle, self).__init__(language)
|
2020-03-19 05:42:08 +00:00
|
|
|
self.filename = filename
|
|
|
|
self.page_link = link
|
|
|
|
self.type = type
|
|
|
|
self.video = video
|
2020-04-25 17:04:43 +00:00
|
|
|
self.fps = fps
|
2020-08-16 08:01:21 +00:00
|
|
|
self.release_info = filename
|
2023-06-12 19:24:40 +00:00
|
|
|
self.subs_id_name = subs_id_name
|
|
|
|
self.subs_id_value = subs_id_value
|
2021-08-23 23:58:59 +00:00
|
|
|
self.content = None
|
|
|
|
self._is_valid = False
|
2020-08-16 08:01:21 +00:00
|
|
|
if fps:
|
|
|
|
if video.fps and float(video.fps) == fps:
|
2021-04-18 14:19:16 +00:00
|
|
|
self.release_info += " [{:.3f}]".format(fps)
|
2020-08-16 08:01:21 +00:00
|
|
|
else:
|
|
|
|
self.release_info += " [{:.3f}]".format(fps)
|
2020-03-19 05:42:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2020-04-25 17:04:43 +00:00
|
|
|
return self.page_link + self.filename
|
|
|
|
|
|
|
|
def get_fps(self):
|
|
|
|
return self.fps
|
2020-03-19 05:42:08 +00:00
|
|
|
|
|
|
|
def make_picklable(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
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)
|
2020-05-24 10:02:34 +00:00
|
|
|
video_filename = re.sub(r'\[\w+\]$', '', video_filename).strip().upper()
|
2020-03-19 05:42:08 +00:00
|
|
|
|
|
|
|
subtitle_filename = self.filename
|
|
|
|
subtitle_filename = os.path.basename(subtitle_filename)
|
|
|
|
subtitle_filename, _ = os.path.splitext(subtitle_filename)
|
2020-05-24 10:02:34 +00:00
|
|
|
subtitle_filename = re.sub(r'\[\w+\]$', '', subtitle_filename).strip().upper()
|
2020-03-19 05:42:08 +00:00
|
|
|
|
2020-05-24 10:02:34 +00:00
|
|
|
if ((video_filename == subtitle_filename) or
|
2023-06-12 19:24:40 +00:00
|
|
|
(self.single_file is True and video_filename in self.notes.upper())):
|
2020-05-31 16:01:53 +00:00
|
|
|
matches.add('hash')
|
2020-03-19 05:42:08 +00:00
|
|
|
|
2020-04-25 17:04:43 +00:00
|
|
|
if video.year and self.year == video.year:
|
|
|
|
matches.add('year')
|
|
|
|
|
2020-05-24 10:02:34 +00:00
|
|
|
matches |= guess_matches(video, guessit(self.title, {'type': self.type}))
|
|
|
|
matches |= guess_matches(video, guessit(self.filename, {'type': self.type}))
|
2020-03-19 05:42:08 +00:00
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class YavkaNetProvider(Provider):
|
|
|
|
"""YavkaNet Provider."""
|
|
|
|
languages = {Language(l) for l in [
|
|
|
|
'bul', 'eng', 'rus', 'spa', 'ita'
|
|
|
|
]}
|
2021-11-10 03:55:47 +00:00
|
|
|
video_types = (Episode, Movie)
|
2020-03-19 05:42:08 +00:00
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
self.session = Session()
|
|
|
|
self.session.headers['User-Agent'] = AGENT_LIST[randint(0, len(AGENT_LIST) - 1)]
|
|
|
|
self.session.headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
|
|
self.session.headers["Accept-Language"] = "en-US,en;q=0.5"
|
|
|
|
self.session.headers["Accept-Encoding"] = "gzip, deflate, br"
|
|
|
|
self.session.headers["DNT"] = "1"
|
|
|
|
self.session.headers["Connection"] = "keep-alive"
|
|
|
|
self.session.headers["Upgrade-Insecure-Requests"] = "1"
|
|
|
|
self.session.headers["Cache-Control"] = "max-age=0"
|
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
self.session.close()
|
|
|
|
|
|
|
|
def query(self, language, video):
|
|
|
|
subtitles = []
|
|
|
|
isEpisode = isinstance(video, Episode)
|
|
|
|
|
|
|
|
if isEpisode:
|
2023-08-12 13:57:48 +00:00
|
|
|
imdb_id = video.series_imdb_id
|
2020-03-19 05:42:08 +00:00
|
|
|
else:
|
2023-08-12 13:57:48 +00:00
|
|
|
imdb_id = video.imdb_id
|
|
|
|
|
|
|
|
logger.info(f'Searching subtitle for {imdb_id}')
|
|
|
|
response = self.retry(self.session.get(f'https://yavka.net/imdb/{imdb_id}', timeout=10,
|
|
|
|
headers={'Referer': 'https://yavka.net/'}))
|
2021-08-23 23:58:59 +00:00
|
|
|
if not response:
|
|
|
|
return subtitles
|
2020-03-19 05:42:08 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
logger.debug('No subtitles found')
|
|
|
|
return subtitles
|
|
|
|
|
2020-03-22 07:58:31 +00:00
|
|
|
soup = BeautifulSoup(response.content, 'lxml')
|
2020-04-25 17:04:43 +00:00
|
|
|
rows = soup.findAll('tr')
|
2023-06-12 19:24:40 +00:00
|
|
|
|
2020-04-25 17:04:43 +00:00
|
|
|
# Search on first 25 rows only
|
|
|
|
for row in rows[:25]:
|
2021-01-25 14:48:08 +00:00
|
|
|
element = row.select_one('a.balon, a.selector')
|
2020-03-19 05:42:08 +00:00
|
|
|
if element:
|
|
|
|
link = element.get('href')
|
2020-08-16 08:01:21 +00:00
|
|
|
notes = re.sub(r'(?s)<p.*><img [A-z0-9=\'/\. :;#]*>(.*)</p>', r"\1", element.get('content'))
|
2020-04-25 17:04:43 +00:00
|
|
|
title = element.get_text()
|
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(element.find_next_sibling('span').text.strip('()'))
|
|
|
|
except:
|
|
|
|
year = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
fps = float(row.find('span', {'title': 'Кадри в секунда'}).text.strip())
|
|
|
|
except:
|
|
|
|
fps = None
|
|
|
|
|
2020-03-22 07:58:31 +00:00
|
|
|
element = row.find('a', {'class': 'click'})
|
|
|
|
uploader = element.get_text() if element else None
|
2020-03-19 05:42:08 +00:00
|
|
|
logger.info('Found subtitle link %r', link)
|
2021-08-23 23:58:59 +00:00
|
|
|
# slow down to prevent being throttled
|
|
|
|
time.sleep(1)
|
|
|
|
response = self.retry(self.session.get('https://yavka.net' + link))
|
|
|
|
if not response:
|
|
|
|
continue
|
|
|
|
soup = BeautifulSoup(response.content, 'lxml')
|
2023-06-12 19:24:40 +00:00
|
|
|
subs_id = soup.find("input")
|
2021-08-23 23:58:59 +00:00
|
|
|
if subs_id:
|
2023-06-12 19:24:40 +00:00
|
|
|
subs_id_name = subs_id['name']
|
|
|
|
subs_id_value = subs_id['value']
|
2021-08-23 23:58:59 +00:00
|
|
|
else:
|
|
|
|
continue
|
|
|
|
sub = self.download_archive_and_add_subtitle_files('https://yavka.net' + link + '/', language, video,
|
2023-06-12 19:24:40 +00:00
|
|
|
fps, subs_id_name, subs_id_value)
|
2020-04-25 17:04:43 +00:00
|
|
|
for s in sub:
|
|
|
|
s.title = title
|
|
|
|
s.notes = notes
|
|
|
|
s.year = year
|
2020-03-22 07:58:31 +00:00
|
|
|
s.uploader = uploader
|
2020-05-24 10:02:34 +00:00
|
|
|
s.single_file = True if len(sub) == 1 else False
|
2020-03-28 08:10:24 +00:00
|
|
|
subtitles = subtitles + sub
|
2020-03-19 05:42:08 +00:00
|
|
|
return subtitles
|
2023-06-12 19:24:40 +00:00
|
|
|
|
2020-03-19 05:42:08 +00:00
|
|
|
def list_subtitles(self, video, languages):
|
2021-08-23 23:58:59 +00:00
|
|
|
return [s for lang in languages for s in self.query(lang, video)]
|
2020-03-19 05:42:08 +00:00
|
|
|
|
|
|
|
def download_subtitle(self, subtitle):
|
|
|
|
if subtitle.content:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
seeking_subtitle_file = subtitle.filename
|
2020-04-25 17:04:43 +00:00
|
|
|
arch = self.download_archive_and_add_subtitle_files(subtitle.page_link, subtitle.language, subtitle.video,
|
2023-06-12 19:24:40 +00:00
|
|
|
subtitle.fps, subtitle.subs_id_name,
|
|
|
|
subtitle.subs_id_value)
|
2020-03-19 05:42:08 +00:00
|
|
|
for s in arch:
|
|
|
|
if s.filename == seeking_subtitle_file:
|
|
|
|
subtitle.content = s.content
|
|
|
|
|
2021-08-23 23:58:59 +00:00
|
|
|
@staticmethod
|
2023-06-12 19:24:40 +00:00
|
|
|
def process_archive_subtitle_files(archive_stream, language, video, link, fps, subs_id_name, subs_id_value):
|
2020-03-19 05:42:08 +00:00
|
|
|
subtitles = []
|
2021-08-23 23:58:59 +00:00
|
|
|
media_type = 'episode' if isinstance(video, Episode) else 'movie'
|
|
|
|
for file_name in archive_stream.namelist():
|
2020-03-19 05:42:08 +00:00
|
|
|
if file_name.lower().endswith(('.srt', '.sub')):
|
|
|
|
logger.info('Found subtitle file %r', file_name)
|
2023-06-12 19:24:40 +00:00
|
|
|
subtitle = YavkaNetSubtitle(language, file_name, media_type, video, link, fps, subs_id_name,
|
|
|
|
subs_id_value)
|
2021-08-23 23:58:59 +00:00
|
|
|
subtitle.content = fix_line_ending(archive_stream.read(file_name))
|
2020-03-19 05:42:08 +00:00
|
|
|
subtitles.append(subtitle)
|
|
|
|
return subtitles
|
|
|
|
|
2023-06-12 19:24:40 +00:00
|
|
|
def download_archive_and_add_subtitle_files(self, link, language, video, fps, subs_id_name, subs_id_value):
|
2020-03-19 05:42:08 +00:00
|
|
|
logger.info('Downloading subtitle %r', link)
|
2020-05-24 10:02:34 +00:00
|
|
|
cache_key = sha1(link.encode("utf-8")).digest()
|
|
|
|
request = region.get(cache_key)
|
|
|
|
if request is NO_VALUE:
|
2021-08-23 23:58:59 +00:00
|
|
|
time.sleep(1)
|
|
|
|
request = self.retry(self.session.post(link, data={
|
2023-06-12 19:24:40 +00:00
|
|
|
subs_id_name: subs_id_value,
|
2021-08-23 23:58:59 +00:00
|
|
|
'lng': language.basename.upper()
|
|
|
|
}, headers={
|
2021-04-18 14:19:16 +00:00
|
|
|
'referer': link
|
2021-08-23 23:58:59 +00:00
|
|
|
}, allow_redirects=False))
|
|
|
|
if not request:
|
|
|
|
return []
|
2020-05-24 10:02:34 +00:00
|
|
|
request.raise_for_status()
|
|
|
|
region.set(cache_key, request)
|
|
|
|
else:
|
|
|
|
logger.info('Cache file: %s', codecs.encode(cache_key, 'hex_codec').decode('utf-8'))
|
2020-03-19 05:42:08 +00:00
|
|
|
|
2020-08-16 08:01:21 +00:00
|
|
|
try:
|
|
|
|
archive_stream = io.BytesIO(request.content)
|
|
|
|
if is_rarfile(archive_stream):
|
2023-06-12 19:24:40 +00:00
|
|
|
return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps,
|
|
|
|
subs_id_name, subs_id_value)
|
2020-08-16 08:01:21 +00:00
|
|
|
elif is_zipfile(archive_stream):
|
2023-06-12 19:24:40 +00:00
|
|
|
return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps,
|
|
|
|
subs_id_name, subs_id_value)
|
2020-08-16 08:01:21 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
logger.error('Ignore unsupported archive %r', request.headers)
|
|
|
|
region.delete(cache_key)
|
|
|
|
return []
|
2021-08-23 23:58:59 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def retry(func, limit=5, delay=5):
|
|
|
|
for i in range(limit):
|
|
|
|
response = func
|
|
|
|
if response.content:
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
logging.debug('Slowing down because we are getting throttled. Iteration {0} of {1}.Waiting {2} seconds '
|
|
|
|
'to retry...'.format(i + 1, limit, delay))
|
|
|
|
time.sleep(delay)
|