Merge branch 'presstv' of https://github.com/Phaeilo/youtube-dl into Phaeilo-presstv
[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_xpath
6 from ..utils import (
7     int_or_none,
8     qualities,
9     unified_strdate,
10     xpath_attr,
11     xpath_element,
12     xpath_text,
13     xpath_with_ns,
14 )
15
16
17 class FirstTVIE(InfoExtractor):
18     IE_NAME = '1tv'
19     IE_DESC = 'Первый канал'
20     _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+p?(?P<id>\d+)'
21
22     _TESTS = [{
23         # single format via video_materials.json API
24         'url': 'http://www.1tv.ru/prj/inprivate/vypusk/35930',
25         'md5': '82a2777648acae812d58b3f5bd42882b',
26         'info_dict': {
27             'id': '35930',
28             'ext': 'mp4',
29             'title': 'Гость Людмила Сенчина. Наедине со всеми. Выпуск от 12.02.2015',
30             'description': 'md5:357933adeede13b202c7c21f91b871b2',
31             'thumbnail': 're:^https?://.*\.(?:jpg|JPG)$',
32             'upload_date': '20150212',
33             'duration': 2694,
34         },
35     }, {
36         # multiple formats via video_materials.json API
37         'url': 'http://www.1tv.ru/video_archive/projects/dobroeutro/p113641',
38         'info_dict': {
39             'id': '113641',
40             'ext': 'mp4',
41             'title': 'Весенняя аллергия. Доброе утро. Фрагмент выпуска от 07.04.2016',
42             'description': 'md5:8dcebb3dded0ff20fade39087fd1fee2',
43             'thumbnail': 're:^https?://.*\.(?:jpg|JPG)$',
44             'upload_date': '20160407',
45             'duration': 179,
46             'formats': 'mincount:3',
47         },
48         'params': {
49             'skip_download': True,
50         },
51     }, {
52         # single format only available via ONE_ONLINE_VIDEOS.archive_single_xml API
53         'url': 'http://www.1tv.ru/video_archive/series/f7552/p47038',
54         'md5': '519d306c5b5669761fd8906c39dbee23',
55         'info_dict': {
56             'id': '47038',
57             'ext': 'mp4',
58             'title': '"Побег". Второй сезон. 3 серия',
59             'description': 'md5:3abf8f6b9bce88201c33e9a3d794a00b',
60             'thumbnail': 're:^https?://.*\.(?:jpg|JPG)$',
61             'upload_date': '20120516',
62             'duration': 3080,
63         },
64     }, {
65         'url': 'http://www.1tv.ru/videoarchive/9967',
66         'only_matching': True,
67     }]
68
69     def _real_extract(self, url):
70         video_id = self._match_id(url)
71
72         # Videos with multiple formats only available via this API
73         video = self._download_json(
74             'http://www.1tv.ru/video_materials.json?legacy_id=%s' % video_id,
75             video_id, fatal=False)
76
77         description, thumbnail, upload_date, duration = [None] * 4
78
79         if video:
80             item = video[0]
81             title = item['title']
82             quality = qualities(('ld', 'sd', 'hd', ))
83             formats = [{
84                 'url': f['src'],
85                 'format_id': f.get('name'),
86                 'quality': quality(f.get('name')),
87             } for f in item['mbr'] if f.get('src')]
88             thumbnail = item.get('poster')
89         else:
90             # Some videos are not available via video_materials.json
91             video = self._download_xml(
92                 'http://www.1tv.ru/owa/win/ONE_ONLINE_VIDEOS.archive_single_xml?pid=%s' % video_id,
93                 video_id)
94
95             NS_MAP = {
96                 'media': 'http://search.yahoo.com/mrss/',
97             }
98
99             item = xpath_element(video, './channel/item', fatal=True)
100             title = xpath_text(item, './title', fatal=True)
101             formats = [{
102                 'url': content.attrib['url'],
103             } for content in item.findall(
104                 compat_xpath(xpath_with_ns('./media:content', NS_MAP))) if content.attrib.get('url')]
105             thumbnail = xpath_attr(
106                 item, xpath_with_ns('./media:thumbnail', NS_MAP), 'url')
107
108         self._sort_formats(formats)
109
110         webpage = self._download_webpage(url, video_id, 'Downloading page', fatal=False)
111         if webpage:
112             title = self._html_search_regex(
113                 (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
114                  r"'title'\s*:\s*'([^']+)'"),
115                 webpage, 'title', default=None) or title
116             description = self._html_search_regex(
117                 r'<div class="descr">\s*<div>&nbsp;</div>\s*<p>([^<]*)</p></div>',
118                 webpage, 'description', default=None) or self._html_search_meta(
119                 'description', webpage, 'description')
120             thumbnail = thumbnail or self._og_search_thumbnail(webpage)
121             duration = int_or_none(self._html_search_meta(
122                 'video:duration', webpage, 'video duration', fatal=False))
123             upload_date = unified_strdate(self._html_search_meta(
124                 'ya:ovs:upload_date', webpage, 'upload date', fatal=False))
125
126         return {
127             'id': video_id,
128             'thumbnail': thumbnail,
129             'title': title,
130             'description': description,
131             'upload_date': upload_date,
132             'duration': int_or_none(duration),
133             'formats': formats
134         }