]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/la7.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / la7.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 float_or_none,
7 HEADRequest,
8 int_or_none,
9 parse_duration,
10 unified_strdate,
11 )
12
13
14 class LA7IE(InfoExtractor):
15 IE_NAME = 'la7.it'
16 _VALID_URL = r'''(?x)(https?://)?(?:
17 (?:www\.)?la7\.it/([^/]+)/(?:rivedila7|video)/|
18 tg\.la7\.it/repliche-tgla7\?id=
19 )(?P<id>.+)'''
20
21 _TESTS = [{
22 # 'src' is a plain URL
23 'url': 'http://www.la7.it/crozza/video/inccool8-02-10-2015-163722',
24 'md5': '8b613ffc0c4bf9b9e377169fc19c214c',
25 'info_dict': {
26 'id': 'inccool8-02-10-2015-163722',
27 'ext': 'mp4',
28 'title': 'Inc.Cool8',
29 'description': 'Benvenuti nell\'incredibile mondo della INC. COOL. 8. dove “INC.” sta per “Incorporated” “COOL” sta per “fashion” ed Eight sta per il gesto atletico',
30 'thumbnail': 're:^https?://.*',
31 'upload_date': '20151002',
32 },
33 }, {
34 'url': 'http://www.la7.it/omnibus/rivedila7/omnibus-news-02-07-2016-189077',
35 'only_matching': True,
36 }]
37 _HOST = 'https://awsvodpkg.iltrovatore.it'
38
39 def _generate_mp4_url(self, quality, m3u8_formats):
40 for f in m3u8_formats:
41 if f['vcodec'] != 'none' and quality in f['url']:
42 http_url = '%s%s.mp4' % (self._HOST, quality)
43
44 urlh = self._request_webpage(
45 HEADRequest(http_url), quality,
46 note='Check filesize', fatal=False)
47 if urlh:
48 http_f = f.copy()
49 del http_f['manifest_url']
50 http_f.update({
51 'format_id': http_f['format_id'].replace('hls-', 'https-'),
52 'url': http_url,
53 'protocol': 'https',
54 'filesize_approx': int_or_none(urlh.headers.get('Content-Length', None)),
55 })
56 return http_f
57 return None
58
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
61
62 if not url.startswith('http'):
63 url = '%s//%s' % (self.http_scheme(), url)
64
65 webpage = self._download_webpage(url, video_id)
66 video_path = self._search_regex(r'(/content/.*?).mp4', webpage, 'video_path')
67
68 formats = self._extract_mpd_formats(
69 f'{self._HOST}/local/dash/,{video_path}.mp4.urlset/manifest.mpd',
70 video_id, mpd_id='dash', fatal=False)
71 m3u8_formats = self._extract_m3u8_formats(
72 f'{self._HOST}/local/hls/,{video_path}.mp4.urlset/master.m3u8',
73 video_id, 'mp4', m3u8_id='hls', fatal=False)
74 formats.extend(m3u8_formats)
75
76 for q in filter(None, video_path.split(',')):
77 http_f = self._generate_mp4_url(q, m3u8_formats)
78 if http_f:
79 formats.append(http_f)
80
81 return {
82 'id': video_id,
83 'title': self._og_search_title(webpage, default=None),
84 'description': self._og_search_description(webpage, default=None),
85 'thumbnail': self._og_search_thumbnail(webpage, default=None),
86 'formats': formats,
87 'upload_date': unified_strdate(self._search_regex(r'datetime="(.+?)"', webpage, 'upload_date', fatal=False))
88 }
89
90
91 class LA7PodcastEpisodeIE(InfoExtractor):
92 IE_NAME = 'la7.it:pod:episode'
93 _VALID_URL = r'''(?x)(https?://)?
94 (?:www\.)?la7\.it/[^/]+/podcast/([^/]+-)?(?P<id>\d+)'''
95
96 _TESTS = [{
97 'url': 'https://www.la7.it/voicetown/podcast/la-carezza-delle-memoria-di-carlo-verdone-23-03-2021-371497',
98 'md5': '7737d4d79b3c1a34b3de3e16297119ed',
99 'info_dict': {
100 'id': '371497',
101 'ext': 'mp3',
102 'title': '"La carezza delle memoria" di Carlo Verdone',
103 'description': 'md5:5abf07c3c551a687db80af3f9ceb7d52',
104 'thumbnail': 'https://www.la7.it/sites/default/files/podcast/371497.jpg',
105 'upload_date': '20210323',
106 },
107 }, {
108 # embed url
109 'url': 'https://www.la7.it/embed/podcast/371497',
110 'only_matching': True,
111 }, {
112 # date already in the title
113 'url': 'https://www.la7.it/propagandalive/podcast/lintervista-di-diego-bianchi-ad-annalisa-cuzzocrea-puntata-del-1932021-20-03-2021-371130',
114 'only_matching': True,
115 }, {
116 # title same as show_title
117 'url': 'https://www.la7.it/otto-e-mezzo/podcast/otto-e-mezzo-26-03-2021-372340',
118 'only_matching': True,
119 }]
120
121 def _extract_info(self, webpage, video_id=None, ppn=None):
122 if not video_id:
123 video_id = self._search_regex(
124 r'data-nid=([\'"])(?P<vid>\d+)\1',
125 webpage, 'video_id', group='vid')
126
127 media_url = self._search_regex(
128 (r'src:\s*([\'"])(?P<url>.+?mp3.+?)\1',
129 r'data-podcast=([\'"])(?P<url>.+?mp3.+?)\1'),
130 webpage, 'media_url', group='url')
131 ext = determine_ext(media_url)
132 formats = [{
133 'url': media_url,
134 'format_id': ext,
135 'ext': ext,
136 }]
137
138 title = self._html_search_regex(
139 (r'<div class="title">(?P<title>.+?)</',
140 r'<title>(?P<title>[^<]+)</title>',
141 r'title:\s*([\'"])(?P<title>.+?)\1'),
142 webpage, 'title', group='title')
143
144 description = (
145 self._html_search_regex(
146 (r'<div class="description">(.+?)</div>',
147 r'<div class="description-mobile">(.+?)</div>',
148 r'<div class="box-txt">([^<]+?)</div>',
149 r'<div class="field-content"><p>(.+?)</p></div>'),
150 webpage, 'description', default=None)
151 or self._html_search_meta('description', webpage))
152
153 thumb = self._html_search_regex(
154 (r'<div class="podcast-image"><img src="(.+?)"></div>',
155 r'<div class="container-embed"[^<]+url\((.+?)\);">',
156 r'<div class="field-content"><img src="(.+?)"'),
157 webpage, 'thumbnail', fatal=False, default=None)
158
159 duration = parse_duration(self._html_search_regex(
160 r'<span class="(?:durata|duration)">([\d:]+)</span>',
161 webpage, 'duration', fatal=False, default=None))
162
163 date = self._html_search_regex(
164 r'class="data">\s*(?:<span>)?([\d\.]+)\s*</',
165 webpage, 'date', default=None)
166
167 date_alt = self._search_regex(
168 r'(\d+[\./]\d+[\./]\d+)', title, 'date_alt', default=None)
169 ppn = ppn or self._search_regex(
170 r'ppN:\s*([\'"])(?P<ppn>.+?)\1',
171 webpage, 'ppn', group='ppn', default=None)
172 # if the date is not in the title
173 # and title is the same as the show_title
174 # add the date to the title
175 if date and not date_alt and ppn and ppn.lower() == title.lower():
176 title += ' del %s' % date
177 return {
178 'id': video_id,
179 'title': title,
180 'description': description,
181 'duration': float_or_none(duration),
182 'formats': formats,
183 'thumbnail': thumb,
184 'upload_date': unified_strdate(date),
185 }
186
187 def _real_extract(self, url):
188 video_id = self._match_id(url)
189 webpage = self._download_webpage(url, video_id)
190
191 return self._extract_info(webpage, video_id)
192
193
194 class LA7PodcastIE(LA7PodcastEpisodeIE): # XXX: Do not subclass from concrete IE
195 IE_NAME = 'la7.it:podcast'
196 _VALID_URL = r'(https?://)?(www\.)?la7\.it/(?P<id>[^/]+)/podcast/?(?:$|[#?])'
197
198 _TESTS = [{
199 'url': 'https://www.la7.it/propagandalive/podcast',
200 'info_dict': {
201 'id': 'propagandalive',
202 'title': "Propaganda Live",
203 },
204 'playlist_count': 10,
205 }]
206
207 def _real_extract(self, url):
208 playlist_id = self._match_id(url)
209 webpage = self._download_webpage(url, playlist_id)
210
211 title = (
212 self._html_search_regex(
213 r'<h1.*?>(.+?)</h1>', webpage, 'title', fatal=False, default=None)
214 or self._og_search_title(webpage))
215 ppn = self._search_regex(
216 r'window\.ppN\s*=\s*([\'"])(?P<ppn>.+?)\1',
217 webpage, 'ppn', group='ppn', default=None)
218
219 entries = []
220 for episode in re.finditer(
221 r'<div class="container-podcast-property">([\s\S]+?)(?:</div>\s*){3}',
222 webpage):
223 entries.append(self._extract_info(episode.group(1), ppn=ppn))
224
225 return self.playlist_result(entries, playlist_id, title)