Fix "invalid escape sequences" error on Python 3.6
[youtube-dl] / youtube_dl / extractor / firsttv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_str,
7     compat_urlparse,
8 )
9 from ..utils import (
10     int_or_none,
11     qualities,
12     unified_strdate,
13 )
14
15
16 class FirstTVIE(InfoExtractor):
17     IE_NAME = '1tv'
18     IE_DESC = 'Первый канал'
19     _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+(?P<id>[^/?#]+)'
20
21     _TESTS = [{
22         # single format
23         'url': 'http://www.1tv.ru/shows/naedine-so-vsemi/vypuski/gost-lyudmila-senchina-naedine-so-vsemi-vypusk-ot-12-02-2015',
24         'md5': 'a1b6b60d530ebcf8daacf4565762bbaf',
25         'info_dict': {
26             'id': '40049',
27             'ext': 'mp4',
28             'title': 'Гость Людмила Сенчина. Наедине со всеми. Выпуск от 12.02.2015',
29             'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
30             'upload_date': '20150212',
31             'duration': 2694,
32         },
33     }, {
34         # multiple formats
35         'url': 'http://www.1tv.ru/shows/dobroe-utro/pro-zdorove/vesennyaya-allergiya-dobroe-utro-fragment-vypuska-ot-07042016',
36         'info_dict': {
37             'id': '364746',
38             'ext': 'mp4',
39             'title': 'Весенняя аллергия. Доброе утро. Фрагмент выпуска от 07.04.2016',
40             'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
41             'upload_date': '20160407',
42             'duration': 179,
43             'formats': 'mincount:3',
44         },
45         'params': {
46             'skip_download': True,
47         },
48     }, {
49         'url': 'http://www.1tv.ru/news/issue/2016-12-01/14:00',
50         'info_dict': {
51             'id': '14:00',
52             'title': 'Выпуск новостей в 14:00   1 декабря 2016 года. Новости. Первый канал',
53             'description': 'md5:2e921b948f8c1ff93901da78ebdb1dfd',
54         },
55         'playlist_count': 13,
56     }, {
57         'url': 'http://www.1tv.ru/shows/tochvtoch-supersezon/vystupleniya/evgeniy-dyatlov-vladimir-vysockiy-koni-priveredlivye-toch-v-toch-supersezon-fragment-vypuska-ot-06-11-2016',
58         'only_matching': True,
59     }]
60
61     def _real_extract(self, url):
62         display_id = self._match_id(url)
63
64         webpage = self._download_webpage(url, display_id)
65         playlist_url = compat_urlparse.urljoin(url, self._search_regex(
66             r'data-playlist-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
67             webpage, 'playlist url', group='url'))
68
69         parsed_url = compat_urlparse.urlparse(playlist_url)
70         qs = compat_urlparse.parse_qs(parsed_url.query)
71         item_ids = qs.get('videos_ids[]') or qs.get('news_ids[]')
72
73         items = self._download_json(playlist_url, display_id)
74
75         if item_ids:
76             items = [
77                 item for item in items
78                 if item.get('uid') and compat_str(item['uid']) in item_ids]
79         else:
80             items = [items[0]]
81
82         entries = []
83         QUALITIES = ('ld', 'sd', 'hd', )
84
85         for item in items:
86             title = item['title']
87             quality = qualities(QUALITIES)
88             formats = []
89             for f in item.get('mbr', []):
90                 src = f.get('src')
91                 if not src or not isinstance(src, compat_str):
92                     continue
93                 tbr = int_or_none(self._search_regex(
94                     r'_(\d{3,})\.mp4', src, 'tbr', default=None))
95                 formats.append({
96                     'url': src,
97                     'format_id': f.get('name'),
98                     'tbr': tbr,
99                     'quality': quality(f.get('name')),
100                 })
101             self._sort_formats(formats)
102
103             thumbnail = item.get('poster') or self._og_search_thumbnail(webpage)
104             duration = int_or_none(item.get('duration') or self._html_search_meta(
105                 'video:duration', webpage, 'video duration', fatal=False))
106             upload_date = unified_strdate(self._html_search_meta(
107                 'ya:ovs:upload_date', webpage, 'upload date', default=None))
108
109             entries.append({
110                 'id': compat_str(item.get('id') or item['uid']),
111                 'thumbnail': thumbnail,
112                 'title': title,
113                 'upload_date': upload_date,
114                 'duration': int_or_none(duration),
115                 'formats': formats
116             })
117
118         title = self._html_search_regex(
119             (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
120              r"'title'\s*:\s*'([^']+)'"),
121             webpage, 'title', default=None) or self._og_search_title(
122             webpage, default=None)
123         description = self._html_search_regex(
124             r'<div class="descr">\s*<div>&nbsp;</div>\s*<p>([^<]*)</p></div>',
125             webpage, 'description', default=None) or self._html_search_meta(
126             'description', webpage, 'description', default=None)
127
128         return self.playlist_result(entries, display_id, title, description)