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