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
|
|
|
|
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
|
|
|
|
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.exceptions import ProviderError
|
|
|
|
from subliminal.utils import sanitize_release_group
|
|
|
|
from subliminal.subtitle import guess_matches
|
|
|
|
from subliminal.video import Episode, Movie
|
|
|
|
from subliminal.subtitle import fix_line_ending
|
|
|
|
from subzero.language import Language
|
|
|
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
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",
|
|
|
|
"Marvel's Jessica Jones": "Jessica Jones",
|
2020-04-21 20:16:15 +00:00
|
|
|
"DC's Legends of Tomorrow": "Legends of Tomorrow",
|
|
|
|
"Doctor Who (2005)": "Doctor Who",
|
2020-03-20 07:56:18 +00:00
|
|
|
}, True)
|
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
class SubsSabBzSubtitle(Subtitle):
|
|
|
|
"""SubsSabBz Subtitle."""
|
|
|
|
provider_name = 'subssabbz'
|
|
|
|
|
2020-04-25 17:24:58 +00:00
|
|
|
def __init__(self, langauge, filename, type, video, link, fps, num_cds):
|
2019-04-07 23:31:39 +00:00
|
|
|
super(SubsSabBzSubtitle, self).__init__(langauge)
|
|
|
|
self.langauge = langauge
|
|
|
|
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-25 17:24:58 +00:00
|
|
|
self.fps = fps
|
|
|
|
self.num_cds = num_cds
|
2020-03-20 07:56:18 +00:00
|
|
|
self.release_info = os.path.splitext(filename)[0]
|
2019-04-07 23:31:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2020-04-25 17:24:58 +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):
|
2020-04-26 02:08:09 +00:00
|
|
|
self.content = None
|
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()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if video_filename == subtitle_filename:
|
|
|
|
matches.add('hash')
|
|
|
|
|
2020-04-25 17:24:58 +00:00
|
|
|
if video.year and self.year == video.year:
|
|
|
|
matches.add('year')
|
|
|
|
|
|
|
|
if isinstance(video, Movie):
|
|
|
|
if video.imdb_id and self.imdb_id == video.imdb_id:
|
|
|
|
matches.add('imdb_id')
|
|
|
|
|
|
|
|
matches |= guess_matches(video, guessit(self.title, {'type': self.type}))
|
2019-04-07 23:31:39 +00:00
|
|
|
matches |= guess_matches(video, guessit(self.filename, {'type': self.type}))
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class SubsSabBzProvider(Provider):
|
|
|
|
"""SubsSabBz Provider."""
|
|
|
|
languages = {Language('por', 'BR')} | {Language(l) for l in [
|
|
|
|
'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()
|
|
|
|
|
|
|
|
def query(self, language, video):
|
|
|
|
subtitles = []
|
|
|
|
isEpisode = isinstance(video, Episode)
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'act': 'search',
|
|
|
|
'movie': '',
|
|
|
|
'select-language': '2',
|
|
|
|
'upldr': '',
|
|
|
|
'yr': '',
|
|
|
|
'release': ''
|
|
|
|
}
|
|
|
|
|
|
|
|
if isEpisode:
|
2020-03-20 07:56:18 +00:00
|
|
|
params['movie'] = "%s %02d %02d" % (sanitize(fix_tv_naming(video.series), {'\''}), video.season, video.episode)
|
2019-04-07 23:31:39 +00:00
|
|
|
else:
|
|
|
|
params['yr'] = video.year
|
2020-03-20 07:56:18 +00:00
|
|
|
params['movie'] = sanitize(video.title, {'\''})
|
2019-04-07 23:31:39 +00:00
|
|
|
|
|
|
|
if language == 'en' or language == 'eng':
|
|
|
|
params['select-language'] = 1
|
|
|
|
|
|
|
|
logger.info('Searching subtitle %r', params)
|
|
|
|
response = self.session.post('http://subs.sab.bz/index.php?', params=params, allow_redirects=False, timeout=10, headers={
|
|
|
|
'Referer': 'http://subs.sab.bz/',
|
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
2019-04-07 23:31:39 +00:00
|
|
|
rows = soup.findAll('tr', {'class': 'subs-row'})
|
|
|
|
|
2020-04-25 17:24:58 +00:00
|
|
|
# Search on first 25 rows only
|
|
|
|
for row in rows[:25]:
|
2019-04-07 23:31:39 +00:00
|
|
|
a_element_wrapper = row.find('td', { 'class': 'c2field' })
|
|
|
|
if a_element_wrapper:
|
|
|
|
element = a_element_wrapper.find('a')
|
|
|
|
if element:
|
|
|
|
link = element.get('href')
|
2020-04-25 17:24:58 +00:00
|
|
|
notes = element.get('onmouseover')
|
|
|
|
title = element.get_text()
|
|
|
|
|
|
|
|
try:
|
|
|
|
year = int(str(element.next_sibling).strip(' ()'))
|
|
|
|
except:
|
|
|
|
year = None
|
|
|
|
|
|
|
|
td = row.findAll('td')
|
|
|
|
|
|
|
|
try:
|
|
|
|
num_cds = int(td[6].get_text())
|
|
|
|
except:
|
|
|
|
num_cds = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
fps = float(td[7].get_text())
|
|
|
|
except:
|
|
|
|
fps = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
uploader = td[8].get_text()
|
|
|
|
except:
|
|
|
|
uploader = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
imdb_id = re.findall(r'imdb.com/title/(tt\d+)/?$', td[9].find('a').get('href'))[0]
|
|
|
|
except:
|
|
|
|
imdb_id = None
|
|
|
|
|
2019-04-07 23:31:39 +00:00
|
|
|
logger.info('Found subtitle link %r', link)
|
2020-04-25 17:24:58 +00:00
|
|
|
sub = self.download_archive_and_add_subtitle_files(link, language, video, fps, num_cds)
|
|
|
|
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-04-25 17:24:58 +00:00
|
|
|
s.imdb_id = imdb_id
|
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-25 17:24:58 +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
|
2019-04-07 23:31:39 +00:00
|
|
|
|
2020-04-25 17:24:58 +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'
|
|
|
|
for file_name in archiveStream.namelist():
|
|
|
|
if file_name.lower().endswith(('.srt', '.sub')):
|
|
|
|
logger.info('Found subtitle file %r', file_name)
|
2020-04-25 17:24:58 +00:00
|
|
|
subtitle = SubsSabBzSubtitle(language, file_name, type, video, link, fps, num_cds)
|
2019-04-07 23:31:39 +00:00
|
|
|
subtitle.content = archiveStream.read(file_name)
|
|
|
|
subtitles.append(subtitle)
|
|
|
|
return subtitles
|
|
|
|
|
2020-04-25 17:24:58 +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)
|
|
|
|
request = self.session.get(link, headers={
|
|
|
|
'Referer': 'http://subs.sab.bz/index.php?'
|
|
|
|
})
|
|
|
|
request.raise_for_status()
|
|
|
|
|
|
|
|
archive_stream = io.BytesIO(request.content)
|
|
|
|
if is_rarfile(archive_stream):
|
2020-04-25 17:24:58 +00:00
|
|
|
return self.process_archive_subtitle_files(RarFile(archive_stream), language, video, link, fps, num_cds)
|
2019-04-07 23:31:39 +00:00
|
|
|
elif is_zipfile(archive_stream):
|
2020-04-25 17:24:58 +00:00
|
|
|
return self.process_archive_subtitle_files(ZipFile(archive_stream), language, video, link, fps, num_cds)
|
2019-04-07 23:31:39 +00:00
|
|
|
else:
|
2020-04-14 06:40:37 +00:00
|
|
|
logger.error('Ignore unsupported archive %r', request.headers)
|
|
|
|
return []
|