[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / videomore.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     int_or_none,
10     orderedSet,
11     parse_duration,
12     str_or_none,
13     unified_strdate,
14     url_or_none,
15     xpath_element,
16     xpath_text,
17 )
18
19
20 class VideomoreIE(InfoExtractor):
21     IE_NAME = 'videomore'
22     _VALID_URL = r'''(?x)
23                     videomore:(?P<sid>\d+)$|
24                     https?://(?:player\.)?videomore\.ru/
25                         (?:
26                             (?:
27                                 embed|
28                                 [^/]+/[^/]+
29                             )/|
30                             [^/]*\?.*?\btrack_id=
31                         )
32                         (?P<id>\d+)
33                         (?:[/?#&]|\.(?:xml|json)|$)
34                     '''
35     _TESTS = [{
36         'url': 'http://videomore.ru/kino_v_detalayah/5_sezon/367617',
37         'md5': '44455a346edc0d509ac5b5a5b531dc35',
38         'info_dict': {
39             'id': '367617',
40             'ext': 'flv',
41             'title': 'Кино в деталях 5 сезон В гостях Алексей Чумаков и Юлия Ковальчук',
42             'series': 'Кино в деталях',
43             'episode': 'В гостях Алексей Чумаков и Юлия Ковальчук',
44             'thumbnail': r're:^https?://.*\.jpg',
45             'duration': 2910,
46             'view_count': int,
47             'comment_count': int,
48             'age_limit': 16,
49         },
50     }, {
51         'url': 'http://videomore.ru/embed/259974',
52         'info_dict': {
53             'id': '259974',
54             'ext': 'flv',
55             'title': 'Молодежка 2 сезон 40 серия',
56             'series': 'Молодежка',
57             'episode': '40 серия',
58             'thumbnail': r're:^https?://.*\.jpg',
59             'duration': 2809,
60             'view_count': int,
61             'comment_count': int,
62             'age_limit': 16,
63         },
64         'params': {
65             'skip_download': True,
66         },
67     }, {
68         'url': 'http://videomore.ru/molodezhka/sezon_promo/341073',
69         'info_dict': {
70             'id': '341073',
71             'ext': 'flv',
72             'title': 'Промо Команда проиграла из-за Бакина?',
73             'episode': 'Команда проиграла из-за Бакина?',
74             'thumbnail': r're:^https?://.*\.jpg',
75             'duration': 29,
76             'age_limit': 16,
77             'view_count': int,
78         },
79         'params': {
80             'skip_download': True,
81         },
82     }, {
83         'url': 'http://videomore.ru/elki_3?track_id=364623',
84         'only_matching': True,
85     }, {
86         'url': 'http://videomore.ru/embed/364623',
87         'only_matching': True,
88     }, {
89         'url': 'http://videomore.ru/video/tracks/364623.xml',
90         'only_matching': True,
91     }, {
92         'url': 'http://videomore.ru/video/tracks/364623.json',
93         'only_matching': True,
94     }, {
95         'url': 'http://videomore.ru/video/tracks/158031/quotes/33248',
96         'only_matching': True,
97     }, {
98         'url': 'videomore:367617',
99         'only_matching': True,
100     }, {
101         'url': 'https://player.videomore.ru/?partner_id=97&track_id=736234&autoplay=0&userToken=',
102         'only_matching': True,
103     }]
104
105     @staticmethod
106     def _extract_url(webpage):
107         mobj = re.search(
108             r'<object[^>]+data=(["\'])https?://videomore\.ru/player\.swf\?.*config=(?P<url>https?://videomore\.ru/(?:[^/]+/)+\d+\.xml).*\1',
109             webpage)
110         if not mobj:
111             mobj = re.search(
112                 r'<iframe[^>]+src=([\'"])(?P<url>https?://videomore\.ru/embed/\d+)',
113                 webpage)
114
115         if mobj:
116             return mobj.group('url')
117
118     def _real_extract(self, url):
119         mobj = re.match(self._VALID_URL, url)
120         video_id = mobj.group('sid') or mobj.group('id')
121
122         video = self._download_xml(
123             'http://videomore.ru/video/tracks/%s.xml' % video_id,
124             video_id, 'Downloading video XML')
125
126         item = xpath_element(video, './/playlist/item', fatal=True)
127
128         title = xpath_text(
129             item, ('./title', './episode_name'), 'title', fatal=True)
130
131         video_url = xpath_text(item, './video_url', 'video url', fatal=True)
132         formats = self._extract_f4m_formats(video_url, video_id, f4m_id='hds')
133         self._sort_formats(formats)
134
135         thumbnail = xpath_text(item, './thumbnail_url')
136         duration = int_or_none(xpath_text(item, './duration'))
137         view_count = int_or_none(xpath_text(item, './views'))
138         comment_count = int_or_none(xpath_text(item, './count_comments'))
139         age_limit = int_or_none(xpath_text(item, './min_age'))
140
141         series = xpath_text(item, './project_name')
142         episode = xpath_text(item, './episode_name')
143
144         return {
145             'id': video_id,
146             'title': title,
147             'series': series,
148             'episode': episode,
149             'thumbnail': thumbnail,
150             'duration': duration,
151             'view_count': view_count,
152             'comment_count': comment_count,
153             'age_limit': age_limit,
154             'formats': formats,
155         }
156
157
158 class VideomoreVideoIE(InfoExtractor):
159     IE_NAME = 'videomore:video'
160     _VALID_URL = r'https?://videomore\.ru/(?:(?:[^/]+/){2})?(?P<id>[^/?#&]+)(?:/*|[?#&].*?)$'
161     _TESTS = [{
162         # single video with og:video:iframe
163         'url': 'http://videomore.ru/elki_3',
164         'info_dict': {
165             'id': '364623',
166             'ext': 'flv',
167             'title': 'Ёлки 3',
168             'description': '',
169             'thumbnail': r're:^https?://.*\.jpg',
170             'duration': 5579,
171             'age_limit': 6,
172             'view_count': int,
173         },
174         'params': {
175             'skip_download': True,
176         },
177     }, {
178         # season single series with og:video:iframe
179         'url': 'http://videomore.ru/poslednii_ment/1_sezon/14_seriya',
180         'only_matching': True,
181     }, {
182         'url': 'http://videomore.ru/sejchas_v_seti/serii_221-240/226_vypusk',
183         'only_matching': True,
184     }, {
185         # single video without og:video:iframe
186         'url': 'http://videomore.ru/marin_i_ego_druzya',
187         'info_dict': {
188             'id': '359073',
189             'ext': 'flv',
190             'title': '1 серия. Здравствуй, Аквавилль!',
191             'description': 'md5:c6003179538b5d353e7bcd5b1372b2d7',
192             'thumbnail': r're:^https?://.*\.jpg',
193             'duration': 754,
194             'age_limit': 6,
195             'view_count': int,
196         },
197         'params': {
198             'skip_download': True,
199         },
200     }, {
201         'url': 'https://videomore.ru/molodezhka/6_sezon/29_seriya?utm_so',
202         'only_matching': True,
203     }]
204
205     @classmethod
206     def suitable(cls, url):
207         return False if VideomoreIE.suitable(url) else super(VideomoreVideoIE, cls).suitable(url)
208
209     def _real_extract(self, url):
210         display_id = self._match_id(url)
211
212         webpage = self._download_webpage(url, display_id)
213
214         video_url = self._og_search_property(
215             'video:iframe', webpage, 'video url', default=None)
216
217         if not video_url:
218             video_id = self._search_regex(
219                 (r'config\s*:\s*["\']https?://videomore\.ru/video/tracks/(\d+)\.xml',
220                  r'track-id=["\'](\d+)',
221                  r'xcnt_product_id\s*=\s*(\d+)'), webpage, 'video id')
222             video_url = 'videomore:%s' % video_id
223         else:
224             video_id = None
225
226         return self.url_result(
227             video_url, ie=VideomoreIE.ie_key(), video_id=video_id)
228
229
230 class VideomoreSeasonIE(InfoExtractor):
231     IE_NAME = 'videomore:season'
232     _VALID_URL = r'https?://videomore\.ru/(?!embed)(?P<id>[^/]+/[^/?#&]+)(?:/*|[?#&].*?)$'
233     _TESTS = [{
234         'url': 'http://videomore.ru/molodezhka/sezon_promo',
235         'info_dict': {
236             'id': 'molodezhka/sezon_promo',
237             'title': 'Молодежка Промо',
238         },
239         'playlist_mincount': 12,
240     }, {
241         'url': 'http://videomore.ru/molodezhka/sezon_promo?utm_so',
242         'only_matching': True,
243     }]
244
245     @classmethod
246     def suitable(cls, url):
247         return (False if (VideomoreIE.suitable(url) or VideomoreVideoIE.suitable(url))
248                 else super(VideomoreSeasonIE, cls).suitable(url))
249
250     def _real_extract(self, url):
251         display_id = self._match_id(url)
252
253         webpage = self._download_webpage(url, display_id)
254
255         title = self._og_search_title(webpage)
256
257         data = self._parse_json(
258             self._html_search_regex(
259                 r'\bclass=["\']seasons-tracks["\'][^>]+\bdata-custom-data=(["\'])(?P<value>{.+?})\1',
260                 webpage, 'data', default='{}', group='value'),
261             display_id, fatal=False)
262
263         entries = []
264
265         if data:
266             episodes = data.get('episodes')
267             if isinstance(episodes, list):
268                 for ep in episodes:
269                     if not isinstance(ep, dict):
270                         continue
271                     ep_id = int_or_none(ep.get('id'))
272                     ep_url = url_or_none(ep.get('url'))
273                     if ep_id:
274                         e = {
275                             'url': 'videomore:%s' % ep_id,
276                             'id': compat_str(ep_id),
277                         }
278                     elif ep_url:
279                         e = {'url': ep_url}
280                     else:
281                         continue
282                     e.update({
283                         '_type': 'url',
284                         'ie_key': VideomoreIE.ie_key(),
285                         'title': str_or_none(ep.get('title')),
286                         'thumbnail': url_or_none(ep.get('image')),
287                         'duration': parse_duration(ep.get('duration')),
288                         'episode_number': int_or_none(ep.get('number')),
289                         'upload_date': unified_strdate(ep.get('date')),
290                     })
291                     entries.append(e)
292
293         if not entries:
294             entries = [
295                 self.url_result(
296                     'videomore:%s' % video_id, ie=VideomoreIE.ie_key(),
297                     video_id=video_id)
298                 for video_id in orderedSet(re.findall(
299                     r':(?:id|key)=["\'](\d+)["\']', webpage))]
300
301         if not entries:
302             entries = [
303                 self.url_result(item) for item in re.findall(
304                     r'<a[^>]+href="((?:https?:)?//videomore\.ru/%s/[^/]+)"[^>]+class="widget-item-desc"'
305                     % display_id, webpage)]
306
307         return self.playlist_result(entries, display_id, title)