]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/tv2.py
[cbs] extract smpte and vtt subtitles
[youtube-dl.git] / youtube_dl / extractor / tv2.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     float_or_none,
11     js_to_json,
12     parse_iso8601,
13     remove_end,
14 )
15
16
17 class TV2IE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
19     _TEST = {
20         'url': 'http://www.tv2.no/v/916509/',
21         'info_dict': {
22             'id': '916509',
23             'ext': 'mp4',
24             'title': 'Se Frode 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         'params': {
33             # m3u8 download
34             'skip_download': True,
35         },
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, fatal=False))
59                 elif ext == 'm3u8':
60                     formats.extend(self._extract_m3u8_formats(
61                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
62                         m3u8_id=format_id, fatal=False))
63                 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
64                     pass
65                 else:
66                     formats.append({
67                         'url': video_url,
68                         'format_id': format_id,
69                         'tbr': int_or_none(item.get('bitrate')),
70                         'filesize': int_or_none(item.get('fileSize')),
71                     })
72         self._sort_formats(formats)
73
74         asset = self._download_json(
75             'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
76             video_id, 'Downloading metadata JSON')['asset']
77
78         title = asset['title']
79         description = asset.get('description')
80         timestamp = parse_iso8601(asset.get('createTime'))
81         duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
82         view_count = int_or_none(asset.get('views'))
83         categories = asset.get('keywords', '').split(',')
84
85         thumbnails = [{
86             'id': thumbnail.get('@type'),
87             'url': thumbnail.get('url'),
88         } for _, thumbnail in asset.get('imageVersions', {}).items()]
89
90         return {
91             'id': video_id,
92             'url': video_url,
93             'title': title,
94             'description': description,
95             'thumbnails': thumbnails,
96             'timestamp': timestamp,
97             'duration': duration,
98             'view_count': view_count,
99             'categories': categories,
100             'formats': formats,
101         }
102
103
104 class TV2ArticleIE(InfoExtractor):
105     _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
106     _TESTS = [{
107         'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
108         'info_dict': {
109             'id': '6930542',
110             'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
111             'description': 'md5:339573779d3eea3542ffe12006190954',
112         },
113         'playlist_count': 2,
114     }, {
115         'url': 'http://www.tv2.no/a/6930542',
116         'only_matching': True,
117     }]
118
119     def _real_extract(self, url):
120         playlist_id = self._match_id(url)
121
122         webpage = self._download_webpage(url, playlist_id)
123
124         # Old embed pattern (looks unused nowadays)
125         assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
126
127         if not assets:
128             # New embed pattern
129             for v in re.findall(r'TV2ContentboxVideo\(({.+?})\)', webpage):
130                 video = self._parse_json(
131                     v, playlist_id, transform_source=js_to_json, fatal=False)
132                 if not video:
133                     continue
134                 asset = video.get('assetId')
135                 if asset:
136                     assets.append(asset)
137
138         entries = [
139             self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
140             for asset_id in assets]
141
142         title = remove_end(self._og_search_title(webpage), ' - TV2.no')
143         description = remove_end(self._og_search_description(webpage), ' - TV2.no')
144
145         return self.playlist_result(entries, playlist_id, title, description)