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.
150 lines
5.5 KiB
150 lines
5.5 KiB
5 years ago
|
# -*- coding: utf-8 -*-
|
||
|
import logging
|
||
|
import os
|
||
|
|
||
|
from requests import Session
|
||
|
from subzero.language import Language
|
||
|
|
||
4 years ago
|
from guessit import guessit
|
||
|
from subliminal import Episode, __short_version__
|
||
5 years ago
|
from subliminal_patch.subtitle import Subtitle, guess_matches
|
||
4 years ago
|
from subliminal.providers import ParserBeautifulSoup
|
||
4 years ago
|
from subliminal.subtitle import fix_line_ending
|
||
5 years ago
|
from subliminal_patch.providers import Provider
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class SubtitulamosTVSubtitle(Subtitle):
|
||
|
provider_name = 'subtitulamostv'
|
||
5 years ago
|
hash_verifiable = False
|
||
|
|
||
4 years ago
|
def __init__(self, language, page_link, download_link, release_info):
|
||
4 years ago
|
super(SubtitulamosTVSubtitle, self).__init__(language, hearing_impaired=False, page_link=page_link)
|
||
5 years ago
|
self.download_link = download_link
|
||
5 years ago
|
self.release_info = release_info
|
||
5 years ago
|
|
||
|
@property
|
||
|
def id(self):
|
||
5 years ago
|
return self.download_link
|
||
5 years ago
|
|
||
|
def get_matches(self, video):
|
||
4 years ago
|
matches = {'series', 'season', 'episode', 'year', 'title'}
|
||
4 years ago
|
|
||
4 years ago
|
if video.release_group and video.release_group.lower() in self.release_info.lower():
|
||
5 years ago
|
matches.add('release_group')
|
||
|
|
||
2 years ago
|
matches |= guess_matches(video, guessit(self.release_info, {"type": "episode"}))
|
||
5 years ago
|
|
||
|
return matches
|
||
|
|
||
|
|
||
|
class SubtitulamosTVProvider(Provider):
|
||
|
"""Subtitulamostv Provider"""
|
||
4 years ago
|
languages = {Language.fromietf(lang) for lang in ['en', 'es']}
|
||
5 years ago
|
video_types = (Episode,)
|
||
|
|
||
4 years ago
|
server_url = 'https://www.subtitulamos.tv'
|
||
4 years ago
|
|
||
|
def __init__(self):
|
||
|
self.session = None
|
||
|
|
||
5 years ago
|
def initialize(self):
|
||
|
self.session = Session()
|
||
|
self.session.headers = {
|
||
|
'User-Agent': os.environ.get("SZ_USER_AGENT", "Sub-Zero/2")}
|
||
|
|
||
|
def terminate(self):
|
||
|
self.session.close()
|
||
|
|
||
|
def query(self, languages, video):
|
||
4 years ago
|
subtitle_name = "%s %dx%02d" % (video.series, video.season, video.episode)
|
||
|
logger.debug('Searching subtitles "%s"' % subtitle_name)
|
||
5 years ago
|
|
||
4 years ago
|
response = self.session.get(self.server_url + '/search/query', params={'q': video.series}, timeout=10)
|
||
|
response.raise_for_status()
|
||
|
result = response.json()
|
||
5 years ago
|
|
||
|
subtitles = []
|
||
4 years ago
|
for serie in result:
|
||
|
# skip non-matching series
|
||
4 years ago
|
if video.series.lower() != serie['show_name'].lower():
|
||
4 years ago
|
continue
|
||
|
|
||
4 years ago
|
# season page
|
||
|
response = self.session.get(self.server_url + "/shows/%d" % serie['show_id'], timeout=10)
|
||
4 years ago
|
response.raise_for_status()
|
||
|
soup = ParserBeautifulSoup(response.text, ['lxml', 'html.parser'])
|
||
4 years ago
|
season_found = False
|
||
|
for season in soup.select('#season-choices a'):
|
||
|
if season.text.strip() == str(video.season):
|
||
|
season_found = True
|
||
|
if "selected" not in season.attrs['class']:
|
||
|
# go to the right season page
|
||
|
response = self.session.get(self.server_url + season['href'], timeout=10)
|
||
|
response.raise_for_status()
|
||
|
soup = ParserBeautifulSoup(response.text, ['lxml', 'html.parser'])
|
||
|
break
|
||
|
if not season_found:
|
||
|
continue
|
||
4 years ago
|
|
||
4 years ago
|
# episode page
|
||
|
episode_found = False
|
||
|
for episode in soup.select('#episode-choices a'):
|
||
|
if episode.text.strip() == str(video.episode):
|
||
|
episode_found = True
|
||
|
if "selected" not in episode.attrs['class']:
|
||
|
# go to the right episode page
|
||
|
response = self.session.get(self.server_url + episode['href'], timeout=10)
|
||
|
response.raise_for_status()
|
||
|
soup = ParserBeautifulSoup(response.text, ['lxml', 'html.parser'])
|
||
|
break
|
||
|
if not episode_found:
|
||
|
continue
|
||
|
episode_url = response.url
|
||
|
|
||
|
# subtitles
|
||
|
for lang in soup.select("div.language-container"):
|
||
|
lang_name = lang.select("div.language-name")[0].text
|
||
|
if "English" in lang_name:
|
||
|
language = "en"
|
||
|
elif "Español" in lang_name:
|
||
|
language = "es"
|
||
|
else:
|
||
|
continue # not supported yet
|
||
|
logger.debug('Found subtitles in "%s" language.', language)
|
||
|
|
||
|
for release in lang.select("div.version-container"):
|
||
|
if len(release.select('a[href*="/download"]')) != 1:
|
||
|
continue # incomplete translation, download link is not available
|
||
|
|
||
|
release_name = release.select('div.version-container p')[1].text
|
||
|
release_url = self.server_url + release.select('a[href*="/download"]')[0]['href']
|
||
|
|
||
|
subtitles.append(
|
||
|
SubtitulamosTVSubtitle(
|
||
|
Language.fromietf(language),
|
||
|
episode_url,
|
||
|
release_url,
|
||
|
release_name
|
||
5 years ago
|
)
|
||
4 years ago
|
)
|
||
4 years ago
|
|
||
5 years ago
|
return subtitles
|
||
|
|
||
|
def list_subtitles(self, video, languages):
|
||
|
return self.query(languages, video)
|
||
|
|
||
|
def download_subtitle(self, subtitle):
|
||
|
logger.info('Downloading subtitle %r', subtitle)
|
||
|
r = self.session.get(subtitle.download_link, timeout=10)
|
||
|
r.raise_for_status()
|
||
|
|
||
5 years ago
|
r.encoding = "ISO-8859-1"
|
||
5 years ago
|
subtitle_content = r.content
|
||
5 years ago
|
|
||
|
if subtitle_content:
|
||
|
subtitle.content = fix_line_ending(subtitle_content)
|
||
|
else:
|
||
|
logger.debug('Could not download subtitle from %s', subtitle.download_link)
|