Skip to content

Commit

Permalink
Fix disabled providers (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze authored Oct 29, 2024
1 parent 326c636 commit 0f72149
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 23 deletions.
3 changes: 3 additions & 0 deletions changelog.d/1181.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove addic7ed and napiprojekt from the list of disabled providers.
Remove the default_providers and default_refiners variables,
instead the get_default_providers() and get_default_refiners() functions can be used.
6 changes: 3 additions & 3 deletions subliminal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
scan_videos,
)
from subliminal.core import ARCHIVE_EXTENSIONS, scan_name, search_external_subtitles
from subliminal.extensions import default_providers, default_refiners
from subliminal.extensions import get_default_providers, get_default_refiners
from subliminal.score import match_hearing_impaired
from subliminal.utils import merge_extend_and_ignore_unions

Expand Down Expand Up @@ -508,7 +508,7 @@ def download(
'ignore': ignore_provider,
},
obj['provider_lists'],
default_providers,
get_default_providers(),
)
logger.info('Use providers: %s', use_providers)
use_refiners = merge_extend_and_ignore_unions(
Expand All @@ -518,7 +518,7 @@ def download(
'ignore': ignore_refiner,
},
obj['refiner_lists'],
default_refiners,
get_default_refiners(),
)
logger.info('Use refiners: %s', use_refiners)

Expand Down
8 changes: 4 additions & 4 deletions subliminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from rarfile import BadRarFile, Error, NotRarFile, RarCannotExec, RarFile, is_rarfile # type: ignore[import-untyped]

from .extensions import (
default_providers,
default_refiners,
discarded_episode_refiners,
discarded_movie_refiners,
get_default_providers,
get_default_refiners,
provider_manager,
refiner_manager,
)
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
providers: Sequence[str] | None = None,
provider_configs: Mapping[str, Any] | None = None,
) -> None:
self.providers = providers if providers is not None else default_providers
self.providers = providers if providers is not None else get_default_providers()
self.provider_configs = provider_configs or {}
self.initialized_providers = {}
self.discarded_providers = set()
Expand Down Expand Up @@ -687,7 +687,7 @@ def refine(
:param kwargs: additional parameters for the :func:`~subliminal.refiners.refine` functions.
"""
refiners = refiners if refiners is not None else default_refiners
refiners = refiners if refiners is not None else get_default_refiners()
if isinstance(video, Movie):
refiners = [r for r in refiners if r not in discarded_movie_refiners]
if isinstance(video, Episode):
Expand Down
16 changes: 11 additions & 5 deletions subliminal/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ def parse_entry_point(src: str, group: str) -> EntryPoint:
)

#: Disabled providers
disabled_providers = ['addic7ed', 'napiprojekt', 'opensubtitlesvip', 'opensubtitlescomvip']
disabled_providers = ['opensubtitlesvip', 'opensubtitlescomvip']


def get_default_providers() -> list[str]:
"""Return the default enabled providers."""
return [p for p in provider_manager.names() if p not in disabled_providers]

#: Default enabled providers
default_providers = [p for p in provider_manager.names() if p not in disabled_providers]

#: Refiner manager
refiner_manager = RegistrableExtensionManager(
Expand All @@ -158,8 +161,11 @@ def parse_entry_point(src: str, group: str) -> EntryPoint:
#: Disabled refiners
disabled_refiners: list[str] = []

#: Default enabled refiners
default_refiners = [r for r in refiner_manager.names() if r not in disabled_refiners]

def get_default_refiners() -> list[str]:
"""Return the default enabled refiners."""
return [r for r in refiner_manager.names() if r not in disabled_refiners]


#: Discarded Movie refiners
discarded_movie_refiners: list[str] = ['tvdb']
Expand Down
6 changes: 4 additions & 2 deletions subliminal/refiners/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import struct
from typing import TYPE_CHECKING, Any, cast

from subliminal.extensions import default_providers, provider_manager
from subliminal.extensions import get_default_providers, provider_manager
from subliminal.providers import Provider

if TYPE_CHECKING:
Expand Down Expand Up @@ -78,8 +78,10 @@ def refine(
logger.warning('Size is lower than 10MB: hashes not computed')
return video

providers = providers if providers is not None else get_default_providers()

logger.debug('Computing hashes for %r', video.name)
for name in providers or default_providers:
for name in providers:
provider = cast(Provider, provider_manager[name].plugin)
if not provider.check_types(video):
continue
Expand Down
577 changes: 577 additions & 0 deletions tests/cassettes/providers/test_list_subtitles_providers_download.yaml

Large diffs are not rendered by default.

52 changes: 47 additions & 5 deletions tests/providers/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
download_subtitles,
list_subtitles,
)
from subliminal.extensions import provider_manager
from subliminal.extensions import disabled_providers, provider_manager
from subliminal.providers.tvsubtitles import TVsubtitlesSubtitle
from subliminal.score import episode_scores
from subliminal.subtitle import Subtitle
Expand Down Expand Up @@ -68,6 +68,7 @@ def test_provider_pool_list_subtitles(episodes):
pool = ProviderPool()
subtitles = pool.list_subtitles(episodes['bbt_s07e05'], {Language('eng')})
assert sorted(subtitles) == [ # type: ignore[type-var,comparison-overlap]
'addic7ed',
'bsplayer',
'gestdown',
'opensubtitles',
Expand All @@ -94,6 +95,7 @@ def test_async_provider_pool_list_subtitles(episodes):
pool = AsyncProviderPool()
subtitles = pool.list_subtitles(episodes['bbt_s07e05'], {Language('eng')})
assert sorted(subtitles) == [ # type: ignore[type-var,comparison-overlap]
'addic7ed',
'bsplayer',
'gestdown',
'opensubtitles',
Expand Down Expand Up @@ -133,15 +135,16 @@ def test_list_subtitles_episode(episodes):
subtitles = list_subtitles({video}, languages)

# test providers
for name in ('addic7ed', 'napiprojekt', 'opensubtitlesvip'):
for name in ('napiprojekt', 'opensubtitlesvip', 'opensubtitlescomvip'):
assert not provider_manager[name].plugin.list_subtitles.called

for name in ('bsplayer', 'gestdown', 'opensubtitles', 'opensubtitlescom', 'podnapisi', 'tvsubtitles'):
for name in ('addic7ed', 'bsplayer', 'gestdown', 'opensubtitles', 'opensubtitlescom', 'podnapisi', 'tvsubtitles'):
assert provider_manager[name].plugin.list_subtitles.called

# test result
assert len(subtitles) == 1
assert sorted(subtitles[episodes['bbt_s07e05']]) == [ # type: ignore[type-var,comparison-overlap]
'addic7ed',
'bsplayer',
'gestdown',
'opensubtitles',
Expand Down Expand Up @@ -178,15 +181,16 @@ def test_list_subtitles_episode_no_hash(episodes):
subtitles = list_subtitles({video}, languages)

# test providers
for name in ('addic7ed', 'napiprojekt', 'opensubtitlesvip'):
for name in ('napiprojekt', 'opensubtitlesvip', 'opensubtitlescomvip'):
assert not provider_manager[name].plugin.list_subtitles.called

for name in ('bsplayer', 'gestdown', 'opensubtitles', 'podnapisi', 'tvsubtitles'):
for name in ('addic7ed', 'bsplayer', 'gestdown', 'opensubtitles', 'podnapisi', 'tvsubtitles'):
assert provider_manager[name].plugin.list_subtitles.called

# test result
assert len(subtitles) == 1
assert sorted(subtitles[episodes['dallas_s01e03']]) == [ # type: ignore[type-var,comparison-overlap]
'addic7ed',
'bsplayer',
'gestdown',
'opensubtitles',
Expand Down Expand Up @@ -335,3 +339,41 @@ def test_download_bad_subtitle(movies):

assert subtitle.content is None
assert subtitle.is_valid() is False


@pytest.mark.integration()
@vcr.use_cassette
def test_list_subtitles_providers_download(episodes):
video = episodes['bbt_s07e05']
languages = {Language('eng')}

# modify global variable
try:
if 'gestdown' not in disabled_providers:
disabled_providers.append('gestdown')

# no subtitles from 'gestdown'
subtitles = list_subtitles({video}, languages)
assert not any(sub.provider_name == 'gestdown' for sub in subtitles[video])

# force using 'gestdown', bypass default when init ProviderPool
subtitles = list_subtitles({video}, languages, providers=['gestdown'])

# test result
assert len(subtitles) == 1
assert len(subtitles[video]) > 0
subtitle = subtitles[video][0]
assert subtitle.provider_name == 'gestdown'
assert subtitle.content is None

# download subtitles
download_subtitles([subtitle], providers=['gestdown'])
assert subtitle.content is not None

# force using 'gestdown', bypass default when init ProviderPool
subtitles = list_subtitles({video}, languages, providers=['gestdown'])

finally:
# reset global variable
if 'gestdown' in disabled_providers:
disabled_providers.remove('gestdown')
8 changes: 4 additions & 4 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from subliminal.extensions import (
EntryPoint,
RegistrableExtensionManager,
default_providers,
default_refiners,
disabled_providers,
disabled_refiners,
get_default_providers,
get_default_refiners,
parse_entry_point,
provider_manager,
refiner_manager,
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_provider_manager():
internal_names = {
parse_entry_point(iep, provider_manager.namespace).name for iep in provider_manager.internal_extensions
}
enabled_names = set(default_providers)
enabled_names = set(get_default_providers())
disabled_names = set(disabled_providers)
assert enabled_names == setup_names - disabled_names
assert internal_names == enabled_names | disabled_names
Expand All @@ -126,7 +126,7 @@ def test_refiner_manager():
internal_names = {
parse_entry_point(iep, refiner_manager.namespace).name for iep in refiner_manager.internal_extensions
}
enabled_names = set(default_refiners)
enabled_names = set(get_default_refiners())
disabled_names = set(disabled_refiners)
assert enabled_names == setup_names - disabled_names
assert internal_names == enabled_names | disabled_names

0 comments on commit 0f72149

Please sign in to comment.