[youtube] fix extraction for embed restricted live streams(fixes #16433)
[youtube-dl] / youtube_dl / extractor / raywenderlich.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .vimeo import VimeoIE
7 from ..utils import (
8     extract_attributes,
9     ExtractorError,
10     smuggle_url,
11     unsmuggle_url,
12     urljoin,
13 )
14
15
16 class RayWenderlichIE(InfoExtractor):
17     _VALID_URL = r'https?://videos\.raywenderlich\.com/courses/(?P<course_id>[^/]+)/lessons/(?P<id>\d+)'
18
19     _TESTS = [{
20         'url': 'https://videos.raywenderlich.com/courses/105-testing-in-ios/lessons/1',
21         'info_dict': {
22             'id': '248377018',
23             'ext': 'mp4',
24             'title': 'Testing In iOS Episode 1: Introduction',
25             'duration': 133,
26             'uploader': 'Ray Wenderlich',
27             'uploader_id': 'user3304672',
28         },
29         'params': {
30             'noplaylist': True,
31             'skip_download': True,
32         },
33         'add_ie': [VimeoIE.ie_key()],
34         'expected_warnings': ['HTTP Error 403: Forbidden'],
35     }, {
36         'url': 'https://videos.raywenderlich.com/courses/105-testing-in-ios/lessons/1',
37         'info_dict': {
38             'title': 'Testing in iOS',
39             'id': '105-testing-in-ios',
40         },
41         'params': {
42             'noplaylist': False,
43         },
44         'playlist_count': 29,
45     }]
46
47     def _real_extract(self, url):
48         url, smuggled_data = unsmuggle_url(url, {})
49
50         mobj = re.match(self._VALID_URL, url)
51         course_id, lesson_id = mobj.group('course_id', 'id')
52         video_id = '%s/%s' % (course_id, lesson_id)
53
54         webpage = self._download_webpage(url, video_id)
55
56         no_playlist = self._downloader.params.get('noplaylist')
57         if no_playlist or smuggled_data.get('force_video', False):
58             if no_playlist:
59                 self.to_screen(
60                     'Downloading just video %s because of --no-playlist'
61                     % video_id)
62             if '>Subscribe to unlock' in webpage:
63                 raise ExtractorError(
64                     'This content is only available for subscribers',
65                     expected=True)
66             vimeo_id = self._search_regex(
67                 r'data-vimeo-id=["\'](\d+)', webpage, 'video id')
68             return self.url_result(
69                 VimeoIE._smuggle_referrer(
70                     'https://player.vimeo.com/video/%s' % vimeo_id, url),
71                 ie=VimeoIE.ie_key(), video_id=vimeo_id)
72
73         self.to_screen(
74             'Downloading playlist %s - add --no-playlist to just download video'
75             % course_id)
76
77         lesson_ids = set((lesson_id, ))
78         for lesson in re.findall(
79                 r'(<a[^>]+\bclass=["\']lesson-link[^>]+>)', webpage):
80             attrs = extract_attributes(lesson)
81             if not attrs:
82                 continue
83             lesson_url = attrs.get('href')
84             if not lesson_url:
85                 continue
86             lesson_id = self._search_regex(
87                 r'/lessons/(\d+)', lesson_url, 'lesson id', default=None)
88             if not lesson_id:
89                 continue
90             lesson_ids.add(lesson_id)
91
92         entries = []
93         for lesson_id in sorted(lesson_ids):
94             entries.append(self.url_result(
95                 smuggle_url(urljoin(url, lesson_id), {'force_video': True}),
96                 ie=RayWenderlichIE.ie_key()))
97
98         title = self._search_regex(
99             r'class=["\']course-title[^>]+>([^<]+)', webpage, 'course title',
100             default=None)
101
102         return self.playlist_result(entries, course_id, title)