File: test_youtube.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (103 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download | duplicates (4)
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
from unittest.mock import Mock

import pytest

from streamlink.plugins.youtube import YouTube
from tests.plugins import PluginCanHandleUrl


class TestPluginCanHandleUrlYouTube(PluginCanHandleUrl):
    __plugin__ = YouTube

    should_match_groups = [
        (
            ("default", "https://www.youtube.com/v/aqz-KE-bpKQ"),
            {"video_id": "aqz-KE-bpKQ"},
        ),
        (
            ("default", "https://www.youtube.com/live/aqz-KE-bpKQ"),
            {"video_id": "aqz-KE-bpKQ"},
        ),
        (
            ("default", "https://www.youtube.com/watch?foo=bar&baz=qux&v=aqz-KE-bpKQ&asdf=1234"),
            {"video_id": "aqz-KE-bpKQ"},
        ),
        (
            ("channel", "https://www.youtube.com/CHANNELNAME"),
            {"channel": "CHANNELNAME"},
        ),
        (
            ("channel", "https://www.youtube.com/CHANNELNAME/live"),
            {"channel": "CHANNELNAME", "live": "/live"},
        ),
        (
            ("channel", "https://www.youtube.com/@CHANNELNAME"),
            {"channel": "CHANNELNAME"},
        ),
        (
            ("channel", "https://www.youtube.com/@CHANNELNAME/live"),
            {"channel": "CHANNELNAME", "live": "/live"},
        ),
        (
            ("channel", "https://www.youtube.com/c/CHANNELNAME"),
            {"channel": "CHANNELNAME"},
        ),
        (
            ("channel", "https://www.youtube.com/c/CHANNELNAME/live"),
            {"channel": "CHANNELNAME", "live": "/live"},
        ),
        (
            ("channel", "https://www.youtube.com/channel/CHANNELID"),
            {"channel": "CHANNELID"},
        ),
        (
            ("channel", "https://www.youtube.com/channel/CHANNELID/live"),
            {"channel": "CHANNELID", "live": "/live"},
        ),
        (
            ("channel", "https://www.youtube.com/user/CHANNELNAME"),
            {"channel": "CHANNELNAME"},
        ),
        (
            ("channel", "https://www.youtube.com/user/CHANNELNAME/live"),
            {"channel": "CHANNELNAME", "live": "/live"},
        ),
        (
            ("embed", "https://www.youtube.com/embed/aqz-KE-bpKQ"),
            {"video_id": "aqz-KE-bpKQ"},
        ),
        (
            ("embed", "https://www.youtube.com/embed/live_stream?channel=CHANNELNAME"),
            {"live": "CHANNELNAME"},
        ),
        (
            ("shorthand", "https://youtu.be/aqz-KE-bpKQ"),
            {"video_id": "aqz-KE-bpKQ"},
        ),
    ]

    should_not_match = [
        "https://accounts.google.com/",
        "https://www.youtube.com",
        "https://www.youtube.com/",
        "https://www.youtube.com/feed/guide_builder",
        "https://www.youtube.com/t/terms",
        "https://youtu.be",
        "https://youtu.be/",
        "https://youtu.be/c/CHANNELNAME",
        "https://youtu.be/c/CHANNELNAME/live",
    ]


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("http://gaming.youtube.com/watch?v=0123456789A", "https://www.youtube.com/watch?v=0123456789A"),
        ("http://youtu.be/0123456789A", "https://www.youtube.com/watch?v=0123456789A"),
        ("http://youtube.com/embed/0123456789A", "https://www.youtube.com/watch?v=0123456789A"),
        ("http://youtube.com/embed/live_stream?channel=CHANNELID", "https://www.youtube.com/channel/CHANNELID/live"),
        ("http://www.youtube.com/watch?v=0123456789A", "https://www.youtube.com/watch?v=0123456789A"),
    ],
)
def test_translate_url(url, expected):
    assert YouTube(Mock(), url).url == expected