]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/tv4.py
Imported Upstream version 2015.11.10
[youtubedl] / youtube_dl / extractor / tv4.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 parse_iso8601,
8 )
9
10
11 class TV4IE(InfoExtractor):
12 IE_DESC = 'tv4.se and tv4play.se'
13 _VALID_URL = r'''(?x)https?://(?:www\.)?
14 (?:
15 tv4\.se/(?:[^/]+)/klipp/(?:.*)-|
16 tv4play\.se/
17 (?:
18 (?:program|barn)/(?:[^\?]+)\?video_id=|
19 iframe/video/|
20 film/|
21 sport/|
22 )
23 )(?P<id>[0-9]+)'''
24 _TESTS = [
25 {
26 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
27 'md5': '909d6454b87b10a25aa04c4bdd416a9b',
28 'info_dict': {
29 'id': '2491650',
30 'ext': 'mp4',
31 'title': 'Kalla Fakta 5 (english subtitles)',
32 'thumbnail': 're:^https?://.*\.jpg$',
33 'timestamp': int,
34 'upload_date': '20131125',
35 },
36 },
37 {
38 'url': 'http://www.tv4play.se/iframe/video/3054113',
39 'md5': '77f851c55139ffe0ebd41b6a5552489b',
40 'info_dict': {
41 'id': '3054113',
42 'ext': 'mp4',
43 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
44 'thumbnail': 're:^https?://.*\.jpg$',
45 'description': 'Unika bilder avslöjar hur turisternas fickor vittjas mitt på Stockholms central. Två experter på ficktjuvarna avslöjar knepen du ska se upp för.',
46 'timestamp': int,
47 'upload_date': '20150130',
48 },
49 },
50 {
51 'url': 'http://www.tv4play.se/sport/3060959',
52 'only_matching': True,
53 },
54 {
55 'url': 'http://www.tv4play.se/film/2378136',
56 'only_matching': True,
57 },
58 {
59 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412',
60 'only_matching': True,
61 },
62 ]
63
64 def _real_extract(self, url):
65 video_id = self._match_id(url)
66
67 info = self._download_json(
68 'http://www.tv4play.se/player/assets/%s.json' % video_id, video_id, 'Downloading video info JSON')
69
70 # If is_geo_restricted is true, it doesn't neceserally mean we can't download it
71 if info['is_geo_restricted']:
72 self.report_warning('This content might not be available in your country due to licensing restrictions.')
73 if info['requires_subscription']:
74 raise ExtractorError('This content requires subscription.', expected=True)
75
76 sources_data = self._download_json(
77 'https://prima.tv4play.se/api/web/asset/%s/play.json?protocol=http&videoFormat=MP4' % video_id, video_id, 'Downloading sources JSON')
78 sources = sources_data['playback']
79
80 formats = []
81 for item in sources.get('items', {}).get('item', []):
82 ext, bitrate = item['mediaFormat'], item['bitrate']
83 formats.append({
84 'format_id': '%s_%s' % (ext, bitrate),
85 'tbr': bitrate,
86 'ext': ext,
87 'url': item['url'],
88 })
89 self._sort_formats(formats)
90
91 return {
92 'id': video_id,
93 'title': info['title'],
94 'formats': formats,
95 'description': info.get('description'),
96 'timestamp': parse_iso8601(info.get('broadcast_date_time')),
97 'duration': info.get('duration'),
98 'thumbnail': info.get('image'),
99 'is_live': sources.get('live'),
100 }