Example #1
0
def test_float_to_srt_time_format():
    caption1 = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    assert caption1.float_to_srt_time_format(3.89) == "00:00:03,890"
Example #2
0
def test_download_caption_with_language_found(youtube_item):
    youtube_item.title = "video title"
    caption = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    caption.download = MagicMock(return_value="file_path")
    youtube_item.captions = CaptionQuery([caption])
    cli.download_caption(youtube_item, "en")
    caption.download.assert_called_with(title="video title", output_path=None)
Example #3
0
def test_download(srt):
    open_mock = mock_open()
    with patch("builtins.open", open_mock):
        srt.return_value = ""
        caption = Caption(
            {
                "url": "url1",
                "name": {
                    "simpleText": "name1"
                },
                "languageCode": "en"
            }, None)
        caption.download("title")
        assert open_mock.call_args_list[0][0][0].split(
            "/")[-1] == "title (en).srt"
Example #4
0
def test_download_xml_and_trim_extension(xml):
    open_mock = mock_open()
    with patch("builtins.open", open_mock):
        xml.return_value = ""
        caption = Caption(
            {
                "url": "url1",
                "name": {
                    "simpleText": "name1"
                },
                "languageCode": "en"
            }, None)
        caption.download("title.xml", srt=False)
        assert open_mock.call_args_list[0][0][0].split(
            "/")[-1] == "title (en).xml"
Example #5
0
def test_download_with_output_path(srt):
    open_mock = mock_open()
    captions.target_directory = MagicMock(return_value="/target")
    with patch("builtins.open", open_mock):
        srt.return_value = ""
        caption = Caption(
            {
                "url": "url1",
                "name": {
                    "simpleText": "name1"
                },
                "languageCode": "en"
            }, None)
        file_path = caption.download("title", output_path="blah")
        assert file_path == "/target/title (en).srt"
        captions.target_directory.assert_called_with("blah")
Example #6
0
    def caption_tracks(self) -> List[Caption]:
        """Get a list of :class:`Caption <Caption>`.

        :rtype: List[Caption]
        """
        raw_tracks = (self.player_response.get("captions", {}).get(
            "playerCaptionsTracklistRenderer", {}).get("captionTracks", []))
        return [Caption(track, self.request_headers) for track in raw_tracks]
Example #7
0
def test_caption_query_get_by_language_code_when_exists():
    caption1 = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    caption2 = Caption(
        {
            "url": "url2",
            "name": {
                "simpleText": "name2"
            },
            "languageCode": "fr"
        }, None)
    caption_query = CaptionQuery(captions=[caption1, caption2])
    assert caption_query["en"] == caption1
Example #8
0
def test_xml_captions(request_get):
    request_get.return_value = "test"
    caption = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    assert caption.xml_captions == "test"
Example #9
0
def test_caption_query_get_by_language_code_when_not_exists():
    caption1 = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    caption2 = Caption(
        {
            "url": "url2",
            "name": {
                "simpleText": "name2"
            },
            "languageCode": "fr"
        }, None)
    caption_query = CaptionQuery(captions=[caption1, caption2])
    with pytest.raises(KeyError):
        not_found = caption_query["hello"]
        assert not_found is not None  # should never reach here
Example #10
0
def test_generate_srt_captions(request):
    request.get.return_value = (
        '<?xml version="1.0" encoding="utf-8" ?><transcript><text start="6.5" dur="1.7">['
        'Herb, Software Engineer]\n本影片包含隱藏式字幕。</text><text start="8.3" dur="2.7">'
        "如要啓動字幕,請按一下這裡的圖示。</text></transcript>")
    caption = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    assert caption.generate_srt_captions() == (
        "1\n"
        "00:00:06,500 --> 00:00:08,200\n"
        "[Herb, Software Engineer] 本影片包含隱藏式字幕。\n"
        "\n"
        "2\n"
        "00:00:08,300 --> 00:00:11,000\n"
        "如要啓動字幕,請按一下這裡的圖示。")
Example #11
0
def test_repr():
    caption = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    assert str(caption) == '<Caption lang="name1" code="en">'

    caption_query = CaptionQuery(captions=[caption])
    assert repr(caption_query) == '{\'en\': <Caption lang="name1" code="en">}'
Example #12
0
def test_caption_query_sequence():
    caption1 = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    caption2 = Caption(
        {
            "url": "url2",
            "name": {
                "simpleText": "name2"
            },
            "languageCode": "fr"
        }, None)
    caption_query = CaptionQuery(captions=[caption1, caption2])
    assert len(caption_query) == 2
    assert caption_query["en"] == caption1
    assert caption_query["fr"] == caption2
    with pytest.raises(KeyError):
        not_exists = caption_query["nada"]
        assert not_exists is not None  # should never reach this
Example #13
0
def test_download_caption_with_none(youtube_item, print_available):
    # Given
    caption = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    youtube_item.captions = CaptionQuery([caption])
    # When
    cli.download_caption(youtube_item, None)
    # Then
    print_available.assert_called_with(youtube_item.captions)
Example #14
0
def test_print_available_captions(capsys):
    # Given
    caption1 = Caption(
        {
            "url": "url1",
            "name": {
                "simpleText": "name1"
            },
            "languageCode": "en"
        }, None)
    caption2 = Caption(
        {
            "url": "url2",
            "name": {
                "simpleText": "name2"
            },
            "languageCode": "fr"
        }, None)
    query = CaptionQuery([caption1, caption2])
    # When
    cli._print_available_captions(query)
    # Then
    captured = capsys.readouterr()
    assert captured.out == "Available caption codes are: en, fr\n"