File size: 3,370 Bytes
a0ca1c2 49627f0 1776b2f efe24a6 a0ca1c2 ee0deb6 a0ca1c2 d68bda8 a0ca1c2 d68bda8 a0ca1c2 06ecde8 4f06190 8f9dd79 06ecde8 49627f0 ee0deb6 49627f0 d68bda8 82321d6 d68bda8 49627f0 24c3a19 328e22b ee0deb6 d68bda8 ee0deb6 328e22b 24c3a19 328e22b 82321d6 328e22b 7f9dcc9 67e6144 1776b2f 67e6144 efe24a6 ee0deb6 efe24a6 e707f93 57b3c57 e0b8c49 d68bda8 e0b8c49 d68bda8 e0d67e2 8ff1168 e0d67e2 |
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 |
# -*- coding: utf-8 -*-
"""Unit tests for the :module:`extract <extract>` module."""
from datetime import datetime
import pytest
from pytube import extract
from pytube.exceptions import RegexMatchError
def test_extract_video_id():
url = "https://www.youtube.com/watch?v=2lAe1cqCOXo"
video_id = extract.video_id(url)
assert video_id == "2lAe1cqCOXo"
def test_info_url(age_restricted):
video_info_url = extract.video_info_url_age_restricted(
video_id="QRS8MkLhQmM", embed_html=age_restricted["embed_html"],
)
expected = (
"https://youtube.com/get_video_info?video_id=QRS8MkLhQmM&eurl"
"=https%3A%2F%2Fyoutube.googleapis.com%2Fv%2FQRS8MkLhQmM&sts="
)
assert video_info_url == expected
def test_info_url_age_restricted(cipher_signature):
video_info_url = extract.video_info_url(
video_id=cipher_signature.video_id,
watch_url=cipher_signature.watch_url,
)
expected = (
"https://youtube.com/get_video_info?video_id=2lAe1cqCOXo"
"&ps=default&eurl=https%253A%2F%2Fyoutube.com%2Fwatch%253Fv%"
"253D2lAe1cqCOXo&hl=en_US"
)
assert video_info_url == expected
def test_js_url(cipher_signature):
expected = (
"https://youtube.com/s/player/9b65e980/player_ias.vflset/en_US/base.js"
)
result = extract.js_url(cipher_signature.watch_html)
assert expected == result
def test_age_restricted(age_restricted):
assert extract.is_age_restricted(age_restricted["watch_html"])
def test_non_age_restricted(cipher_signature):
assert not extract.is_age_restricted(cipher_signature.watch_html)
def test_is_private(private):
assert extract.is_private(private['watch_html'])
def test_not_is_private(cipher_signature):
assert not extract.is_private(cipher_signature.watch_html)
def test_recording_available(cipher_signature):
assert extract.recording_available(cipher_signature.watch_html)
def test_publish_date(cipher_signature):
expected = datetime(2019, 12, 5)
assert cipher_signature.publish_date == expected
assert extract.publish_date('') is None
def test_not_recording_available(missing_recording):
assert not extract.recording_available(missing_recording['watch_html'])
def test_mime_type_codec():
mime_type, mime_subtype = extract.mime_type_codec(
'audio/webm; codecs="opus"'
)
assert mime_type == "audio/webm"
assert mime_subtype == ["opus"]
def test_mime_type_codec_with_no_match_should_error():
with pytest.raises(RegexMatchError):
extract.mime_type_codec("audio/webm")
def test_get_ytplayer_config_with_no_match_should_error():
with pytest.raises(RegexMatchError):
extract.get_ytplayer_config("")
def test_get_ytplayer_js_with_no_match_should_error():
with pytest.raises(RegexMatchError):
extract.get_ytplayer_js("")
def test_signature_cipher_does_not_error(stream_dict):
config_args = extract.get_ytplayer_config(stream_dict)['args']
extract.apply_descrambler(config_args, "url_encoded_fmt_stream_map")
assert "s" in config_args["url_encoded_fmt_stream_map"][0].keys()
def test_initial_data_missing():
with pytest.raises(RegexMatchError):
extract.initial_data('')
def test_initial_data(stream_dict):
initial_data = extract.initial_data(stream_dict)
assert 'contents' in initial_data
|