]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/firsttv.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / firsttv.py
1 import urllib.parse
2
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 qualities,
7 unified_strdate,
8 url_or_none,
9 )
10
11
12 class FirstTVIE(InfoExtractor):
13 IE_NAME = '1tv'
14 IE_DESC = 'Первый канал'
15 _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+(?P<id>[^/?#]+)'
16
17 _TESTS = [{
18 # single format
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 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
26 'upload_date': '20150212',
27 'duration': 2694,
28 },
29 }, {
30 # multiple formats
31 'url': 'http://www.1tv.ru/shows/dobroe-utro/pro-zdorove/vesennyaya-allergiya-dobroe-utro-fragment-vypuska-ot-07042016',
32 'info_dict': {
33 'id': '364746',
34 'ext': 'mp4',
35 'title': 'Весенняя аллергия. Доброе утро. Фрагмент выпуска от 07.04.2016',
36 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
37 'upload_date': '20160407',
38 'duration': 179,
39 'formats': 'mincount:3',
40 },
41 'params': {
42 'skip_download': True,
43 },
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,
55 }]
56
57 def _real_extract(self, url):
58 display_id = self._match_id(url)
59
60 webpage = self._download_webpage(url, display_id)
61 playlist_url = urllib.parse.urljoin(url, self._search_regex(
62 r'data-playlist-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
63 webpage, 'playlist url', group='url'))
64
65 parsed_url = urllib.parse.urlparse(playlist_url)
66 qs = urllib.parse.parse_qs(parsed_url.query)
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
74 if item.get('uid') and str(item['uid']) in item_ids]
75 else:
76 items = [items[0]]
77
78 entries = []
79 QUALITIES = ('ld', 'sd', 'hd')
80
81 for item in items:
82 title = item['title']
83 quality = qualities(QUALITIES)
84 formats = []
85 path = None
86 for f in item.get('mbr', []):
87 src = url_or_none(f.get('src'))
88 if not src:
89 continue
90 tbr = int_or_none(self._search_regex(
91 r'_(\d{3,})\.mp4', src, 'tbr', default=None))
92 if not path:
93 path = self._search_regex(
94 r'//[^/]+/(.+?)_\d+\.mp4', src,
95 'm3u8 path', default=None)
96 formats.append({
97 'url': src,
98 'format_id': f.get('name'),
99 'tbr': tbr,
100 'source_preference': quality(f.get('name')),
101 # quality metadata of http formats may be incorrect
102 'preference': -10,
103 })
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:
117 tbrs = [str(t) for t in sorted(f['tbr'] for f in formats)]
118 m3u8_path = '_,{},{}'.format(','.join(tbrs), '.mp4')
119 formats.extend(self._extract_m3u8_formats(
120 f'http://balancer-vod.1tv.ru/{path}{m3u8_path}.urlset/master.m3u8',
121 display_id, 'mp4',
122 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
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))
129
130 entries.append({
131 'id': str(item.get('id') or item['uid']),
132 'thumbnail': thumbnail,
133 'title': title,
134 'upload_date': upload_date,
135 'duration': int_or_none(duration),
136 'formats': formats,
137 })
138
139 title = self._html_search_regex(
140 (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
141 r"'title'\s*:\s*'([^']+)'"),
142 webpage, 'title', default=None) or self._og_search_title(
143 webpage, default=None)
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(
147 'description', webpage, 'description', default=None)
148
149 return self.playlist_result(entries, display_id, title, description)