nficano commited on
Commit
f2d9cda
·
1 Parent(s): af737f9

added age restriction error

Browse files
Files changed (3) hide show
  1. pytube/__main__.py +3 -0
  2. pytube/exceptions.py +4 -0
  3. pytube/extract.py +18 -2
pytube/__main__.py CHANGED
@@ -18,6 +18,7 @@ from pytube import request
18
  from pytube import Stream
19
  from pytube import StreamQuery
20
  from pytube.compat import parse_qsl
 
21
  from pytube.helpers import apply_mixin
22
 
23
 
@@ -125,6 +126,8 @@ class YouTube(object):
125
 
126
  """
127
  self.watch_html = request.get(url=self.watch_url)
 
 
128
  self.vid_info_url = extract.video_info_url(
129
  video_id=self.video_id,
130
  watch_url=self.watch_url,
 
18
  from pytube import Stream
19
  from pytube import StreamQuery
20
  from pytube.compat import parse_qsl
21
+ from pytube.exceptions import AgeRestrictionError
22
  from pytube.helpers import apply_mixin
23
 
24
 
 
126
 
127
  """
128
  self.watch_html = request.get(url=self.watch_url)
129
+ if extract.is_age_restricted(self.watch_html):
130
+ raise AgeRestrictionError('Content is age restricted')
131
  self.vid_info_url = extract.video_info_url(
132
  video_id=self.video_id,
133
  watch_url=self.watch_url,
pytube/exceptions.py CHANGED
@@ -59,3 +59,7 @@ class ExtractError(PytubeError):
59
 
60
  class RegexMatchError(ExtractError):
61
  """Regex pattern did not return any matches."""
 
 
 
 
 
59
 
60
  class RegexMatchError(ExtractError):
61
  """Regex pattern did not return any matches."""
62
+
63
+
64
+ class AgeRestrictionError(ExtractError):
65
+ """Content is age restricted."""
pytube/extract.py CHANGED
@@ -5,9 +5,26 @@ from collections import OrderedDict
5
 
6
  from pytube.compat import quote
7
  from pytube.compat import urlencode
 
8
  from pytube.helpers import regex_search
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def video_id(url):
12
  """Extract the ``video_id`` from a YouTube url.
13
 
@@ -50,8 +67,7 @@ def video_info_url(video_id, watch_url, watch_html):
50
  """
51
  # I'm not entirely sure what ``t`` represents. Looks to represent a
52
  # boolean.
53
- pattern = r'\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]'
54
- t = regex_search(pattern, watch_html, group=0)
55
  # Here we use ``OrderedDict`` so that the output is consistant between
56
  # Python 2.7+.
57
  params = OrderedDict([
 
5
 
6
  from pytube.compat import quote
7
  from pytube.compat import urlencode
8
+ from pytube.exceptions import RegexMatchError
9
  from pytube.helpers import regex_search
10
 
11
 
12
+ def is_age_restricted(watch_html):
13
+ """Check if content is age restricted.
14
+
15
+ :param str watch_html:
16
+ The html contents of the watch page.
17
+ :rtype: bool
18
+ :returns:
19
+ Whether or not the content is age restricted.
20
+ """
21
+ try:
22
+ regex_search(r'og:restrictions:age', watch_html, group=0)
23
+ except RegexMatchError:
24
+ return False
25
+ return True
26
+
27
+
28
  def video_id(url):
29
  """Extract the ``video_id`` from a YouTube url.
30
 
 
67
  """
68
  # I'm not entirely sure what ``t`` represents. Looks to represent a
69
  # boolean.
70
+ t = regex_search(r'\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]', watch_html, group=0)
 
71
  # Here we use ``OrderedDict`` so that the output is consistant between
72
  # Python 2.7+.
73
  params = OrderedDict([