2 from __future__ import unicode_literals
6 from .common import InfoExtractor
18 class TV2IE(InfoExtractor):
19 _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
21 'url': 'http://www.tv2.no/v/916509/',
25 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
26 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
27 'timestamp': 1431715610,
28 'upload_date': '20150515',
35 'skip_download': True,
39 def _real_extract(self, url):
40 video_id = self._match_id(url)
44 for protocol in ('HDS', 'HLS'):
45 data = self._download_json(
46 'http://sumo.tv2.no/api/web/asset/%s/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % (video_id, protocol),
47 video_id, 'Downloading play JSON')['playback']
48 items = try_get(data, lambda x: x['items']['item'])
51 if not isinstance(items, list):
54 if not isinstance(item, dict):
56 video_url = item.get('url')
57 if not video_url or video_url in format_urls:
59 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
60 if not self._is_valid_url(video_url, video_id, format_id):
62 format_urls.append(video_url)
63 ext = determine_ext(video_url)
65 formats.extend(self._extract_f4m_formats(
66 video_url, video_id, f4m_id=format_id, fatal=False))
68 formats.extend(self._extract_m3u8_formats(
69 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
70 m3u8_id=format_id, fatal=False))
71 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
76 'format_id': format_id,
77 'tbr': int_or_none(item.get('bitrate')),
78 'filesize': int_or_none(item.get('fileSize')),
80 self._sort_formats(formats)
82 asset = self._download_json(
83 'http://sumo.tv2.no/api/web/asset/%s.json' % video_id,
84 video_id, 'Downloading metadata JSON')['asset']
86 title = asset['title']
87 description = asset.get('description')
88 timestamp = parse_iso8601(asset.get('createTime'))
89 duration = float_or_none(asset.get('accurateDuration') or asset.get('duration'))
90 view_count = int_or_none(asset.get('views'))
91 categories = asset.get('keywords', '').split(',')
94 'id': thumbnail.get('@type'),
95 'url': thumbnail.get('url'),
96 } for _, thumbnail in asset.get('imageVersions', {}).items()]
102 'description': description,
103 'thumbnails': thumbnails,
104 'timestamp': timestamp,
105 'duration': duration,
106 'view_count': view_count,
107 'categories': categories,
112 class TV2ArticleIE(InfoExtractor):
113 _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
115 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
118 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
119 'description': 'md5:339573779d3eea3542ffe12006190954',
123 'url': 'http://www.tv2.no/a/6930542',
124 'only_matching': True,
127 def _real_extract(self, url):
128 playlist_id = self._match_id(url)
130 webpage = self._download_webpage(url, playlist_id)
132 # Old embed pattern (looks unused nowadays)
133 assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
137 for v in re.findall(r'TV2ContentboxVideo\(({.+?})\)', webpage):
138 video = self._parse_json(
139 v, playlist_id, transform_source=js_to_json, fatal=False)
142 asset = video.get('assetId')
147 self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
148 for asset_id in assets]
150 title = remove_end(self._og_search_title(webpage), ' - TV2.no')
151 description = remove_end(self._og_search_description(webpage), ' - TV2.no')
153 return self.playlist_result(entries, playlist_id, title, description)