Subtitrarinoi provider: add several improvements

* Fix episode matches (#2018)
* Use standard utils to download subtitles
* Use standard utils to guess matches (partially implemented)
This commit is contained in:
Vitiko 2022-12-20 17:22:49 -04:00
parent e6864a9001
commit 1233026adc
2 changed files with 80 additions and 59 deletions

View File

@ -1,28 +1,30 @@
# coding=utf-8
from __future__ import absolute_import
import os
import io
import logging
import re
from zipfile import ZipFile, is_zipfile
from rarfile import RarFile, is_rarfile
from guessit import guessit
from subliminal.providers import ParserBeautifulSoup
from subliminal.video import Episode
from subliminal.video import Movie
from subliminal_patch.providers import Provider
from subliminal_patch.providers.mixins import ProviderSubtitleArchiveMixin
from subliminal_patch.subtitle import Subtitle, guess_matches
from subliminal_patch.utils import sanitize, fix_inconsistent_naming as _fix_inconsistent_naming
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
from subliminal.exceptions import ProviderError
from subliminal.providers import ParserBeautifulSoup
from subliminal.video import Episode, Movie
from subliminal.subtitle import SUBTITLE_EXTENSIONS
from subliminal_patch.providers.utils import get_archive_from_bytes
from subliminal_patch.providers.utils import get_subtitle_from_archive
from subliminal_patch.providers.utils import update_matches
from subliminal_patch.subtitle import guess_matches
from subliminal_patch.subtitle import Subtitle
from subliminal_patch.utils import \
fix_inconsistent_naming as _fix_inconsistent_naming
from subliminal_patch.utils import sanitize
from subzero.language import Language
# parsing regex definitions
title_re = re.compile(r'(?P<title>(?:.+(?= [Aa][Kk][Aa] ))|.+)(?:(?:.+)(?P<altitle>(?<= [Aa][Kk][Aa] ).+))?')
_SEASON_RE = re.compile(r"(s|(season|sezonul)\s)(?P<x>\d{1,2})", flags=re.IGNORECASE)
def fix_inconsistent_naming(title):
"""Fix titles with inconsistent naming using dictionary and sanitize them.
@ -48,7 +50,7 @@ class SubtitrarinoiSubtitle(Subtitle):
super(SubtitrarinoiSubtitle, self).__init__(language)
self.sid = sid
self.title = title
self.imdb_id = imdb_id
self.imdb_id = (imdb_id or "").rstrip("/")
self.download_link = download_link
self.year = year
self.download_count = download_count
@ -87,8 +89,7 @@ class SubtitrarinoiSubtitle(Subtitle):
if video.imdb_id and self.imdb_id == video.imdb_id:
matches.add('imdb_id')
# guess match others
matches |= guess_matches(video, guessit(self.comments, {"type": "movie"}))
update_matches(matches, video, self.comments)
else:
# title
@ -100,16 +101,19 @@ class SubtitrarinoiSubtitle(Subtitle):
if video.series_imdb_id and self.imdb_id == video.series_imdb_id:
matches.add('imdb_id')
# season
if f"Sezonul {video.season}" in self.comments:
matches.add('season')
season = _SEASON_RE.search(self.comments)
if season is not None:
season = int(season.group("x"))
if season == video.season:
matches.add('season')
logger.debug("Season matched? %s [%s -> %s]", "season" in matches, video.season, self.comments)
# episode
if {"imdb_id", "season"}.issubset(matches):
matches.add('episode')
# guess match others
matches |= guess_matches(video, guessit(self.comments, {"type": "episode"}))
update_matches(matches, video, self.comments)
self.matches = matches
@ -277,42 +281,5 @@ class SubtitrarinoiProvider(Provider, ProviderSubtitleArchiveMixin):
r = self.session.get(subtitle.download_link, headers={'Referer': self.api_url}, timeout=10)
r.raise_for_status()
# open the archive
archive_stream = io.BytesIO(r.content)
if is_rarfile(archive_stream):
logger.debug('Archive identified as rar')
archive = RarFile(archive_stream)
elif is_zipfile(archive_stream):
logger.debug('Archive identified as zip')
archive = ZipFile(archive_stream)
else:
subtitle.content = r.content
if subtitle.is_valid():
return
subtitle.content = None
raise ProviderError('Unidentified archive type')
if subtitle.is_episode:
subtitle.content = self._get_subtitle_from_archive(subtitle, archive)
else:
subtitle.content = self.get_subtitle_from_archive(subtitle, archive)
@staticmethod
def _get_subtitle_from_archive(subtitle, archive):
for name in archive.namelist():
# discard hidden files
if os.path.split(name)[-1].startswith('.'):
continue
# discard non-subtitle files
if not name.lower().endswith(SUBTITLE_EXTENSIONS):
continue
_guess = guessit(name)
if subtitle.desired_episode == _guess['episode']:
return archive.read(name)
return None
# vim: set expandtab ts=4 sw=4:
archive = get_archive_from_bytes(r.content)
subtitle.content = get_subtitle_from_archive(archive, episode=subtitle.desired_episode)

View File

@ -0,0 +1,54 @@
import pytest
from subliminal_patch.providers.subtitrarinoi import SubtitrarinoiProvider
from subliminal_patch.providers.subtitrarinoi import SubtitrarinoiSubtitle
from subzero.language import Language
romanian = Language("ron")
def test_list_subtitles(episodes):
episode = episodes["breaking_bad_s01e01"]
with SubtitrarinoiProvider() as provider:
assert provider.list_subtitles(episode, [romanian])
@pytest.fixture
def subtitrari_subtitle():
yield SubtitrarinoiSubtitle(
romanian,
"https://www.subtitrari-noi.ro/7493-subtitrari noi.ro\ ",
3,
"Sezonul 1 ep. 1-7 Sincronizari si pentru variantele HDTV x264 (Sincro atty)",
"Breaking Bad",
"tt0903747/",
"Alice",
"https://www.subtitrari-noi.ro/index.php?page=movie_details&act=1&id=7493",
2008,
4230,
True,
1,
)
@pytest.mark.parametrize("comment", ["season 01", "Sezonul 1 ep. 1-7", "S01"])
def test_subtitle_get_matches_episode(subtitrari_subtitle, episodes, comment):
episode = episodes["breaking_bad_s01e01"]
episode.episode = 1
subtitrari_subtitle.comments = comment
assert {"season", "episode", "series", "imdb_id"}.issubset(
subtitrari_subtitle.get_matches(episode)
)
@pytest.mark.parametrize("comment", ["season 02", "Sezonul 2 ep. 1-7", "version 01"])
def test_subtitle_get_matches_episode_false(subtitrari_subtitle, episodes, comment):
episode = episodes["breaking_bad_s01e01"]
episode.episode = 1
subtitrari_subtitle.comments = comment
assert not {"season", "episode"}.issubset(subtitrari_subtitle.get_matches(episode))
def test_provider_download_subtitle(subtitrari_subtitle):
with SubtitrarinoiProvider() as provider:
provider.download_subtitle(subtitrari_subtitle)
assert subtitrari_subtitle.is_valid()