]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/tv2.py
Prepare to upload.
[youtubedl] / youtube_dl / extractor / tv2.py
1 # encoding: 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 float_or_none,
11 parse_iso8601,
12 remove_end,
13 )
14
15
16 class TV2IE(InfoExtractor):
17 _VALID_URL = 'http://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
18 _TEST = {
19 'url': 'http://www.tv2.no/v/916509/',
20 'info_dict': {
21 'id': '916509',
22 'ext': 'mp4',
23 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
24 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
25 'timestamp': 1431715610,
26 'upload_date': '20150515',
27 'duration': 156.967,
28 'view_count': int,
29 'categories': list,
30 },
31 'params': {
32 # m3u8 download
33 'skip_download': True,
34 },
35 }
36
37 def _real_extract(self, url):
38 video_id = self._match_id(url)
39
40 formats = []
41 format_urls = []
42 for protocol in ('HDS', 'HLS'):
43 data = self._download_json(
44 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
45 video_id, 'Downloading play JSON')['playback']
46 for item in data['items']['item']:
47 video_url = item.get('url')
48 if not video_url or video_url in format_urls:
49 continue
50 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
51 if not self._is_valid_url(video_url, video_id, format_id):
52 continue
53 format_urls.append(video_url)
54 ext = determine_ext(video_url)
55 if ext == 'f4m':
56 formats.extend(self._extract_f4m_formats(
57 video_url, video_id, f4m_id=format_id))
58 elif ext == 'm3u8':
59 formats.extend(self._extract_m3u8_formats(
60 video_url, video_id, 'mp4', m3u8_id=format_id))
61 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
62 pass
63 else:
64 formats.append({
65 'url': video_url,
66 'format_id': format_id,
67 'tbr': int_or_none(item.get('bitrate')),
68 'filesize': int_or_none(item.get('fileSize')),
69 })
70 self._sort_formats(formats)
71
72 asset = self._download_json(
73 'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
74 video_id, 'Downloading metadata JSON')['asset']
75
76 title = asset['title']
77 description = asset.get('description')
78 timestamp = parse_iso8601(asset.get('createTime'))
79 duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
80 view_count = int_or_none(asset.get('views'))
81 categories = asset.get('keywords', '').split(',')
82
83 thumbnails = [{
84 'id': thumbnail.get('@type'),
85 'url': thumbnail.get('url'),
86 } for _, thumbnail in asset.get('imageVersions', {}).items()]
87
88 return {
89 'id': video_id,
90 'url': video_url,
91 'title': title,
92 'description': description,
93 'thumbnails': thumbnails,
94 'timestamp': timestamp,
95 'duration': duration,
96 'view_count': view_count,
97 'categories': categories,
98 'formats': formats,
99 }
100
101
102 class TV2ArticleIE(InfoExtractor):
103 _VALID_URL = 'http://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
104 _TESTS = [{
105 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
106 'info_dict': {
107 'id': '6930542',
108 'title': 'Russen hetses etter pingvintyveri – innrømmer å ha åpnet luken på buret',
109 'description': 'md5:339573779d3eea3542ffe12006190954',
110 },
111 'playlist_count': 2,
112 }, {
113 'url': 'http://www.tv2.no/a/6930542',
114 'only_matching': True,
115 }]
116
117 def _real_extract(self, url):
118 playlist_id = self._match_id(url)
119
120 webpage = self._download_webpage(url, playlist_id)
121
122 entries = [
123 self.url_result('http://www.tv2.no/v/%s' % video_id, 'TV2')
124 for video_id in re.findall(r'data-assetid="(\d+)"', webpage)]
125
126 title = remove_end(self._og_search_title(webpage), ' - TV2.no')
127 description = remove_end(self._og_search_description(webpage), ' - TV2.no')
128
129 return self.playlist_result(entries, playlist_id, title, description)