]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/tv2.py
[tv2:article] Fix extraction (Closes #10188)
[yt-dlp.git] / youtube_dl / extractor / tv2.py
CommitLineData
bc0f937b
S
1# encoding: utf-8
2from __future__ import unicode_literals
3
588b82bb
S
4import re
5
bc0f937b
S
6from .common import InfoExtractor
7from ..utils import (
8 determine_ext,
9 int_or_none,
10 float_or_none,
481c5c51 11 js_to_json,
bc0f937b 12 parse_iso8601,
588b82bb 13 remove_end,
bc0f937b
S
14)
15
16
17class TV2IE(InfoExtractor):
5886b38d 18 _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
bc0f937b
S
19 _TEST = {
20 'url': 'http://www.tv2.no/v/916509/',
bc0f937b
S
21 'info_dict': {
22 'id': '916509',
ed1a3905
YCH
23 'ext': 'mp4',
24 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
bc0f937b
S
25 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
26 'timestamp': 1431715610,
27 'upload_date': '20150515',
28 'duration': 156.967,
29 'view_count': int,
30 'categories': list,
ed1a3905
YCH
31 },
32 'params': {
33 # m3u8 download
34 'skip_download': True,
35 },
bc0f937b
S
36 }
37
38 def _real_extract(self, url):
39 video_id = self._match_id(url)
40
41 formats = []
42 format_urls = []
43 for protocol in ('HDS', 'HLS'):
44 data = self._download_json(
45 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
46 video_id, 'Downloading play JSON')['playback']
47 for item in data['items']['item']:
48 video_url = item.get('url')
49 if not video_url or video_url in format_urls:
50 continue
51 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
52 if not self._is_valid_url(video_url, video_id, format_id):
53 continue
54 format_urls.append(video_url)
55 ext = determine_ext(video_url)
56 if ext == 'f4m':
57 formats.extend(self._extract_f4m_formats(
58 video_url, video_id, f4m_id=format_id))
59 elif ext == 'm3u8':
60 formats.extend(self._extract_m3u8_formats(
61 video_url, video_id, 'mp4', m3u8_id=format_id))
62 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
63 pass
64 else:
65 formats.append({
66 'url': video_url,
67 'format_id': format_id,
68 'tbr': int_or_none(item.get('bitrate')),
69 'filesize': int_or_none(item.get('fileSize')),
70 })
71 self._sort_formats(formats)
72
73 asset = self._download_json(
74 'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
75 video_id, 'Downloading metadata JSON')['asset']
76
77 title = asset['title']
78 description = asset.get('description')
79 timestamp = parse_iso8601(asset.get('createTime'))
80 duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
81 view_count = int_or_none(asset.get('views'))
82 categories = asset.get('keywords', '').split(',')
83
84 thumbnails = [{
85 'id': thumbnail.get('@type'),
86 'url': thumbnail.get('url'),
87 } for _, thumbnail in asset.get('imageVersions', {}).items()]
88
89 return {
90 'id': video_id,
91 'url': video_url,
92 'title': title,
93 'description': description,
94 'thumbnails': thumbnails,
95 'timestamp': timestamp,
96 'duration': duration,
97 'view_count': view_count,
98 'categories': categories,
99 'formats': formats,
100 }
588b82bb
S
101
102
103class TV2ArticleIE(InfoExtractor):
5886b38d 104 _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
588b82bb
S
105 _TESTS = [{
106 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
107 'info_dict': {
108 'id': '6930542',
481c5c51 109 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
588b82bb
S
110 'description': 'md5:339573779d3eea3542ffe12006190954',
111 },
112 'playlist_count': 2,
113 }, {
114 'url': 'http://www.tv2.no/a/6930542',
115 'only_matching': True,
116 }]
117
118 def _real_extract(self, url):
119 playlist_id = self._match_id(url)
120
121 webpage = self._download_webpage(url, playlist_id)
122
481c5c51
S
123 # Old embed pattern (looks unused nowadays)
124 assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
125
126 if not assets:
127 # New embed pattern
128 for v in re.findall('TV2ContentboxVideo\(({.+?})\)', webpage):
129 video = self._parse_json(
130 v, playlist_id, transform_source=js_to_json, fatal=False)
131 if not video:
132 continue
133 asset = video.get('assetId')
134 if asset:
135 assets.append(asset)
136
588b82bb 137 entries = [
481c5c51
S
138 self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
139 for asset_id in assets]
588b82bb
S
140
141 title = remove_end(self._og_search_title(webpage), ' - TV2.no')
142 description = remove_end(self._og_search_description(webpage), ' - TV2.no')
143
144 return self.playlist_result(entries, playlist_id, title, description)