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