aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/subliminal_patch/test_titlovi.py
blob: 886c43be035b890171b60edc86555d1bbefe24fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pytest
import subliminal
import datetime
import tempfile
import os

from subliminal_patch.providers.titlovi import TitloviProvider 
from subliminal_patch.providers.titlovi import TitloviSubtitle 
from dogpile.cache.region import register_backend as register_cache_backend
from subliminal_patch.core import Episode
from subzero.language import Language
from subliminal.subtitle import fix_line_ending

from zipfile import ZipFile

@pytest.fixture(scope="session")
def titlovi_episodes():
    return {
        "la_femme_nikita_s01e13": Episode(
            "La Femme Nikita (1997) - S01E13 - Recruit [HDTV-720p][Opus 2.0][x265].mkv",
            "La Femme Nikita",
            1,
            13,
            series_imdb_id="tt21209876",
            video_codec="x265",
        ),
    }

@pytest.fixture(scope="session")
def region():
    register_cache_backend("subzero.cache.file", "subzero.cache_backends.file", "SZFileBackend")
    subliminal.region.configure(
        "subzero.cache.file",
        expiration_time=datetime.timedelta(days=30),
        arguments={"appname": "sz_cache", "app_cache_dir": tempfile.gettempdir()},
        replace_existing_backend=True,
    )
    subliminal.region.backend.sync()

def test_list_subtitles_and_download_from_pack(region, titlovi_episodes, requests_mock, data):
    language = Language.fromietf('sr-Latn')
    item = titlovi_episodes["la_femme_nikita_s01e13"]

    with open(os.path.join(data, 'titlovi_gettoken_response.json'), "rb") as f:
        response = f.read()
        requests_mock.post('https://kodi.titlovi.com/api/subtitles/gettoken?username=user1&password=pass1&json=True', content=response)

    with open(os.path.join(data, 'titlovi_search_response.json'), "rb") as f:
        response = f.read()
        requests_mock.get('https://kodi.titlovi.com/api/subtitles/search?token=asdf1234&userid=111&&query=la femme nikita&lang=Srpski&json=True', content=response)
        
    with open(os.path.join(data, 'titlovi_some_subtitle_pack.zip'), "rb") as f:
        response = f.read()
        requests_mock.get('https://titlovi.com/download/?type=1&mediaid=81022', content=response)
        
    with TitloviProvider('user1','pass1') as provider:
        subtitles = provider.list_subtitles(item, languages={language})

        assert len(subtitles) == 1

        subtitle = subtitles[0]
        provider.download_subtitle(subtitle)
        with open(os.path.join(data, 'titlovi_some_subtitle_pack.zip'), "rb") as f:
          archive = ZipFile(f)
          # subs_in_archive = archive.namelist()
          subtitle_content = fix_line_ending(archive.read('La Femme Nikita - 01x13 - Recruit.srt'))
          assert(subtitle.content == subtitle_content)