[tv2] Add extractor (#5724)
[youtube-dl] / youtube_dl / extractor / tv2.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     determine_ext,
7     int_or_none,
8     float_or_none,
9     parse_iso8601,
10 )
11
12
13 class TV2IE(InfoExtractor):
14     _VALID_URL = 'http://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
15     _TEST = {
16         'url': 'http://www.tv2.no/v/916509/',
17         'md5': '9cb9e3410b18b515d71892f27856e9b1',
18         'info_dict': {
19             'id': '916509',
20             'ext': 'flv',
21             'title': 'Se Gryttens hyllest av Steven Gerrard',
22             'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
23             'timestamp': 1431715610,
24             'upload_date': '20150515',
25             'duration': 156.967,
26             'view_count': int,
27             'categories': list,
28         }
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33
34         formats = []
35         format_urls = []
36         for protocol in ('HDS', 'HLS'):
37             data = self._download_json(
38                 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
39                 video_id, 'Downloading play JSON')['playback']
40             for item in data['items']['item']:
41                 video_url = item.get('url')
42                 if not video_url or video_url in format_urls:
43                     continue
44                 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
45                 if not self._is_valid_url(video_url, video_id, format_id):
46                     continue
47                 format_urls.append(video_url)
48                 ext = determine_ext(video_url)
49                 if ext == 'f4m':
50                     formats.extend(self._extract_f4m_formats(
51                         video_url, video_id, f4m_id=format_id))
52                 elif ext == 'm3u8':
53                     formats.extend(self._extract_m3u8_formats(
54                         video_url, video_id, 'mp4', m3u8_id=format_id))
55                 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
56                     pass
57                 else:
58                     formats.append({
59                         'url': video_url,
60                         'format_id': format_id,
61                         'tbr': int_or_none(item.get('bitrate')),
62                         'filesize': int_or_none(item.get('fileSize')),
63                     })
64         self._sort_formats(formats)
65
66         asset = self._download_json(
67             'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
68             video_id, 'Downloading metadata JSON')['asset']
69
70         title = asset['title']
71         description = asset.get('description')
72         timestamp = parse_iso8601(asset.get('createTime'))
73         duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
74         view_count = int_or_none(asset.get('views'))
75         categories = asset.get('keywords', '').split(',')
76
77         thumbnails = [{
78             'id': thumbnail.get('@type'),
79             'url': thumbnail.get('url'),
80         } for _, thumbnail in asset.get('imageVersions', {}).items()]
81
82         return {
83             'id': video_id,
84             'url': video_url,
85             'title': title,
86             'description': description,
87             'thumbnails': thumbnails,
88             'timestamp': timestamp,
89             'duration': duration,
90             'view_count': view_count,
91             'categories': categories,
92             'formats': formats,
93         }