]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/firsttv.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / firsttv.py
CommitLineData
0f6ed94a 1from .common import InfoExtractor
9b5288c9
S
2from ..compat import (
3 compat_str,
4 compat_urlparse,
5)
61dd350a
S
6from ..utils import (
7 int_or_none,
8 qualities,
9 unified_strdate,
3052a30d 10 url_or_none,
61dd350a 11)
0f6ed94a
S
12
13
14class FirstTVIE(InfoExtractor):
9347fddb
S
15 IE_NAME = '1tv'
16 IE_DESC = 'Первый канал'
9b8c554e 17 _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+(?P<id>[^/?#]+)'
0f6ed94a 18
9347fddb 19 _TESTS = [{
d0fa172e 20 # single format
9b8c554e
RA
21 'url': 'http://www.1tv.ru/shows/naedine-so-vsemi/vypuski/gost-lyudmila-senchina-naedine-so-vsemi-vypusk-ot-12-02-2015',
22 'md5': 'a1b6b60d530ebcf8daacf4565762bbaf',
0f6ed94a 23 'info_dict': {
9b8c554e 24 'id': '40049',
0f6ed94a 25 'ext': 'mp4',
9b5288c9 26 'title': 'Гость Людмила Сенчина. Наедине со всеми. Выпуск от 12.02.2015',
ec85ded8 27 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
61dd350a
S
28 'upload_date': '20150212',
29 'duration': 2694,
cb389289 30 },
9347fddb 31 }, {
d0fa172e 32 # multiple formats
9b8c554e 33 'url': 'http://www.1tv.ru/shows/dobroe-utro/pro-zdorove/vesennyaya-allergiya-dobroe-utro-fragment-vypuska-ot-07042016',
d0fa172e
RA
34 'info_dict': {
35 'id': '364746',
36 'ext': 'mp4',
9b5288c9 37 'title': 'Весенняя аллергия. Доброе утро. Фрагмент выпуска от 07.04.2016',
ec85ded8 38 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
d0fa172e
RA
39 'upload_date': '20160407',
40 'duration': 179,
41 'formats': 'mincount:3',
42 },
43 'params': {
44 'skip_download': True,
45 },
9b5288c9
S
46 }, {
47 'url': 'http://www.1tv.ru/news/issue/2016-12-01/14:00',
48 'info_dict': {
49 'id': '14:00',
50 'title': 'Выпуск новостей в 14:00 1 декабря 2016 года. Новости. Первый канал',
51 'description': 'md5:2e921b948f8c1ff93901da78ebdb1dfd',
52 },
53 'playlist_count': 13,
54 }, {
55 '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',
56 'only_matching': True,
9347fddb 57 }]
0f6ed94a
S
58
59 def _real_extract(self, url):
9b8c554e 60 display_id = self._match_id(url)
0f6ed94a 61
9b8c554e
RA
62 webpage = self._download_webpage(url, display_id)
63 playlist_url = compat_urlparse.urljoin(url, self._search_regex(
9b5288c9
S
64 r'data-playlist-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
65 webpage, 'playlist url', group='url'))
66
67 parsed_url = compat_urlparse.urlparse(playlist_url)
68 qs = compat_urlparse.parse_qs(parsed_url.query)
69 item_ids = qs.get('videos_ids[]') or qs.get('news_ids[]')
70
71 items = self._download_json(playlist_url, display_id)
72
73 if item_ids:
74 items = [
75 item for item in items
76 if item.get('uid') and compat_str(item['uid']) in item_ids]
77 else:
78 items = [items[0]]
79
80 entries = []
81 QUALITIES = ('ld', 'sd', 'hd', )
82
83 for item in items:
84 title = item['title']
85 quality = qualities(QUALITIES)
86 formats = []
1fe84be0 87 path = None
9b5288c9 88 for f in item.get('mbr', []):
3052a30d
S
89 src = url_or_none(f.get('src'))
90 if not src:
9b5288c9
S
91 continue
92 tbr = int_or_none(self._search_regex(
93 r'_(\d{3,})\.mp4', src, 'tbr', default=None))
1fe84be0
S
94 if not path:
95 path = self._search_regex(
96 r'//[^/]+/(.+?)_\d+\.mp4', src,
97 'm3u8 path', default=None)
9b5288c9
S
98 formats.append({
99 'url': src,
100 'format_id': f.get('name'),
101 'tbr': tbr,
1fe84be0 102 'source_preference': quality(f.get('name')),
f7a747ce 103 # quality metadata of http formats may be incorrect
f983b875 104 'preference': -10,
9b5288c9 105 })
1fe84be0
S
106 # m3u8 URL format is reverse engineered from [1] (search for
107 # master.m3u8). dashEdges (that is currently balancer-vod.1tv.ru)
108 # is taken from [2].
109 # 1. http://static.1tv.ru/player/eump1tv-current/eump-1tv.all.min.js?rnd=9097422834:formatted
110 # 2. http://static.1tv.ru/player/eump1tv-config/config-main.js?rnd=9097422834
111 if not path and len(formats) == 1:
112 path = self._search_regex(
113 r'//[^/]+/(.+?$)', formats[0]['url'],
114 'm3u8 path', default=None)
115 if path:
116 if len(formats) == 1:
117 m3u8_path = ','
118 else:
119 tbrs = [compat_str(t) for t in sorted(f['tbr'] for f in formats)]
120 m3u8_path = '_,%s,%s' % (','.join(tbrs), '.mp4')
121 formats.extend(self._extract_m3u8_formats(
122 'http://balancer-vod.1tv.ru/%s%s.urlset/master.m3u8'
123 % (path, m3u8_path),
124 display_id, 'mp4',
125 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
9b5288c9
S
126
127 thumbnail = item.get('poster') or self._og_search_thumbnail(webpage)
128 duration = int_or_none(item.get('duration') or self._html_search_meta(
129 'video:duration', webpage, 'video duration', fatal=False))
130 upload_date = unified_strdate(self._html_search_meta(
131 'ya:ovs:upload_date', webpage, 'upload date', default=None))
0f6ed94a 132
9b5288c9 133 entries.append({
4afa4ff2 134 'id': compat_str(item.get('id') or item['uid']),
9b5288c9
S
135 'thumbnail': thumbnail,
136 'title': title,
137 'upload_date': upload_date,
138 'duration': int_or_none(duration),
139 'formats': formats
9b8c554e 140 })
61dd350a 141
9b8c554e
RA
142 title = self._html_search_regex(
143 (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
144 r"'title'\s*:\s*'([^']+)'"),
9b5288c9
S
145 webpage, 'title', default=None) or self._og_search_title(
146 webpage, default=None)
9b8c554e
RA
147 description = self._html_search_regex(
148 r'<div class="descr">\s*<div>&nbsp;</div>\s*<p>([^<]*)</p></div>',
149 webpage, 'description', default=None) or self._html_search_meta(
9b5288c9 150 'description', webpage, 'description', default=None)
0f6ed94a 151
9b5288c9 152 return self.playlist_result(entries, display_id, title, description)