2019-04-07 23:31:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-09-17 02:04:27 +00:00
|
|
|
from __future__ import absolute_import
|
2019-04-07 23:31:39 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import io
|
|
|
|
import os
|
2020-05-24 10:02:34 +00:00
|
|
|
import codecs
|
|
|
|
from hashlib import sha1
|
2019-04-07 23:31:39 +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
|
2019-04-07 23:31:39 +00:00
|
|
|
from subliminal_patch.providers import Provider
|
|
|
|
from subliminal_patch.subtitle import Subtitle
|
2020-03-20 07:56:18 +00:00
|
|
|
from subliminal_patch.utils import sanitize, fix_inconsistent_naming
|
2019-04-07 23:31:39 +00:00
|
|
|
from subliminal.subtitle import guess_matches
|
|
|
|
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
|
2019-04-07 23:31:39 +00:00
|
|
|
from subzero.language import Language
|
2020-05-26 17:31:32 +00:00
|
|
|
from py7zr import is_7zfile, SevenZipFile
|
2019-04-07 23:31:39 +00:00
|
|
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-05-26 17:31:32 +00:00
|
|
|
|
2020-03-20 07:56:18 +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, {"Marvel's Daredevil": "Daredevil",
|
|
|
|
"Marvel's Luke Cage": "Luke Cage",
|
|
|
|
"Marvel's Iron Fist": "Iron Fist",
|
2020-04-21 20:16:15 +00:00
|
|
|
"DC's Legends of Tomorrow": "Legends of Tomorrow",
|
|
|
|
"Doctor Who (2005)": "Doctor Who",
|
2021-01-03 15:57:25 +00:00
|
|
|
"Star Trek: Deep Space Nine": "Star Trek DS9",
|
|
|
|
"Star Trek: The Next Generation": "Star Trek TNG",
|
2020-03-20 07:56:18 +00:00
|
|
|
}, True)
|
|
|
|
|
2020-05-26 17:31:32 +00:00
|
|
|
|
2020-09-13 11:24:49 +00:00
|
|
|
def fix_movie_naming(title):
|
|
|
|
return fix_inconsistent_naming(title, {"Back to the Future Part III": "Back to the Future 3",
|
|
|
|
"Back to the Future Part II": "Back to the Future 2",
|
2021-01-03 15:57:25 +00:00
|
|
|
'Bill & Ted Face the Music': 'Bill Ted Face the Music',
|
2020-09-13 11:24:49 +00:00
|
|
|
}, True)
|
|
|
|
|
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
class SubsUnacsSubtitle(Subtitle):
|
|
|
|
"""SubsUnacs Subtitle."""
|
|
|
|
provider_name = 'subsunacs'
|
|
|
|
|
2020-05-26 17:31:32 +00:00
|
|
|
def __init__(self, language, filename, type, video, link, fps, num_cds):
|
|
|
|
super(SubsUnacsSubtitle, self).__init__(language)
|
2019-04-07 23:31:39 +00:00
|
|
|
self.filename = filename
|
2019-04-08 17:58:13 +00:00
|
|
|
self.page_link = link
|
2019-04-07 23:31:39 +00:00
|
|
|
self.type = type
|
2019-04-08 17:58:13 +00:00
|
|
|
self.video = video
|
2020-04-26 02:08:09 +00:00
|
|
|
self.fps = fps
|
|
|
|
self.num_cds = num_cds
|
2020-08-16 08:01:21 +00:00
|
|
|
self.release_info = filename
|
|
|
|
if fps:
|
|
|
|
if video.fps and float(video.fps) == fps:
|
|
|
|
self.release_info += " <b>[{:.3f}]</b>".format(fps)
|
|
|
|
else:
|
|
|
|
self.release_info += " [{:.3f}]".format(fps)
|
2019-04-07 23:31:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2020-04-26 02:08:09 +00:00
|
|
|
return self.page_link + self.filename
|
|
|
|
|
|
|
|
def get_fps(self):
|
|
|
|
return self.fps
|
2019-04-07 23:31:39 +00:00
|
|
|
|
2019-04-08 17:58:13 +00:00
|
|
|
def make_picklable(self):
|
|
|
|
self.content = None
|
2020-05-24 10:02:34 +00:00
|
|
|
self._is_valid = False
|
2019-04-08 17:58:13 +00:00
|
|
|
return self
|
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
def get_matches(self, video):
|
|
|
|
matches = set()
|
2019-04-08 17:58:13 +00:00
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
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()
|
2019-04-07 23:31:39 +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()
|
2019-04-07 23:31:39 +00:00
|
|
|
|
2020-05-24 10:02:34 +00:00
|
|
|
if ((video_filename == subtitle_filename) or
|
|
|
|
(self.single_file is True and video_filename in self.notes.upper())):
|
2020-05-26 17:31:32 +00:00
|
|
|
matches.add('hash')
|
2019-04-07 23:31:39 +00:00
|
|
|
|
2020-04-26 02:08:09 +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}))
|
2020-05-31 15:07:59 +00:00
|
|
|
|
|
|
|
guess_filename = guessit(self.filename, video.hints)
|
|
|
|
matches |= guess_matches(video, guess_filename)
|
|
|
|
|
|
|
|
if isinstance(video, Movie) and (self.num_cds > 1 or 'cd' in guess_filename):
|
|
|
|
# reduce score of subtitles for multi-disc movie releases
|
|
|
|
return set()
|
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class SubsUnacsProvider(Provider):
|
|
|
|
"""SubsUnacs Provider."""
|
2020-05-01 15:20:04 +00:00
|
|
|
languages = {Language(l) for l in [
|
2019-04-07 23:31:39 +00:00
|
|
|
'bul', 'eng'
|
|
|
|
]}
|
|
|
|
|
|
|
|
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()
|
2019-04-08 17:58:13 +00:00
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
def query(self, language, video):
|
|
|
|
subtitles = []
|
|
|
|
isEpisode = isinstance(video, Episode)
|
2019-04-08 17:58:13 +00:00
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
params = {
|
|
|
|
'm': '',
|
|
|
|
'l': 0,
|
|
|
|
'c': '',
|
|
|
|
'y': '',
|
|
|
|
'action': " Търси ",
|
|
|
|
'a': '',
|
|
|
|
'd': '',
|
|
|
|
'u': '',
|
|
|
|
'g': '',
|
|
|
|
't': '',
|
|
|
|
'imdbcheck': 1}
|
|
|
|
|
|
|
|
if isEpisode:
|
2020-03-20 07:56:18 +00:00
|
|
|
params['m'] = "%s %02d %02d" % (sanitize(fix_tv_naming(video.series), {'\''}), video.season, video.episode)
|
2019-04-07 23:31:39 +00:00
|
|
|
else:
|
|
|
|
params['y'] = video.year
|
2020-09-13 11:24:49 +00:00
|
|
|
params['m'] = sanitize(fix_movie_naming(video.title), {'\''})
|
2019-04-07 23:31:39 +00:00
|
|
|
|
|
|
|
if language == 'en' or language == 'eng':
|
|
|
|
params['l'] = 1
|
|
|
|
|
|
|
|
logger.info('Searching subtitle %r', params)
|
|
|
|
response = self.session.post('https://subsunacs.net/search.php', params=params, allow_redirects=False, timeout=10, headers={
|
|
|
|
'Referer': 'https://subsunacs.net/index.php',
|
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
|
|
|
rows = soup.findAll('tr', onmouseover=True)
|
2019-04-08 17:58:13 +00:00
|
|
|
|
2020-03-20 07:56:18 +00:00
|
|
|
# Search on first 20 rows only
|
|
|
|
for row in rows[:20]:
|
2020-03-22 07:58:31 +00:00
|
|
|
a_element_wrapper = row.find('td', {'class': 'tdMovie'})
|
|
|
|
if a_element_wrapper:
|
|
|
|
element = a_element_wrapper.find('a', {'class': 'tooltip'})
|
|
|
|
if element:
|
|
|
|
link = element.get('href')
|
2020-08-16 08:01:21 +00:00
|
|
|
notes = re.sub(r'(<img.*)(src=")(/)(.*.jpg">)', r"", element.get('title'))
|
2020-04-26 02:08:09 +00:00
|
|
|
title = element.get_text()
|
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(element.find_next_sibling('span', {'class' : 'smGray'}).text.strip('\xa0()'))
|
|
|
|
except:
|
|
|
|
year = None
|
|
|
|
|
|
|
|
td = row.findAll('td')
|
|
|
|
|
|
|
|
try:
|
|
|
|
num_cds = int(td[1].get_text())
|
|
|
|
except:
|
|
|
|
num_cds = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
fps = float(td[2].get_text())
|
|
|
|
except:
|
|
|
|
fps = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
rating = float(td[3].find('img').get('title'))
|
|
|
|
except:
|
|
|
|
rating = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
uploader = td[5].get_text()
|
|
|
|
except:
|
|
|
|
uploader = None
|
|
|
|
|
2020-03-22 07:58:31 +00:00
|
|
|
logger.info('Found subtitle link %r', link)
|
2020-04-26 02:08:09 +00:00
|
|
|
sub = self.download_archive_and_add_subtitle_files('https://subsunacs.net' + link, language, video, fps, num_cds)
|
|
|
|
for s in sub:
|
|
|
|
s.title = title
|
|
|
|
s.notes = notes
|
|
|
|
s.year = year
|
|
|
|
s.rating = rating
|
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 and num_cds == 1 else False
|
2020-03-28 08:10:24 +00:00
|
|
|
subtitles = subtitles + sub
|
2019-04-07 23:31:39 +00:00
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def list_subtitles(self, video, languages):
|
|
|
|
return [s for l in languages for s in self.query(l, video)]
|
|
|
|
|
|
|
|
def download_subtitle(self, subtitle):
|
2019-04-08 17:58:13 +00:00
|
|
|
if subtitle.content:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
seeking_subtitle_file = subtitle.filename
|
2020-04-26 02:08:09 +00:00
|
|
|
arch = self.download_archive_and_add_subtitle_files(subtitle.page_link, subtitle.language, subtitle.video,
|
|
|
|
subtitle.fps, subtitle.num_cds)
|
2019-04-08 17:58:13 +00:00
|
|
|
for s in arch:
|
|
|
|
if s.filename == seeking_subtitle_file:
|
|
|
|
subtitle.content = s.content
|
|
|
|
|
2020-04-26 02:08:09 +00:00
|
|
|
def process_archive_subtitle_files(self, archiveStream, language, video, link, fps, num_cds):
|
2019-04-07 23:31:39 +00:00
|
|
|
subtitles = []
|
|
|
|
type = 'episode' if isinstance(video, Episode) else 'movie'
|
2020-05-26 17:31:32 +00:00
|
|
|
|
|
|
|
is_7zip = isinstance(archiveStream, SevenZipFile)
|
|
|
|
if is_7zip:
|
2020-08-16 08:01:21 +00:00
|
|
|
file_content = archiveStream.readall()
|
|
|
|
file_list = sorted(file_content)
|
2020-05-26 17:31:32 +00:00
|
|
|
else:
|
|
|
|
file_list = sorted(archiveStream.namelist())
|
|
|
|
|
|
|
|
for file_name in file_list:
|
|
|
|
if file_name.lower().endswith(('srt', '.sub', '.txt')):
|
2020-03-29 06:36:58 +00:00
|
|
|
file_is_txt = True if file_name.lower().endswith('.txt') else False
|
2020-05-26 17:31:32 +00:00
|
|
|
if file_is_txt and re.search(r'subsunacs\.net|танете част|прочети|^read ?me|procheti', file_name, re.I):
|
2020-03-29 06:36:58 +00:00
|
|
|
logger.info('Ignore readme txt file %r', file_name)
|
|
|
|
continue
|
2019-04-07 23:31:39 +00:00
|
|
|
logger.info('Found subtitle file %r', file_name)
|
2020-04-26 02:08:09 +00:00
|
|
|
subtitle = SubsUnacsSubtitle(language, file_name, type, video, link, fps, num_cds)
|
2020-05-26 17:31:32 +00:00
|
|
|
if is_7zip:
|
|
|
|
subtitle.content = fix_line_ending(file_content[file_name].read())
|
|
|
|
else:
|
|
|
|
subtitle.content = fix_line_ending(archiveStream.read(file_name))
|
|
|
|
if subtitle.is_valid():
|
2020-03-29 06:36:58 +00:00
|
|
|
subtitles.append(subtitle)
|
2020-05-26 17:31:32 +00:00
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
return subtitles
|
2019-04-08 17:58:13 +00:00
|
|
|
|
2020-04-26 02:08:09 +00:00
|
|
|
def download_archive_and_add_subtitle_files(self, link, language, video, fps, num_cds):
|
2019-04-07 23:31:39 +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:
|
|
|
|
request = self.session.get(link, headers={
|
|
|
|
'Referer': 'https://subsunacs.net/search.php'
|
|
|
|
})
|
|
|
|
request.raise_for_status()
|
|
|
|
region.set(cache_key, request)
|
|
|
|
else:
|
|
|
|
logger.info('Cache file: %s', codecs.encode(cache_key, 'hex_codec').decode('utf-8'))
|
2019-04-07 23:31:39 +00:00
|
|
|
|
2020-08-16 08:01:21 +00:00
|
|
|
try:
|
|
|
|
archive_stream = io.BytesIO(request.content)
|
|
|
|
if is_rarfile(archive_stream):
|
|
|
|
return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps, num_cds)
|
|
|
|
elif is_zipfile(archive_stream):
|
|
|
|
return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps, num_cds)
|
|
|
|
elif archive_stream.seek(0) == 0 and is_7zfile(archive_stream):
|
|
|
|
return self.process_archive_subtitle_files(SevenZipFile(archive_stream), language, video, link, fps, num_cds)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
logger.error('Ignore unsupported archive %r', request.headers)
|
|
|
|
region.delete(cache_key)
|
|
|
|
return []
|