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