Esempio n. 1
0
def test_add_vtt_files_to_movie_content_skips_existing(
        tmp_path: PosixPath) -> None:
    videos: List[str] = ["eng.vtt", "subtitle.vtt"]
    existing: PosixPath = tmp_path / videos[0]
    for video in videos:
        (tmp_path / video).touch()
    movie_subtitle: MovieSubtitle = MovieSubtitleFactory(
        full_path=str(existing),
        relative_path=existing.relative_to(existing.parent.parent.parent),
        file_name=existing.name,
        suffix=existing.suffix,
    )
    movie_content: MovieContent = MovieContentFactory.create(
        movie_subtitle=[movie_subtitle])

    assert MovieSubtitle.objects.count() == 1
    assert movie_content.movie_subtitle.count() == 1

    _add_vtt_files_to_movie_content(movie_content=movie_content,
                                    subtitles_folder=tmp_path)
    movie_content.refresh_from_db()

    assert MovieSubtitle.objects.count() == 2
    assert movie_content.movie_subtitle.count() == 2

    movie_subtitles = movie_content.movie_subtitle.all()

    for index, movie_subtitle in enumerate(movie_subtitles):
        assert movie_subtitle.full_path == str(tmp_path / videos[index])
        assert movie_subtitle.relative_path == str(
            Path(tmp_path.parent.name) / tmp_path.name / videos[index])
        assert movie_subtitle.file_name == videos[index]
        assert movie_subtitle.suffix == ".vtt"
Esempio n. 2
0
    def test_functions_are_called_correctly(self,
                                            mocker: MockerFixture) -> None:
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        remove_category = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                     "remove_category")
        create_category = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                     "create_category")
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        download_from_link = mocker.spy(panel.tasks.tests.mocks.MockClient,
                                        "download_from_link")
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])

        download_torrent(movie_content_id=movie_content.id)

        remove_category.assert_called_once_with(str(movie_content.id))
        create_category.assert_called_once_with(str(movie_content.id))
        download_from_link.assert_called_once_with(
            movie_content.torrent_source, category=str(movie_content.id))
        panel.tasks.torrent.check_and_process_torrent.delay.assert_called_once(
        )
        panel.tasks.torrent.download_movie_info.delay.assert_called_once()
Esempio n. 3
0
def test_add_vtt_files_to_movie_content(tmp_path: PosixPath) -> None:
    videos: List[str] = ["ger.vtt", "subtitle.vtt"]
    file_to_lang: Dict[str, str] = {file_name: "ger" for file_name in videos}
    for video in videos:
        (tmp_path / video).touch()
    movie_content: MovieContent = MovieContentFactory()

    assert not MovieSubtitle.objects.exists()

    _add_vtt_files_to_movie_content(
        movie_content=movie_content,
        subtitles_folder=tmp_path,
        file_to_lang=file_to_lang,
    )
    movie_content.refresh_from_db()

    assert MovieSubtitle.objects.count() == 2
    assert movie_content.movie_subtitle.count() == 2

    movie_subtitles = movie_content.movie_subtitle.all()

    for index, movie_subtitle in enumerate(movie_subtitles):
        assert movie_subtitle.full_path == str(tmp_path / videos[index])
        assert movie_subtitle.relative_path == str(
            Path(tmp_path.parent.name) / tmp_path.name / videos[index]
        )
        assert movie_subtitle.file_name == videos[index]
        assert movie_subtitle.lang_three == "ger"
        assert movie_subtitle.suffix == ".vtt"
Esempio n. 4
0
    def test_updates_movie_content_and_movie(self, mocker: MockerFixture,
                                             tmp_path: PosixPath) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        mocker.patch("panel.tasks.torrent.fetch_subtitles.delay")
        mocker.patch("os.chmod")
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True)
        run.return_value = MockSubprocess(
            stdout=json.dumps(RAW_INFO).encode("UTF-8"))
        movie_content: MovieContent = MovieContentFactory()
        movie: Movie = MovieFactory.create(movie_content=[movie_content])
        movie_content.full_path = str(tmp_path)
        movie_content.save()
        video: PosixPath = tmp_path / "video.mkv"
        video.touch()

        process_videos_in_folder(movie_content.id)
        movie_content.refresh_from_db()
        movie.refresh_from_db()
        hashed_title: str = _hash(video.name)

        assert movie_content.full_path == str(
            (tmp_path / (f"{hashed_title}.mp4")))
        assert movie_content.file_name == hashed_title
        assert movie_content.relative_path == f"{hashed_title}.mp4"
        assert movie_content.file_extension == ".mp4"
        assert movie_content.source_file_name == video.name
        assert movie_content.source_file_extension == video.suffix
        assert movie_content.resolution_width == RAW_INFO["streams"][0][
            "width"]
        assert movie_content.resolution_height == RAW_INFO["streams"][0][
            "height"]
        assert movie_content.is_ready is True
        assert movie.duration == int(float(RAW_INFO["streams"][0]["duration"]))
Esempio n. 5
0
    def test_raises_if_no_movie_torrent(self):
        movie_content: MovieContent = MovieContentFactory()

        with pytest.raises(Exception) as exc:
            _get_root_path(movie_content.id)

        assert (str(exc.value) ==
                f"MovieTorrent could not be found for {movie_content.id}")
Esempio n. 6
0
    def test_if_full_path_is_a_file(self, tmp_path: PosixPath) -> None:
        new_file: PosixPath = tmp_path / "test.mp4"
        new_file.touch()
        movie_content: MovieContent = MovieContentFactory()
        movie_content.full_path = str(new_file)
        movie_content.save()

        assert _get_root_path(movie_content.id) == str(tmp_path)
Esempio n. 7
0
    def test_raises_when_movie_content_full_path_not_exists(self) -> None:
        movie_content: MovieContent = MovieContentFactory()
        with pytest.raises(FileNotFoundError) as exc:
            fetch_subtitles(movie_content.id)

        assert (
            str(exc.value) ==
            f"{movie_content.full_path} does not exist for MovieContent {movie_content.id}"
        )
Esempio n. 8
0
    def test_main_folder_is_defined_but_not_full_path(
            self, tmp_path: PosixPath, mocker: MockerFixture) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        folder: PosixPath = tmp_path / "folder"
        folder.mkdir(exist_ok=True)
        movie_content: MovieContent = MovieContentFactory()
        movie_content.main_folder = folder.name
        movie_content.save()

        assert _get_root_path(movie_content.id) == str(folder)
Esempio n. 9
0
    def test_full_path_as_file_and_main_folder_as_file(
            self, tmp_path: PosixPath, mocker: MockerFixture) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        new_file: PosixPath = tmp_path / "test.mp4"
        new_file.touch()
        movie_content: MovieContent = MovieContentFactory()
        movie_content.full_path = str(new_file)
        movie_content.main_folder = new_file.name
        movie_content.save()

        assert _get_root_path(movie_content.id) == str(tmp_path)
Esempio n. 10
0
    def test_raises_if_no_process_videos(self, tmp_path: PosixPath) -> None:
        movie_content: MovieContent = MovieContentFactory()
        movie_content.full_path = str(tmp_path)
        movie_content.save()

        with pytest.raises(Exception) as exc:
            process_videos_in_folder(movie_content.id)

        assert (
            str(exc.value) ==
            f"No videos are returned to be processed for movie content {movie_content.id}"
        )
Esempio n. 11
0
    def test_creates_movie_torrent(self, mocker: MockerFixture) -> None:
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        mocker.patch("panel.tasks.torrent.check_and_process_torrent.delay")
        mocker.patch("panel.tasks.torrent.download_movie_info.delay")
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])

        assert not MovieTorrent.objects.filter(
            movie_content=movie_content).exists()

        download_torrent(movie_content_id=movie_content.id)

        assert MovieTorrent.objects.filter(
            movie_content=movie_content).exists()
Esempio n. 12
0
    def test_fetches_subtitles(
        self, tmp_path: PosixPath, mocker: MockerFixture
    ) -> None:
        mocker.patch("panel.tasks.subtitles.requests", MockRequest)
        mocker.patch("panel.tasks.subtitles.get_hash", lambda x: "somehash")
        mocker.patch(
            "panel.tasks.subtitles._get_response_from_api", _mock_get_response_from_api
        )
        mocker.patch.object(settings, "SUBTITLE_LANGS", "eng,ger")
        subtitle = tmp_path / "test.srt"
        subtitle.touch()
        subtitle_dir: str = "vtt_subtitles"
        movie_content: MovieContent = MovieContentFactory(full_path=str(tmp_path))
        MovieFactory.create(movie_content=[movie_content])
        langs: List[str] = get_subtitle_language()

        assert not movie_content.movie_subtitle.exists()

        fetch_subtitles(
            movie_content_id=movie_content.id, limit=2, delete_original=True
        )

        assert (tmp_path / subtitle_dir).is_dir()
        for lang in langs:
            assert (tmp_path / subtitle_dir / f"{lang}1.vtt").is_file()
            assert (tmp_path / subtitle_dir / f"{lang}2.vtt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}1.srt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}2.srt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}3.vtt").is_file()
            assert not (tmp_path / subtitle_dir / f"{lang}3.srt").is_file()

        assert movie_content.movie_subtitle.count() == 5
        assert movie_content.movie_subtitle.first().full_path == str(
            tmp_path / subtitle_dir / f"{langs[0]}1.vtt"
        )

        assert {i.full_path for i in movie_content.movie_subtitle.all()}.issubset(
            {str(i) for i in (tmp_path / subtitle_dir).iterdir()}
        )

        assert movie_content.movie_subtitle.last().full_path == str(
            tmp_path / subtitle_dir / "org1.vtt"
        )
Esempio n. 13
0
    def test_sub_functions_are_called(self, tmp_path: PosixPath,
                                      mocker: MockerFixture) -> None:
        mocker.patch.object(settings, "MEDIA_FOLDER", str(tmp_path))
        mocker.patch("panel.tasks.torrent.fetch_subtitles.delay")
        mocker.patch("os.chmod")
        mocker.patch("panel.tasks.torrent.Client", MockClient)
        get_root_path = mocker.spy(panel.tasks.torrent, "_get_root_path")
        get_videos_from_folder = mocker.spy(panel.tasks.torrent,
                                            "get_videos_from_folder")
        process_videos = mocker.spy(panel.tasks.torrent, "_process_videos")
        run = mocker.patch("panel.tasks.torrent.subprocess.run", autospec=True)
        run.return_value = MockSubprocess(
            stdout=json.dumps(RAW_INFO).encode("UTF-8"))
        movie_content: MovieContent = MovieContentFactory()
        MovieFactory.create(movie_content=[movie_content])
        movie_content.full_path = str(tmp_path)
        movie_content.save()
        video: PosixPath = tmp_path / "video.mkv"
        video.touch()
        hashed_title: str = _hash(video.name)

        process_videos_in_folder(movie_content_id=movie_content.id,
                                 delete_original=True)

        get_root_path.assert_called_once_with(
            movie_content_id=movie_content.id)
        get_videos_from_folder.assert_called_once_with(root_path=str(tmp_path))
        process_videos.assert_called_once_with(videos={video},
                                               delete_original=True)
        os.chmod.assert_called_once_with(
            str((tmp_path / f"{hashed_title}.mp4")), 0o644)
        panel.tasks.torrent.fetch_subtitles.delay.assert_called_once_with(
            movie_content_id=movie_content.id,
            limit=5,
            delete_original=settings.DELETE_ORIGINAL_FILES,
        )
Esempio n. 14
0
def movie_content() -> MovieContent:
    return MovieContentFactory.create(is_ready=True, full_path="/path")
Esempio n. 15
0
    def test_if_full_path_is_a_dir(self, tmp_path: PosixPath) -> None:
        movie_content: MovieContent = MovieContentFactory()
        movie_content.full_path = str(tmp_path)
        movie_content.save()

        assert _get_root_path(movie_content.id) == str(tmp_path)