[firsttv] fix extraction(closes #9249)
[youtube-dl] / youtube_dl / extractor / firsttv.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urlparse
6 from ..utils import (
7     int_or_none,
8     qualities,
9     unified_strdate,
10 )
11
12
13 class FirstTVIE(InfoExtractor):
14     IE_NAME = '1tv'
15     IE_DESC = 'Первый канал'
16     _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+(?P<id>[^/?#]+)'
17
18     _TESTS = [{
19         'url': 'http://www.1tv.ru/shows/naedine-so-vsemi/vypuski/gost-lyudmila-senchina-naedine-so-vsemi-vypusk-ot-12-02-2015',
20         'md5': 'a1b6b60d530ebcf8daacf4565762bbaf',
21         'info_dict': {
22             'id': '40049',
23             'ext': 'mp4',
24             'title': 'Гость Людмила Сенчина. Наедине со всеми. Выпуск от 12.02.2015',
25             'description': 'md5:36a39c1d19618fec57d12efe212a8370',
26             'thumbnail': 're:^https?://.*\.(?:jpg|JPG)$',
27             'upload_date': '20150212',
28             'duration': 2694,
29         },
30     }, {
31         'url': 'http://www.1tv.ru/shows/dobroe-utro/pro-zdorove/vesennyaya-allergiya-dobroe-utro-fragment-vypuska-ot-07042016',
32         'only_matching': 'true',
33     }]
34
35     def _real_extract(self, url):
36         display_id = self._match_id(url)
37
38         webpage = self._download_webpage(url, display_id)
39         playlist_url = compat_urlparse.urljoin(url, self._search_regex(
40             r'data-playlist-url="([^"]+)', webpage, 'playlist url'))
41
42         item = self._download_json(playlist_url, display_id)[0]
43         video_id = item['id']
44         quality = qualities(('ld', 'sd', 'hd', ))
45         formats = []
46         for f in item.get('mbr', []):
47             src = f.get('src')
48             if not src:
49                 continue
50             fname = f.get('name')
51             formats.append({
52                 'url': src,
53                 'format_id': fname,
54                 'quality': quality(fname),
55             })
56         self._sort_formats(formats)
57
58         title = self._html_search_regex(
59             (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
60              r"'title'\s*:\s*'([^']+)'"),
61             webpage, 'title', default=None) or item['title']
62         description = self._html_search_regex(
63             r'<div class="descr">\s*<div>&nbsp;</div>\s*<p>([^<]*)</p></div>',
64             webpage, 'description', default=None) or self._html_search_meta(
65             'description', webpage, 'description')
66         duration = int_or_none(self._html_search_meta(
67             'video:duration', webpage, 'video duration', fatal=False))
68         upload_date = unified_strdate(self._html_search_meta(
69             'ya:ovs:upload_date', webpage, 'upload date', fatal=False))
70
71         return {
72             'id': video_id,
73             'thumbnail': item.get('poster') or self._og_search_thumbnail(webpage),
74             'title': title,
75             'description': description,
76             'upload_date': upload_date,
77             'duration': int_or_none(duration),
78             'formats': formats
79         }