[vier] Extract more info
[youtube-dl] / youtube_dl / extractor / vier.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import itertools
6
7 from .common import InfoExtractor
8 from ..utils import (
9     urlencode_postdata,
10     int_or_none,
11     unified_strdate,
12 )
13
14
15 class VierIE(InfoExtractor):
16     IE_NAME = 'vier'
17     IE_DESC = 'vier.be and vijf.be'
18     _VALID_URL = r'https?://(?:www\.)?(?P<site>vier|vijf)\.be/(?:[^/]+/videos/(?P<display_id>[^/]+)(?:/(?P<id>\d+))?|video/v3/embed/(?P<embed_id>\d+))'
19     _NETRC_MACHINE = 'vier'
20     _TESTS = [{
21         'url': 'http://www.vier.be/planb/videos/het-wordt-warm-de-moestuin/16129',
22         'md5': 'e4ae2054a6b040ef1e289e20d111b46e',
23         'info_dict': {
24             'id': '16129',
25             'display_id': 'het-wordt-warm-de-moestuin',
26             'ext': 'mp4',
27             'title': 'Het wordt warm in De Moestuin',
28             'description': 'De vele uren werk eisen hun tol. Wim droomt van assistentie...',
29             'upload_date': '20121025',
30         },
31     }, {
32         'url': 'http://www.vijf.be/temptationisland/videos/zo-grappig-temptation-island-hosts-moeten-kiezen-tussen-onmogelijke-dilemmas/2561614',
33         'info_dict': {
34             'id': '2561614',
35             'display_id': 'zo-grappig-temptation-island-hosts-moeten-kiezen-tussen-onmogelijke-dilemmas',
36             'ext': 'mp4',
37             'title': 'md5:84f45fe48b8c1fa296a7f6d208d080a7',
38             'description': 'md5:0356d4981e58b8cbee19355cbd51a8fe',
39             'upload_date': '20170228',
40         },
41         'params': {
42             'skip_download': True,
43         },
44     }, {
45         'url': 'http://www.vier.be/janigaat/videos/jani-gaat-naar-tokio-aflevering-4/2674839',
46         'info_dict': {
47             'id': '2674839',
48             'display_id': 'jani-gaat-naar-tokio-aflevering-4',
49             'ext': 'mp4',
50             'title': 'Jani gaat naar Tokio - Aflevering 4',
51             'description': 'md5:aa8d611541db6ae9e863125704511f88',
52             'upload_date': '20170501',
53             'episode_number': 4,
54         },
55         'params': {
56             'skip_download': True,
57         },
58         'skip': 'Requires account credentials',
59     }, {
60         # Requires account credentials but bypassed extraction via v3/embed page
61         # without metadata
62         'url': 'http://www.vier.be/janigaat/videos/jani-gaat-naar-tokio-aflevering-4/2674839',
63         'info_dict': {
64             'id': '2674839',
65             'display_id': 'jani-gaat-naar-tokio-aflevering-4',
66             'ext': 'mp4',
67             'title': 'jani-gaat-naar-tokio-aflevering-4',
68         },
69         'params': {
70             'skip_download': True,
71         },
72         'expected_warnings': ['Log in to extract metadata'],
73     }, {
74         # Without video id in URL
75         'url': 'http://www.vier.be/planb/videos/dit-najaar-plan-b',
76         'only_matching': True,
77     }, {
78         'url': 'http://www.vier.be/video/v3/embed/16129',
79         'only_matching': True,
80     }]
81
82     def _real_initialize(self):
83         self._logged_in = False
84
85     def _login(self, site):
86         username, password = self._get_login_info()
87         if username is None or password is None:
88             return
89
90         login_page = self._download_webpage(
91             'http://www.%s.be/user/login' % site,
92             None, note='Logging in', errnote='Unable to log in',
93             data=urlencode_postdata({
94                 'form_id': 'user_login',
95                 'name': username,
96                 'pass': password,
97             }),
98             headers={'Content-Type': 'application/x-www-form-urlencoded'})
99
100         login_error = self._html_search_regex(
101             r'(?s)<div class="messages error">\s*<div>\s*<h2.+?</h2>(.+?)<',
102             login_page, 'login error', default=None)
103         if login_error:
104             self.report_warning('Unable to log in: %s' % login_error)
105         else:
106             self._logged_in = True
107
108     def _real_extract(self, url):
109         mobj = re.match(self._VALID_URL, url)
110         embed_id = mobj.group('embed_id')
111         display_id = mobj.group('display_id') or embed_id
112         video_id = mobj.group('id') or embed_id
113         site = mobj.group('site')
114
115         if not self._logged_in:
116             self._login(site)
117
118         webpage = self._download_webpage(url, display_id)
119
120         if r'id="user-login"' in webpage:
121             self.report_warning(
122                 'Log in to extract metadata', video_id=display_id)
123             webpage = self._download_webpage(
124                 'http://www.%s.be/video/v3/embed/%s' % (site, video_id),
125                 display_id)
126
127         video_id = self._search_regex(
128             [r'data-nid="(\d+)"', r'"nid"\s*:\s*"(\d+)"'],
129             webpage, 'video id', default=video_id or display_id)
130         application = self._search_regex(
131             [r'data-application="([^"]+)"', r'"application"\s*:\s*"([^"]+)"'],
132             webpage, 'application', default=site + '_vod')
133         filename = self._search_regex(
134             [r'data-filename="([^"]+)"', r'"filename"\s*:\s*"([^"]+)"'],
135             webpage, 'filename')
136
137         playlist_url = 'http://vod.streamcloud.be/%s/_definst_/mp4:%s.mp4/playlist.m3u8' % (application, filename)
138         formats = self._extract_wowza_formats(playlist_url, display_id, skip_protocols=['dash'])
139         self._sort_formats(formats)
140
141         title = self._og_search_title(webpage, default=display_id)
142         thumbnail = self._og_search_thumbnail(webpage, default=None)
143         description = self._html_search_regex(
144             r'''(?x)<div\ class="[^"]*field-type-text-with-summary[^"]*">\s*
145                       (?:<div\ class="[^"]+">\s*)*
146                      <p>\s*(?:<span>)?(.+?)</''',
147             webpage, 'description', default=None)
148         episode_number = int_or_none(self._search_regex(
149             r'(?i)aflevering (\d+)', title, 'episode_number', default=None,
150             fatal=False))
151         upload_date = unified_strdate(self._html_search_regex(
152             r'''(?x)<div\ class="[^"]*field-name-post-date[^"]*">\s*
153                     (?:<div\ class="[^"]+">\s*)*
154                       (\d{2}/\d{2}/\d{4})''',
155             webpage, 'upload_date', default=None))
156
157         return {
158             'id': video_id,
159             'display_id': display_id,
160             'title': title,
161             'description': description,
162             'episode_number': episode_number,
163             'upload_date': upload_date,
164             'thumbnail': thumbnail,
165             'formats': formats,
166         }
167
168
169 class VierVideosIE(InfoExtractor):
170     IE_NAME = 'vier:videos'
171     _VALID_URL = r'https?://(?:www\.)?(?P<site>vier|vijf)\.be/(?P<program>[^/]+)/videos(?:\?.*\bpage=(?P<page>\d+)|$)'
172     _TESTS = [{
173         'url': 'http://www.vier.be/demoestuin/videos',
174         'info_dict': {
175             'id': 'demoestuin',
176         },
177         'playlist_mincount': 153,
178     }, {
179         'url': 'http://www.vijf.be/temptationisland/videos',
180         'info_dict': {
181             'id': 'temptationisland',
182         },
183         'playlist_mincount': 159,
184     }, {
185         'url': 'http://www.vier.be/demoestuin/videos?page=6',
186         'info_dict': {
187             'id': 'demoestuin-page6',
188         },
189         'playlist_mincount': 20,
190     }, {
191         'url': 'http://www.vier.be/demoestuin/videos?page=7',
192         'info_dict': {
193             'id': 'demoestuin-page7',
194         },
195         'playlist_mincount': 13,
196     }]
197
198     def _real_extract(self, url):
199         mobj = re.match(self._VALID_URL, url)
200         program = mobj.group('program')
201         site = mobj.group('site')
202
203         page_id = mobj.group('page')
204         if page_id:
205             page_id = int(page_id)
206             start_page = page_id
207             playlist_id = '%s-page%d' % (program, page_id)
208         else:
209             start_page = 0
210             playlist_id = program
211
212         entries = []
213         for current_page_id in itertools.count(start_page):
214             current_page = self._download_webpage(
215                 'http://www.%s.be/%s/videos?page=%d' % (site, program, current_page_id),
216                 program,
217                 'Downloading page %d' % (current_page_id + 1))
218             page_entries = [
219                 self.url_result('http://www.' + site + '.be' + video_url, 'Vier')
220                 for video_url in re.findall(
221                     r'<h[23]><a href="(/[^/]+/videos/[^/]+(?:/\d+)?)">', current_page)]
222             entries.extend(page_entries)
223             if page_id or '>Meer<' not in current_page:
224                 break
225
226         return self.playlist_result(entries, playlist_id)