hbmartin commited on
Commit
c1a2fd8
·
1 Parent(s): b1fe428

implement HTMLParseError, additional typing

Browse files
pytube/captions.py CHANGED
@@ -26,7 +26,7 @@ class Caption:
26
  """Download the xml caption tracks."""
27
  return request.get(self.url)
28
 
29
- def generate_srt_captions(self):
30
  """Generate "SubRip Subtitle" captions.
31
 
32
  Takes the xml captions from :meth:`~pytube.Caption.xml_captions` and
@@ -49,7 +49,7 @@ class Caption:
49
  ms = "{:.3f}".format(frac).replace("0.", "")
50
  return time_fmt + ms
51
 
52
- def xml_caption_to_srt(self, xml_captions):
53
  """Convert xml caption tracks to "SubRip Subtitle (srt)".
54
 
55
  :param str xml_captions:
 
26
  """Download the xml caption tracks."""
27
  return request.get(self.url)
28
 
29
+ def generate_srt_captions(self) -> str:
30
  """Generate "SubRip Subtitle" captions.
31
 
32
  Takes the xml captions from :meth:`~pytube.Caption.xml_captions` and
 
49
  ms = "{:.3f}".format(frac).replace("0.", "")
50
  return time_fmt + ms
51
 
52
+ def xml_caption_to_srt(self, xml_captions: str) -> str:
53
  """Convert xml caption tracks to "SubRip Subtitle (srt)".
54
 
55
  :param str xml_captions:
pytube/exceptions.py CHANGED
@@ -42,3 +42,7 @@ class LiveStreamError(ExtractError):
42
 
43
  class VideoUnavailable(PytubeError):
44
  """Video is unavailable."""
 
 
 
 
 
42
 
43
  class VideoUnavailable(PytubeError):
44
  """Video is unavailable."""
45
+
46
+
47
+ class HTMLParseError(PytubeError):
48
+ """HTML could not be parsed"""
pytube/extract.py CHANGED
@@ -4,10 +4,10 @@ import json
4
  from collections import OrderedDict
5
 
6
  from html.parser import HTMLParser
7
- from typing import Any, Optional
8
  from urllib.parse import quote
9
  from urllib.parse import urlencode
10
- from pytube.exceptions import RegexMatchError
11
  from pytube.helpers import regex_search
12
 
13
 
@@ -37,6 +37,9 @@ class PytubeHTMLParser(HTMLParser):
37
  elif self.in_vid_descr:
38
  self.vid_descr += data
39
 
 
 
 
40
 
41
  def is_age_restricted(watch_html: str) -> bool:
42
  """Check if content is age restricted.
@@ -106,8 +109,7 @@ def video_info_url(
106
  :param str watch_url:
107
  A YouTube watch url.
108
  :param str watch_html:
109
- The html contents of the watch page.
110
- TODO: unused
111
  :param str embed_html:
112
  The html contents of the embed page (for age restricted videos).
113
  :param bool age_restricted:
@@ -154,7 +156,7 @@ def js_url(html: str, age_restricted: bool = False) -> str:
154
  return "https://youtube.com" + base_js
155
 
156
 
157
- def mime_type_codec(mime_type_codec):
158
  """Parse the type data.
159
 
160
  Breaks up the data in the ``type`` key of the manifest, which contains the
 
4
  from collections import OrderedDict
5
 
6
  from html.parser import HTMLParser
7
+ from typing import Any, Optional, Tuple, List
8
  from urllib.parse import quote
9
  from urllib.parse import urlencode
10
+ from pytube.exceptions import RegexMatchError, HTMLParseError
11
  from pytube.helpers import regex_search
12
 
13
 
 
37
  elif self.in_vid_descr:
38
  self.vid_descr += data
39
 
40
+ def error(self, message):
41
+ raise HTMLParseError(message)
42
+
43
 
44
  def is_age_restricted(watch_html: str) -> bool:
45
  """Check if content is age restricted.
 
109
  :param str watch_url:
110
  A YouTube watch url.
111
  :param str watch_html:
112
+ (Unused) The html contents of the watch page.
 
113
  :param str embed_html:
114
  The html contents of the embed page (for age restricted videos).
115
  :param bool age_restricted:
 
156
  return "https://youtube.com" + base_js
157
 
158
 
159
+ def mime_type_codec(mime_type_codec: str) -> Tuple[str, List[str]]:
160
  """Parse the type data.
161
 
162
  Breaks up the data in the ``type`` key of the manifest, which contains the
pytube/helpers.py CHANGED
@@ -135,7 +135,7 @@ def safe_filename(s: str, max_length: int = 255) -> str:
135
  return filename[:max_length].rsplit(" ", 0)[0]
136
 
137
 
138
- def create_logger(level=logging.ERROR):
139
  """Create a configured instance of logger.
140
 
141
  :param int level:
 
135
  return filename[:max_length].rsplit(" ", 0)[0]
136
 
137
 
138
+ def create_logger(level:int = logging.ERROR) -> logging.Logger:
139
  """Create a configured instance of logger.
140
 
141
  :param int level:
pytube/itags.py CHANGED
@@ -100,7 +100,7 @@ DASH_MP4_VIDEO = [133, 134, 135, 136, 137, 138, 160, 212, 264, 266, 298, 299]
100
  DASH_MP4_AUDIO = [139, 140, 141, 256, 258, 325, 328]
101
 
102
 
103
- def get_format_profile(itag):
104
  """Get additional format information for a given itag.
105
 
106
  :param str itag:
 
100
  DASH_MP4_AUDIO = [139, 140, 141, 256, 258, 325, 328]
101
 
102
 
103
+ def get_format_profile(itag: int) -> Dict:
104
  """Get additional format information for a given itag.
105
 
106
  :param str itag:
pytube/mixins.py CHANGED
@@ -130,7 +130,7 @@ def apply_descrambler(stream_data: Dict, key: str) -> None:
130
  )
131
 
132
 
133
- def install_proxy(proxy_handler):
134
  proxy_support = request.ProxyHandler(proxy_handler)
135
  opener = request.build_opener(proxy_support)
136
  request.install_opener(opener)
 
130
  )
131
 
132
 
133
+ def install_proxy(proxy_handler: Dict[str, str]) -> None:
134
  proxy_support = request.ProxyHandler(proxy_handler)
135
  opener = request.build_opener(proxy_support)
136
  request.install_opener(opener)