File size: 5,759 Bytes
eec6f64 e10b0fd a92dd5e e10b0fd a92dd5e 15fde2f 3783a2f 7ad4dc3 3783a2f eab63b5 15fde2f 7ad4dc3 15fde2f 7ad4dc3 15fde2f eab63b5 e10b0fd 272841b 15fde2f 7ad4dc3 15fde2f 7ad4dc3 15fde2f 85bb2e5 15fde2f 7ad4dc3 15fde2f 7ad4dc3 15fde2f 85bb2e5 272841b dfbebc5 3f026d1 f7b22d6 dfbebc5 ee0deb6 7ad4dc3 ee0deb6 dfbebc5 ee0deb6 eec6f64 ee0deb6 dfbebc5 84a96f3 3f026d1 f7b22d6 84a96f3 ee0deb6 7ad4dc3 ee0deb6 84a96f3 ee0deb6 eec6f64 ee0deb6 84a96f3 61a4b4e ee0deb6 7ad4dc3 ee0deb6 61a4b4e eec6f64 61a4b4e dfbebc5 3f026d1 f7b22d6 dfbebc5 ee0deb6 7ad4dc3 ee0deb6 dfbebc5 ee0deb6 eec6f64 ee0deb6 84a96f3 7ad4dc3 84a96f3 f1b9623 bfdd549 e10b0fd bfdd549 f1b9623 7ad4dc3 f1b9623 6400f70 7ad4dc3 6400f70 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import os
import pytest
from unittest import mock
from unittest.mock import MagicMock, mock_open, patch
from pytube import Caption, CaptionQuery, captions
def test_float_to_srt_time_format():
caption1 = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
assert caption1.float_to_srt_time_format(3.89) == "00:00:03,890"
def test_caption_query_sequence():
caption1 = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
caption2 = Caption(
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr", "vssId": ".fr"}
)
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):
assert caption_query["nada"] is not None
def test_caption_query_get_by_language_code_when_exists():
caption1 = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
caption2 = Caption(
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr", "vssId": ".fr"}
)
caption_query = CaptionQuery(captions=[caption1, caption2])
assert caption_query["en"] == caption1
def test_caption_query_get_by_language_code_when_not_exists():
caption1 = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
caption2 = Caption(
{"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr", "vssId": ".fr"}
)
caption_query = CaptionQuery(captions=[caption1, caption2])
with pytest.raises(KeyError):
assert caption_query["hello"] is not None
# assert not_found is not None # should never reach here
@mock.patch("pytube.captions.Caption.generate_srt_captions")
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",
"vssId": ".en"
}
)
caption.download("title")
assert (
open_mock.call_args_list[0][0][0].split(os.path.sep)[-1] == "title (en).srt"
)
@mock.patch("pytube.captions.Caption.generate_srt_captions")
def test_download_with_prefix(srt):
open_mock = mock_open()
with patch("builtins.open", open_mock):
srt.return_value = ""
caption = Caption(
{
"url": "url1",
"name": {"simpleText": "name1"},
"languageCode": "en",
"vssId": ".en"
}
)
caption.download("title", filename_prefix="1 ")
assert (
open_mock.call_args_list[0][0][0].split(os.path.sep)[-1]
== "1 title (en).srt"
)
@mock.patch("pytube.captions.Caption.generate_srt_captions")
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",
"vssId": ".en"
}
)
file_path = caption.download("title", output_path="blah")
assert file_path == os.path.join("/target","title (en).srt")
captions.target_directory.assert_called_with("blah")
@mock.patch("pytube.captions.Caption.xml_captions")
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",
"vssId": ".en"
}
)
caption.download("title.xml", srt=False)
assert (
open_mock.call_args_list[0][0][0].split(os.path.sep)[-1] == "title (en).xml"
)
def test_repr():
caption = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
assert str(caption) == '<Caption lang="name1" code="en">'
caption_query = CaptionQuery(captions=[caption])
assert repr(caption_query) == '{\'en\': <Caption lang="name1" code="en">}'
@mock.patch("pytube.request.get")
def test_xml_captions(request_get):
request_get.return_value = "test"
caption = Caption(
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en", "vssId": ".en"}
)
assert caption.xml_captions == "test"
@mock.patch("pytube.captions.request")
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", "vssId": ".en"}
)
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"
"如要啓動字幕,請按一下這裡的圖示。"
)
|