2019-05-25 15:40:52 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-09-17 02:04:27 +00:00
|
|
|
|
from __future__ import absolute_import
|
2019-05-25 15:40:52 +00:00
|
|
|
|
import io
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
2021-03-28 18:32:21 +00:00
|
|
|
|
import re
|
2019-05-25 15:40:52 +00:00
|
|
|
|
import time
|
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
|
|
import rarfile
|
|
|
|
|
from subzero.language import Language
|
|
|
|
|
from requests import Session
|
|
|
|
|
|
|
|
|
|
from subliminal import __short_version__
|
2019-09-22 15:24:02 +00:00
|
|
|
|
from subliminal.exceptions import ServiceUnavailable
|
2020-04-03 17:18:44 +00:00
|
|
|
|
from subliminal.providers import ParserBeautifulSoup
|
2021-01-25 21:18:32 +00:00
|
|
|
|
from subliminal.subtitle import SUBTITLE_EXTENSIONS, fix_line_ending, guess_matches
|
2019-05-25 15:40:52 +00:00
|
|
|
|
from subliminal.video import Episode, Movie
|
2020-02-07 14:01:26 +00:00
|
|
|
|
from subliminal_patch.exceptions import APIThrottled
|
2019-09-17 02:04:27 +00:00
|
|
|
|
from six.moves import range
|
2020-04-03 17:18:44 +00:00
|
|
|
|
from subliminal_patch.score import get_scores
|
|
|
|
|
from subliminal_patch.subtitle import Subtitle
|
|
|
|
|
from subliminal_patch.providers import Provider
|
|
|
|
|
from guessit import guessit
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2021-03-28 18:32:21 +00:00
|
|
|
|
|
|
|
|
|
CLEAN_TITLE_RES = [
|
|
|
|
|
(r"subt[ií]tulos de", ""),
|
|
|
|
|
(r"´|`", "'"),
|
|
|
|
|
(r" {2,}", " "),
|
|
|
|
|
]
|
|
|
|
|
|
2019-05-25 15:40:52 +00:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SubdivxSubtitle(Subtitle):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
provider_name = "subdivx"
|
2019-05-25 15:40:52 +00:00
|
|
|
|
hash_verifiable = False
|
|
|
|
|
|
2020-04-03 17:18:44 +00:00
|
|
|
|
def __init__(self, language, video, page_link, title, description, uploader):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
super(SubdivxSubtitle, self).__init__(
|
|
|
|
|
language, hearing_impaired=False, page_link=page_link
|
|
|
|
|
)
|
2020-04-03 17:18:44 +00:00
|
|
|
|
self.video = video
|
2019-05-25 15:40:52 +00:00
|
|
|
|
self.title = title
|
2020-03-28 19:34:39 +00:00
|
|
|
|
self.description = description
|
|
|
|
|
self.uploader = uploader
|
2020-04-03 17:18:44 +00:00
|
|
|
|
self.release_info = self.title
|
|
|
|
|
if self.description and self.description.strip():
|
2021-01-25 21:18:32 +00:00
|
|
|
|
self.release_info += " | " + self.description
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def id(self):
|
2019-09-21 12:09:11 +00:00
|
|
|
|
return self.page_link
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
def get_matches(self, video):
|
|
|
|
|
matches = set()
|
|
|
|
|
|
|
|
|
|
# episode
|
|
|
|
|
if isinstance(video, Episode):
|
2019-05-25 17:35:13 +00:00
|
|
|
|
# already matched in search query
|
2021-01-25 21:18:32 +00:00
|
|
|
|
matches.update(["title", "series", "season", "episode", "year"])
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
# movie
|
|
|
|
|
elif isinstance(video, Movie):
|
2019-05-25 17:35:13 +00:00
|
|
|
|
# already matched in search query
|
2021-01-25 21:18:32 +00:00
|
|
|
|
matches.update(["title", "year"])
|
|
|
|
|
|
|
|
|
|
# Special string comparisons are unnecessary. Guessit can match keys
|
|
|
|
|
# from any string and find even more keywords.
|
|
|
|
|
matches |= guess_matches(
|
|
|
|
|
video,
|
|
|
|
|
guessit(
|
|
|
|
|
self.description,
|
|
|
|
|
{"type": "episode" if isinstance(video, Episode) else "movie"},
|
|
|
|
|
),
|
|
|
|
|
)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SubdivxSubtitlesProvider(Provider):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
provider_name = "subdivx"
|
2019-05-25 15:40:52 +00:00
|
|
|
|
hash_verifiable = False
|
2021-01-25 21:18:32 +00:00
|
|
|
|
languages = {Language.fromalpha2(lang) for lang in ["es"]}
|
2019-05-25 15:40:52 +00:00
|
|
|
|
subtitle_class = SubdivxSubtitle
|
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
server_url = "https://www.subdivx.com/"
|
2019-05-25 15:40:52 +00:00
|
|
|
|
multi_result_throttle = 2
|
|
|
|
|
language_list = list(languages)
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
|
self.session = Session()
|
2021-01-25 21:18:32 +00:00
|
|
|
|
self.session.headers["User-Agent"] = f"Subliminal/{__short_version__}"
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
|
self.session.close()
|
|
|
|
|
|
2020-04-03 17:18:44 +00:00
|
|
|
|
def query(self, video, languages):
|
|
|
|
|
if isinstance(video, Episode):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
query = f"{video.series} S{video.season:02}E{video.episode:02}"
|
2020-04-03 17:18:44 +00:00
|
|
|
|
else:
|
2020-10-11 19:40:48 +00:00
|
|
|
|
# Subdvix has problems searching foreign movies if the year is
|
2021-01-25 21:18:32 +00:00
|
|
|
|
# appended. A proper solution would be filtering results with the
|
|
|
|
|
# year in self._parse_subtitles_page.
|
2020-04-03 17:18:44 +00:00
|
|
|
|
query = video.title
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
params = {
|
2021-01-25 21:18:32 +00:00
|
|
|
|
"q": query, # search string
|
|
|
|
|
"accion": 5, # action search
|
|
|
|
|
"oxdown": 1, # order by downloads descending
|
|
|
|
|
"pg": 1, # page 1
|
2019-05-25 15:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug(f"Searching subtitles: {query}")
|
2019-05-25 15:40:52 +00:00
|
|
|
|
subtitles = []
|
|
|
|
|
language = self.language_list[0]
|
2021-01-25 21:18:32 +00:00
|
|
|
|
search_link = self.server_url + "index.php"
|
2019-05-25 15:40:52 +00:00
|
|
|
|
while True:
|
2020-03-28 19:34:39 +00:00
|
|
|
|
response = self.session.get(search_link, params=params, timeout=20)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
self._check_response(response)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
try:
|
2020-04-03 17:18:44 +00:00
|
|
|
|
page_subtitles = self._parse_subtitles_page(video, response, language)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
except Exception as e:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.error(f"Error parsing subtitles list: {e}")
|
2020-02-07 14:01:26 +00:00
|
|
|
|
break
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
subtitles += page_subtitles
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2020-10-11 20:03:35 +00:00
|
|
|
|
if len(page_subtitles) < 100:
|
2020-03-28 19:34:39 +00:00
|
|
|
|
break # this is the last page
|
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
params["pg"] += 1 # search next page
|
2020-03-28 19:34:39 +00:00
|
|
|
|
time.sleep(self.multi_result_throttle)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
|
|
def list_subtitles(self, video, languages):
|
2020-04-03 17:18:44 +00:00
|
|
|
|
return self.query(video, languages)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
def download_subtitle(self, subtitle):
|
|
|
|
|
if isinstance(subtitle, SubdivxSubtitle):
|
|
|
|
|
# download the subtitle
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.info("Downloading subtitle %r", subtitle)
|
2019-09-21 12:09:11 +00:00
|
|
|
|
|
|
|
|
|
# get download link
|
2019-09-22 15:24:02 +00:00
|
|
|
|
download_link = self._get_download_link(subtitle)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
# download zip / rar file with the subtitle
|
2021-01-25 21:18:32 +00:00
|
|
|
|
response = self.session.get(
|
|
|
|
|
self.server_url + download_link,
|
|
|
|
|
headers={"Referer": subtitle.page_link},
|
|
|
|
|
timeout=30,
|
|
|
|
|
)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
self._check_response(response)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
# open the compressed archive
|
|
|
|
|
archive = self._get_archive(response.content)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
# extract the subtitle
|
2020-04-03 17:18:44 +00:00
|
|
|
|
subtitle_content = self._get_subtitle_from_archive(archive, subtitle)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
subtitle.content = fix_line_ending(subtitle_content)
|
|
|
|
|
|
2020-04-03 17:18:44 +00:00
|
|
|
|
def _parse_subtitles_page(self, video, response, language):
|
2019-09-22 15:24:02 +00:00
|
|
|
|
subtitles = []
|
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
page_soup = ParserBeautifulSoup(
|
|
|
|
|
response.content.decode("utf-8", "ignore"), ["lxml", "html.parser"]
|
|
|
|
|
)
|
|
|
|
|
title_soups = page_soup.find_all("div", {"id": "menu_detalle_buscador"})
|
|
|
|
|
body_soups = page_soup.find_all("div", {"id": "buscador_detalle"})
|
2019-09-22 15:24:02 +00:00
|
|
|
|
|
|
|
|
|
for subtitle in range(0, len(title_soups)):
|
|
|
|
|
title_soup, body_soup = title_soups[subtitle], body_soups[subtitle]
|
|
|
|
|
|
|
|
|
|
# title
|
2021-03-28 18:32:21 +00:00
|
|
|
|
title = self._clean_title(title_soup.find("a").text)
|
2020-10-11 19:40:48 +00:00
|
|
|
|
|
|
|
|
|
# filter by year
|
|
|
|
|
if video.year and str(video.year) not in title:
|
|
|
|
|
continue
|
|
|
|
|
|
2020-03-28 19:34:39 +00:00
|
|
|
|
page_link = title_soup.find("a")["href"]
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2020-03-28 19:34:39 +00:00
|
|
|
|
# description
|
2021-01-25 21:18:32 +00:00
|
|
|
|
description = body_soup.find("div", {"id": "buscador_detalle_sub"}).text
|
2020-03-28 19:34:39 +00:00
|
|
|
|
description = description.replace(",", " ").lower()
|
|
|
|
|
|
|
|
|
|
# uploader
|
2021-01-25 21:18:32 +00:00
|
|
|
|
uploader = body_soup.find("a", {"class": "link1"}).text
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
subtitle = self.subtitle_class(
|
|
|
|
|
language, video, page_link, title, description, uploader
|
|
|
|
|
)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug("Found subtitle %r", subtitle)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
subtitles.append(subtitle)
|
|
|
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
|
|
def _get_download_link(self, subtitle):
|
2020-03-28 19:34:39 +00:00
|
|
|
|
response = self.session.get(subtitle.page_link, timeout=20)
|
2019-09-22 15:24:02 +00:00
|
|
|
|
self._check_response(response)
|
|
|
|
|
try:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
page_soup = ParserBeautifulSoup(
|
|
|
|
|
response.content.decode("utf-8", "ignore"), ["lxml", "html.parser"]
|
|
|
|
|
)
|
|
|
|
|
links_soup = page_soup.find_all("a", {"class": "detalle_link"})
|
2019-09-22 15:24:02 +00:00
|
|
|
|
for link_soup in links_soup:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
if link_soup["href"].startswith("bajar"):
|
|
|
|
|
return self.server_url + link_soup["href"]
|
|
|
|
|
links_soup = page_soup.find_all("a", {"class": "link1"})
|
2020-02-07 19:20:53 +00:00
|
|
|
|
for link_soup in links_soup:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
if "bajar.php" in link_soup["href"]:
|
|
|
|
|
return link_soup["href"]
|
2019-09-22 15:24:02 +00:00
|
|
|
|
except Exception as e:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
raise APIThrottled(f"Error parsing download link: {e}")
|
2019-09-22 15:24:02 +00:00
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
raise APIThrottled("Download link not found")
|
2019-09-22 15:24:02 +00:00
|
|
|
|
|
2021-03-28 18:32:21 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _clean_title(title):
|
|
|
|
|
"""
|
|
|
|
|
Normalize apostrophes and spaces to avoid matching problems
|
|
|
|
|
(e.g. Subtitulos de Carlito´s Way -> Carlito's Way)
|
|
|
|
|
"""
|
|
|
|
|
for og, new in CLEAN_TITLE_RES:
|
|
|
|
|
title = re.sub(og, new, title, flags=re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
return title
|
|
|
|
|
|
2020-10-03 23:41:41 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _check_response(response):
|
|
|
|
|
if response.status_code != 200:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
raise ServiceUnavailable(f"Bad status code: {response.status_code}")
|
2020-10-03 23:41:41 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_archive(content):
|
2019-09-22 15:24:02 +00:00
|
|
|
|
# open the archive
|
|
|
|
|
archive_stream = io.BytesIO(content)
|
|
|
|
|
if rarfile.is_rarfile(archive_stream):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug("Identified rar archive")
|
2019-09-22 15:24:02 +00:00
|
|
|
|
archive = rarfile.RarFile(archive_stream)
|
|
|
|
|
elif zipfile.is_zipfile(archive_stream):
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug("Identified zip archive")
|
2019-09-22 15:24:02 +00:00
|
|
|
|
archive = zipfile.ZipFile(archive_stream)
|
|
|
|
|
else:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
raise APIThrottled("Unsupported compressed format")
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2019-09-22 15:24:02 +00:00
|
|
|
|
return archive
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2020-10-03 23:41:41 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_subtitle_from_archive(archive, subtitle):
|
|
|
|
|
_valid_names = []
|
2019-09-22 15:24:02 +00:00
|
|
|
|
for name in archive.namelist():
|
|
|
|
|
# discard hidden files
|
|
|
|
|
# discard non-subtitle files
|
2021-01-25 21:18:32 +00:00
|
|
|
|
if not os.path.split(name)[-1].startswith(".") and name.lower().endswith(
|
|
|
|
|
SUBTITLE_EXTENSIONS
|
|
|
|
|
):
|
2020-10-03 23:41:41 +00:00
|
|
|
|
_valid_names.append(name)
|
|
|
|
|
|
|
|
|
|
# archive with only 1 subtitle
|
|
|
|
|
if len(_valid_names) == 1:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug(
|
|
|
|
|
f"returning from archive: {_valid_names[0]} (single subtitle file)"
|
|
|
|
|
)
|
2020-10-03 23:41:41 +00:00
|
|
|
|
return archive.read(_valid_names[0])
|
|
|
|
|
|
|
|
|
|
# in archives with more than 1 subtitle (season pack) we try to guess the best subtitle file
|
|
|
|
|
_scores = get_scores(subtitle.video)
|
|
|
|
|
_max_score = 0
|
|
|
|
|
_max_name = ""
|
|
|
|
|
for name in _valid_names:
|
|
|
|
|
_guess = guessit(name)
|
2021-01-25 21:18:32 +00:00
|
|
|
|
if "season" not in _guess:
|
|
|
|
|
_guess["season"] = -1
|
|
|
|
|
if "episode" not in _guess:
|
|
|
|
|
_guess["episode"] = -1
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2020-04-03 17:18:44 +00:00
|
|
|
|
if isinstance(subtitle.video, Episode):
|
2020-10-03 23:41:41 +00:00
|
|
|
|
logger.debug("guessing %s" % name)
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug(
|
|
|
|
|
f"subtitle S{_guess['season']}E{_guess['episode']} video "
|
|
|
|
|
f"S{subtitle.video.season}E{subtitle.video.episode}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
subtitle.video.episode != _guess["episode"]
|
|
|
|
|
or subtitle.video.season != _guess["season"]
|
|
|
|
|
):
|
|
|
|
|
logger.debug("subtitle does not match video, skipping")
|
2020-04-03 17:18:44 +00:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
matches = set()
|
2020-10-03 23:41:41 +00:00
|
|
|
|
matches |= guess_matches(subtitle.video, _guess)
|
|
|
|
|
_score = sum((_scores.get(match, 0) for match in matches))
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug("srt matches: %s, score %d" % (matches, _score))
|
2020-04-03 17:18:44 +00:00
|
|
|
|
if _score > _max_score:
|
|
|
|
|
_max_score = _score
|
2020-10-03 23:41:41 +00:00
|
|
|
|
_max_name = name
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug(f"new max: {name} {_score}")
|
2020-04-03 17:18:44 +00:00
|
|
|
|
|
|
|
|
|
if _max_score > 0:
|
2021-01-25 21:18:32 +00:00
|
|
|
|
logger.debug(f"returning from archive: {_max_name} scored {_max_score}")
|
2020-04-03 17:18:44 +00:00
|
|
|
|
return archive.read(_max_name)
|
2019-05-25 15:40:52 +00:00
|
|
|
|
|
2021-01-25 21:18:32 +00:00
|
|
|
|
raise APIThrottled("Can not find the subtitle in the compressed file")
|