def caption_to_srt(request): url = request.GET.get('caption_url') caption = Caption({"baseUrl": url, "name": {"simpleText": "a"}, "languageCode": "b"}) response = HttpResponse(content_type='text/srt') response['Content-Disposition'] = 'attachment; filename="caption.srt"' response.write(caption.generate_srt_captions()) return response
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"} ) caption.download("title.xml", srt=False) assert open_mock.call_args_list[0][0][0].split("/")[-1] == "title (en).xml"
def test_float_to_srt_time_format(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) assert caption1.float_to_srt_time_format(3.89) == "00:00:03,890"
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"} ) caption.download("title", filename_prefix="1 ") assert open_mock.call_args_list[0][0][0].split("/")[-1] == "1 title (en).srt"
def test_caption_query_get_by_language_code_when_not_exists(): caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) caption_query = CaptionQuery(captions=[caption1, caption2]) assert caption_query.get_by_language_code("hello") is None
def test_caption_query_all(): caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) caption_query = CaptionQuery(captions=[caption1, caption2]) assert caption_query.captions == [caption1, caption2]
def test_download_caption_with_language_found(youtube): youtube.title = "video title" caption = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption.download = MagicMock(return_value="file_path") youtube.captions = CaptionQuery([caption]) cli.download_caption(youtube, "en") caption.download.assert_called_with(title="video title", output_path=None)
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"} ) file_path = caption.download("title", output_path="blah") assert file_path == "/target/title (en).srt" captions.target_directory.assert_called_with("blah")
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", }) caption.download("title") assert (open_mock.call_args_list[0][0][0].split( os.path.sep)[-1] == "title (en).srt")
def test_print_available_captions(capsys): # Given caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) query = CaptionQuery([caption1, caption2]) # When cli._print_available_captions(query) # Then captured = capsys.readouterr() assert captured.out == "Available caption codes are: en, fr\n"
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) for track in raw_tracks]
def test_caption_query_get_by_language_code_when_not_exists(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) caption2 = Caption({ "url": "url2", "name": { "simpleText": "name2" }, "languageCode": "fr" }) caption_query = CaptionQuery(captions=[caption1, caption2]) with pytest.raises(KeyError): assert caption_query["hello"] is not None
def test_repr(): caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) assert str(caption) == '<Caption lang="name1" code="en">'
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_xml_captions(request_get): request_get.return_value = "test" caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) assert caption.xml_captions == "test"
def test_download_caption_with_lang_not_found(youtube, print_available): # Given caption = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) youtube.captions = CaptionQuery([caption]) # When cli.download_caption(youtube, "blah") # Then print_available.assert_called_with(youtube.captions)
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" }) 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" "如要啓動字幕,請按一下這裡的圖示。")
def test_caption_query_sequence(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) caption2 = Caption({ "url": "url2", "name": { "simpleText": "name2" }, "languageCode": "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_repr(): caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) assert str(caption) == '<Caption lang="name1" code="en">' caption_query = CaptionQuery(captions=[caption]) assert repr(caption_query) == '{\'en\': <Caption lang="name1" code="en">}'
def test_download_caption_with_language_not_found(youtube): caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) youtube.captions = CaptionQuery([caption]) with patch.object(youtube.captions, "all", wraps=youtube.captions.all) as wrapped_all: cli.download_caption(youtube, "blah") wrapped_all.assert_called()
def initialize_caption_objects(self): """Populate instances of :class:`Caption <Caption>`. Take the unscrambled player response data, and use it to initialize instances of :class:`Caption <Caption>`. :rtype: None """ if 'captions' not in self.player_config_args['player_response']: return # https://github.com/nficano/pytube/issues/167 caption_tracks = (self.player_config_args.get( 'player_response', {}).get('captions', {}).get('playerCaptionsTracklistRenderer', {}).get('captionTracks', [])) for caption_track in caption_tracks: self.caption_tracks.append(Caption(caption_track))
def initialize_caption_objects(self): """Populate instances of :class:`Caption <Caption>`. Take the unscrambled player response data, and use it to initialize instances of :class:`Caption <Caption>`. :rtype: None """ if 'captions' not in self.player_config['args']['player_response']: return caption_tracks = ( self.player_config['args']['player_response']['captions'] ['playerCaptionsTracklistRenderer']['captionTracks']) for caption_track in caption_tracks: self.caption_tracks.append(Caption(caption_track))