mypy typing for a few functions
Browse files- pytube/__main__.py +2 -2
- pytube/cipher.py +1 -1
- pytube/cli.py +3 -3
- pytube/exceptions.py +1 -1
- pytube/extract.py +13 -8
- pytube/helpers.py +1 -1
- pytube/itags.py +1 -1
- pytube/mixins.py +4 -3
pytube/__main__.py
CHANGED
@@ -32,7 +32,7 @@ class YouTube(object):
|
|
32 |
|
33 |
def __init__(
|
34 |
self,
|
35 |
-
url
|
36 |
defer_prefetch_init=False,
|
37 |
on_progress_callback=None,
|
38 |
on_complete_callback=None,
|
@@ -185,7 +185,7 @@ class YouTube(object):
|
|
185 |
self.js_url = extract.js_url(self.watch_html, self.age_restricted)
|
186 |
self.js = request.get(self.js_url)
|
187 |
|
188 |
-
def initialize_stream_objects(self, fmt):
|
189 |
"""Convert manifest data to instances of :class:`Stream <Stream>`.
|
190 |
|
191 |
Take the unscrambled stream data and uses it to initialize
|
|
|
32 |
|
33 |
def __init__(
|
34 |
self,
|
35 |
+
url,
|
36 |
defer_prefetch_init=False,
|
37 |
on_progress_callback=None,
|
38 |
on_complete_callback=None,
|
|
|
185 |
self.js_url = extract.js_url(self.watch_html, self.age_restricted)
|
186 |
self.js = request.get(self.js_url)
|
187 |
|
188 |
+
def initialize_stream_objects(self, fmt: str):
|
189 |
"""Convert manifest data to instances of :class:`Stream <Stream>`.
|
190 |
|
191 |
Take the unscrambled stream data and uses it to initialize
|
pytube/cipher.py
CHANGED
@@ -240,7 +240,7 @@ def parse_function(js_func):
|
|
240 |
return regex_search(r"\w+\.(\w+)\(\w,(\d+)\)", js_func, groups=True)
|
241 |
|
242 |
|
243 |
-
def get_signature(js, ciphered_signature):
|
244 |
"""Decipher the signature.
|
245 |
|
246 |
Taking the ciphered signature, applies the transform functions.
|
|
|
240 |
return regex_search(r"\w+\.(\w+)\(\w,(\d+)\)", js_func, groups=True)
|
241 |
|
242 |
|
243 |
+
def get_signature(js: str, ciphered_signature: str) -> str:
|
244 |
"""Decipher the signature.
|
245 |
|
246 |
Taking the ciphered signature, applies the transform functions.
|
pytube/cli.py
CHANGED
@@ -66,7 +66,7 @@ def main():
|
|
66 |
download(args.url, args.itag)
|
67 |
|
68 |
|
69 |
-
def build_playback_report(url):
|
70 |
"""Serialize the request data to json for offline debugging.
|
71 |
|
72 |
:param str url:
|
@@ -150,7 +150,7 @@ def on_progress(stream, chunk, file_handle, bytes_remaining):
|
|
150 |
display_progress_bar(bytes_received, filesize)
|
151 |
|
152 |
|
153 |
-
def download(url, itag):
|
154 |
"""Start downloading a YouTube video.
|
155 |
|
156 |
:param str url:
|
@@ -171,7 +171,7 @@ def download(url, itag):
|
|
171 |
sys.exit()
|
172 |
|
173 |
|
174 |
-
def display_streams(url):
|
175 |
"""Probe YouTube video and lists its available formats.
|
176 |
|
177 |
:param str url:
|
|
|
66 |
download(args.url, args.itag)
|
67 |
|
68 |
|
69 |
+
def build_playback_report(url: str):
|
70 |
"""Serialize the request data to json for offline debugging.
|
71 |
|
72 |
:param str url:
|
|
|
150 |
display_progress_bar(bytes_received, filesize)
|
151 |
|
152 |
|
153 |
+
def download(url: str, itag: str):
|
154 |
"""Start downloading a YouTube video.
|
155 |
|
156 |
:param str url:
|
|
|
171 |
sys.exit()
|
172 |
|
173 |
|
174 |
+
def display_streams(url: str):
|
175 |
"""Probe YouTube video and lists its available formats.
|
176 |
|
177 |
:param str url:
|
pytube/exceptions.py
CHANGED
@@ -15,7 +15,7 @@ class PytubeError(Exception):
|
|
15 |
class ExtractError(PytubeError):
|
16 |
"""Data extraction based exception."""
|
17 |
|
18 |
-
def __init__(self, msg, video_id=None):
|
19 |
"""Construct an instance of a :class:`ExtractError <ExtractError>`.
|
20 |
|
21 |
:param str msg:
|
|
|
15 |
class ExtractError(PytubeError):
|
16 |
"""Data extraction based exception."""
|
17 |
|
18 |
+
def __init__(self, msg: str, video_id: str = None):
|
19 |
"""Construct an instance of a :class:`ExtractError <ExtractError>`.
|
20 |
|
21 |
:param str msg:
|
pytube/extract.py
CHANGED
@@ -37,7 +37,7 @@ class PytubeHTMLParser(HTMLParser):
|
|
37 |
self.vid_descr += data
|
38 |
|
39 |
|
40 |
-
def is_age_restricted(watch_html):
|
41 |
"""Check if content is age restricted.
|
42 |
|
43 |
:param str watch_html:
|
@@ -53,7 +53,7 @@ def is_age_restricted(watch_html):
|
|
53 |
return True
|
54 |
|
55 |
|
56 |
-
def video_id(url):
|
57 |
"""Extract the ``video_id`` from a YouTube url.
|
58 |
|
59 |
This function supports the following patterns:
|
@@ -71,7 +71,7 @@ def video_id(url):
|
|
71 |
return regex_search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url, group=1)
|
72 |
|
73 |
|
74 |
-
def watch_url(video_id):
|
75 |
"""Construct a sanitized YouTube watch url, given a video id.
|
76 |
|
77 |
:param str video_id:
|
@@ -83,17 +83,21 @@ def watch_url(video_id):
|
|
83 |
return "https://youtube.com/watch?v=" + video_id
|
84 |
|
85 |
|
86 |
-
def embed_url(video_id):
|
87 |
return "https://www.youtube.com/embed/{}".format(video_id)
|
88 |
|
89 |
|
90 |
-
def eurl(video_id):
|
91 |
return "https://youtube.googleapis.com/v/{}".format(video_id)
|
92 |
|
93 |
|
94 |
def video_info_url(
|
95 |
-
video_id
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
"""Construct the video_info url.
|
98 |
|
99 |
:param str video_id:
|
@@ -102,6 +106,7 @@ def video_info_url(
|
|
102 |
A YouTube watch url.
|
103 |
:param str watch_html:
|
104 |
The html contents of the watch page.
|
|
|
105 |
:param str embed_html:
|
106 |
The html contents of the embed page (for age restricted videos).
|
107 |
:param bool age_restricted:
|
@@ -131,7 +136,7 @@ def video_info_url(
|
|
131 |
return "https://youtube.com/get_video_info?" + urlencode(params)
|
132 |
|
133 |
|
134 |
-
def js_url(html, age_restricted=False):
|
135 |
"""Get the base JavaScript url.
|
136 |
|
137 |
Construct the base JavaScript url, which contains the decipher
|
|
|
37 |
self.vid_descr += data
|
38 |
|
39 |
|
40 |
+
def is_age_restricted(watch_html: str) -> bool:
|
41 |
"""Check if content is age restricted.
|
42 |
|
43 |
:param str watch_html:
|
|
|
53 |
return True
|
54 |
|
55 |
|
56 |
+
def video_id(url: str) -> str:
|
57 |
"""Extract the ``video_id`` from a YouTube url.
|
58 |
|
59 |
This function supports the following patterns:
|
|
|
71 |
return regex_search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url, group=1)
|
72 |
|
73 |
|
74 |
+
def watch_url(video_id: str) -> str:
|
75 |
"""Construct a sanitized YouTube watch url, given a video id.
|
76 |
|
77 |
:param str video_id:
|
|
|
83 |
return "https://youtube.com/watch?v=" + video_id
|
84 |
|
85 |
|
86 |
+
def embed_url(video_id: str) -> str:
|
87 |
return "https://www.youtube.com/embed/{}".format(video_id)
|
88 |
|
89 |
|
90 |
+
def eurl(video_id: str) -> str:
|
91 |
return "https://youtube.googleapis.com/v/{}".format(video_id)
|
92 |
|
93 |
|
94 |
def video_info_url(
|
95 |
+
video_id: str,
|
96 |
+
watch_url: str,
|
97 |
+
watch_html: str,
|
98 |
+
embed_html: str,
|
99 |
+
age_restricted: bool,
|
100 |
+
) -> str:
|
101 |
"""Construct the video_info url.
|
102 |
|
103 |
:param str video_id:
|
|
|
106 |
A YouTube watch url.
|
107 |
:param str watch_html:
|
108 |
The html contents of the watch page.
|
109 |
+
TODO: unused
|
110 |
:param str embed_html:
|
111 |
The html contents of the embed page (for age restricted videos).
|
112 |
:param bool age_restricted:
|
|
|
136 |
return "https://youtube.com/get_video_info?" + urlencode(params)
|
137 |
|
138 |
|
139 |
+
def js_url(html: str, age_restricted: bool = False) -> str:
|
140 |
"""Get the base JavaScript url.
|
141 |
|
142 |
Construct the base JavaScript url, which contains the decipher
|
pytube/helpers.py
CHANGED
@@ -91,7 +91,7 @@ def apply_mixin(dct, key, func, *args, **kwargs):
|
|
91 |
dct[key] = func(dct[key], *args, **kwargs)
|
92 |
|
93 |
|
94 |
-
def safe_filename(s, max_length=255):
|
95 |
"""Sanitize a string making it safe to use as a filename.
|
96 |
|
97 |
This function was based off the limitations outlined here:
|
|
|
91 |
dct[key] = func(dct[key], *args, **kwargs)
|
92 |
|
93 |
|
94 |
+
def safe_filename(s: str, max_length: int = 255) -> str:
|
95 |
"""Sanitize a string making it safe to use as a filename.
|
96 |
|
97 |
This function was based off the limitations outlined here:
|
pytube/itags.py
CHANGED
@@ -98,7 +98,7 @@ LIVE = [91, 92, 93, 94, 95, 96, 132, 151]
|
|
98 |
|
99 |
|
100 |
def get_format_profile(itag):
|
101 |
-
"""Get
|
102 |
|
103 |
:param str itag:
|
104 |
YouTube format identifier code.
|
|
|
98 |
|
99 |
|
100 |
def get_format_profile(itag):
|
101 |
+
"""Get additional format information for a given itag.
|
102 |
|
103 |
:param str itag:
|
104 |
YouTube format identifier code.
|
pytube/mixins.py
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
import json
|
5 |
import logging
|
6 |
import pprint
|
|
|
7 |
|
8 |
from pytube import cipher
|
9 |
from urllib import request
|
@@ -16,7 +17,7 @@ from pytube.exceptions import LiveStreamError
|
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
18 |
|
19 |
-
def apply_signature(config_args, fmt, js):
|
20 |
"""Apply the decrypted signature to the stream manifest.
|
21 |
|
22 |
:param dict config_args:
|
@@ -66,14 +67,14 @@ def apply_signature(config_args, fmt, js):
|
|
66 |
stream_manifest[i]["url"] = url + "&sig=" + signature
|
67 |
|
68 |
|
69 |
-
def apply_descrambler(stream_data, key):
|
70 |
"""Apply various in-place transforms to YouTube's media stream data.
|
71 |
|
72 |
Creates a ``list`` of dictionaries by string splitting on commas, then
|
73 |
taking each list item, parsing it as a query string, converting it to a
|
74 |
``dict`` and unquoting the value.
|
75 |
|
76 |
-
:param dict
|
77 |
Dictionary containing query string encoded values.
|
78 |
:param str key:
|
79 |
Name of the key in dictionary.
|
|
|
4 |
import json
|
5 |
import logging
|
6 |
import pprint
|
7 |
+
from typing import Dict
|
8 |
|
9 |
from pytube import cipher
|
10 |
from urllib import request
|
|
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
19 |
|
20 |
+
def apply_signature(config_args: Dict, fmt: str, js: str):
|
21 |
"""Apply the decrypted signature to the stream manifest.
|
22 |
|
23 |
:param dict config_args:
|
|
|
67 |
stream_manifest[i]["url"] = url + "&sig=" + signature
|
68 |
|
69 |
|
70 |
+
def apply_descrambler(stream_data: Dict, key: str):
|
71 |
"""Apply various in-place transforms to YouTube's media stream data.
|
72 |
|
73 |
Creates a ``list`` of dictionaries by string splitting on commas, then
|
74 |
taking each list item, parsing it as a query string, converting it to a
|
75 |
``dict`` and unquoting the value.
|
76 |
|
77 |
+
:param dict stream_data:
|
78 |
Dictionary containing query string encoded values.
|
79 |
:param str key:
|
80 |
Name of the key in dictionary.
|