[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / expressen.py
blob33b829845bdba0f795015b295753cf45cd18a389
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 int_or_none,
5 unescapeHTML,
6 unified_timestamp,
10 class ExpressenIE(InfoExtractor):
11 _VALID_URL = r'''(?x)
12 https?://
13 (?:www\.)?(?:expressen|di)\.se/
14 (?:(?:tvspelare/video|video-?player/embed)/)?
15 (?:tv|nyheter)/(?:[^/?#]+/)*
16 (?P<id>[^/?#&]+)
17 '''
18 _EMBED_REGEX = [r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?(?:expressen|di)\.se/(?:tvspelare/video|videoplayer/embed)/tv/.+?)\1']
19 _TESTS = [{
20 'url': 'https://www.expressen.se/tv/ledare/ledarsnack/ledarsnack-om-arbetslosheten-bland-kvinnor-i-speciellt-utsatta-omraden/',
21 'md5': 'deb2ca62e7b1dcd19fa18ba37523f66e',
22 'info_dict': {
23 'id': 'ba90f5a9-78d1-4511-aa02-c177b9c99136',
24 'display_id': 'ledarsnack-om-arbetslosheten-bland-kvinnor-i-speciellt-utsatta-omraden',
25 'ext': 'mp4',
26 'title': 'Ledarsnack: Om arbetslösheten bland kvinnor i speciellt utsatta områden',
27 'description': 'md5:f38c81ff69f3de4d269bbda012fcbbba',
28 'thumbnail': r're:^https?://.*\.jpg$',
29 'duration': 788,
30 'timestamp': 1526639109,
31 'upload_date': '20180518',
33 }, {
34 'url': 'https://www.expressen.se/tv/kultur/kulturdebatt-med-expressens-karin-olsson/',
35 'only_matching': True,
36 }, {
37 'url': 'https://www.expressen.se/tvspelare/video/tv/ditv/ekonomistudion/experterna-har-ar-fragorna-som-avgor-valet/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
38 'only_matching': True,
39 }, {
40 'url': 'https://www.expressen.se/videoplayer/embed/tv/ditv/ekonomistudion/experterna-har-ar-fragorna-som-avgor-valet/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
41 'only_matching': True,
42 }, {
43 'url': 'https://www.di.se/videoplayer/embed/tv/ditv/borsmorgon/implantica-rusar-70--under-borspremiaren-hor-styrelsemedlemmen/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
44 'only_matching': True,
45 }, {
46 'url': 'https://www.expressen.se/video-player/embed/tv/nyheter/ekero-fodda-olof-gustafsson-forvaltar-knarkbaronen-pablo-escobars-namn',
47 'only_matching': True,
48 }, {
49 'url': 'https://www.expressen.se/nyheter/efter-egna-telefonbluffen-escobar-stammer-klarna/',
50 'only_matching': True,
53 def _real_extract(self, url):
54 display_id = self._match_id(url)
56 webpage = self._download_webpage(url, display_id)
58 def extract_data(name):
59 return self._parse_json(
60 self._search_regex(
61 rf'data-{name}=(["\'])(?P<value>(?:(?!\1).)+)\1',
62 webpage, 'info', group='value'),
63 display_id, transform_source=unescapeHTML)
65 info = extract_data('video-tracking-info')
66 video_id = info['contentId']
68 data = extract_data('article-data')
69 stream = data['stream']
71 if determine_ext(stream) == 'm3u8':
72 formats = self._extract_m3u8_formats(
73 stream, display_id, 'mp4', entry_protocol='m3u8_native',
74 m3u8_id='hls')
75 else:
76 formats = [{
77 'url': stream,
80 title = info.get('titleRaw') or data['title']
81 description = info.get('descriptionRaw')
82 thumbnail = info.get('socialMediaImage') or data.get('image')
83 duration = int_or_none(info.get('videoTotalSecondsDuration')
84 or data.get('totalSecondsDuration'))
85 timestamp = unified_timestamp(info.get('publishDate'))
87 return {
88 'id': video_id,
89 'display_id': display_id,
90 'title': title,
91 'description': description,
92 'thumbnail': thumbnail,
93 'duration': duration,
94 'timestamp': timestamp,
95 'formats': formats,