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