]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/expressen.py
Update README.md
[youtubedl] / youtube_dl / extractor / expressen.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 int_or_none,
10 unescapeHTML,
11 unified_timestamp,
12 )
13
14
15 class ExpressenIE(InfoExtractor):
16 _VALID_URL = r'''(?x)
17 https?://
18 (?:www\.)?expressen\.se/
19 (?:(?:tvspelare/video|videoplayer/embed)/)?
20 tv/(?:[^/]+/)*
21 (?P<id>[^/?#&]+)
22 '''
23 _TESTS = [{
24 'url': 'https://www.expressen.se/tv/ledare/ledarsnack/ledarsnack-om-arbetslosheten-bland-kvinnor-i-speciellt-utsatta-omraden/',
25 'md5': '2fbbe3ca14392a6b1b36941858d33a45',
26 'info_dict': {
27 'id': '8690962',
28 'ext': 'mp4',
29 'title': 'Ledarsnack: Om arbetslösheten bland kvinnor i speciellt utsatta områden',
30 'description': 'md5:f38c81ff69f3de4d269bbda012fcbbba',
31 'thumbnail': r're:^https?://.*\.jpg$',
32 'duration': 788,
33 'timestamp': 1526639109,
34 'upload_date': '20180518',
35 },
36 }, {
37 'url': 'https://www.expressen.se/tv/kultur/kulturdebatt-med-expressens-karin-olsson/',
38 'only_matching': True,
39 }, {
40 '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',
41 'only_matching': True,
42 }, {
43 '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',
44 'only_matching': True,
45 }]
46
47 @staticmethod
48 def _extract_urls(webpage):
49 return [
50 mobj.group('url') for mobj in re.finditer(
51 r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?expressen\.se/(?:tvspelare/video|videoplayer/embed)/tv/.+?)\1',
52 webpage)]
53
54 def _real_extract(self, url):
55 display_id = self._match_id(url)
56
57 webpage = self._download_webpage(url, display_id)
58
59 def extract_data(name):
60 return self._parse_json(
61 self._search_regex(
62 r'data-%s=(["\'])(?P<value>(?:(?!\1).)+)\1' % name,
63 webpage, 'info', group='value'),
64 display_id, transform_source=unescapeHTML)
65
66 info = extract_data('video-tracking-info')
67 video_id = info['videoId']
68
69 data = extract_data('article-data')
70 stream = data['stream']
71
72 if determine_ext(stream) == 'm3u8':
73 formats = self._extract_m3u8_formats(
74 stream, display_id, 'mp4', entry_protocol='m3u8_native',
75 m3u8_id='hls')
76 else:
77 formats = [{
78 'url': stream,
79 }]
80 self._sort_formats(formats)
81
82 title = info.get('titleRaw') or data['title']
83 description = info.get('descriptionRaw')
84 thumbnail = info.get('socialMediaImage') or data.get('image')
85 duration = int_or_none(info.get('videoTotalSecondsDuration')
86 or data.get('totalSecondsDuration'))
87 timestamp = unified_timestamp(info.get('publishDate'))
88
89 return {
90 'id': video_id,
91 'display_id': display_id,
92 'title': title,
93 'description': description,
94 'thumbnail': thumbnail,
95 'duration': duration,
96 'timestamp': timestamp,
97 'formats': formats,
98 }