]>
Commit | Line | Data |
---|---|---|
50efb383 NJ |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
9d8985a1 | 5 | from ..compat import compat_str |
50efb383 | 6 | from ..utils import ( |
9d8985a1 | 7 | int_or_none, |
50efb383 | 8 | parse_iso8601, |
9d8985a1 | 9 | try_get, |
1f393a32 | 10 | determine_ext, |
50efb383 NJ |
11 | ) |
12 | ||
13 | ||
14 | class TV4IE(InfoExtractor): | |
15 | IE_DESC = 'tv4.se and tv4play.se' | |
16 | _VALID_URL = r'''(?x)https?://(?:www\.)? | |
17 | (?: | |
18 | tv4\.se/(?:[^/]+)/klipp/(?:.*)-| | |
19 | tv4play\.se/ | |
20 | (?: | |
21 | (?:program|barn)/(?:[^\?]+)\?video_id=| | |
22 | iframe/video/| | |
23 | film/| | |
24 | sport/| | |
25 | ) | |
26 | )(?P<id>[0-9]+)''' | |
27 | _TESTS = [ | |
28 | { | |
29 | 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650', | |
1f393a32 | 30 | 'md5': 'cb837212f342d77cec06e6dad190e96d', |
50efb383 NJ |
31 | 'info_dict': { |
32 | 'id': '2491650', | |
33 | 'ext': 'mp4', | |
34 | 'title': 'Kalla Fakta 5 (english subtitles)', | |
ec85ded8 | 35 | 'thumbnail': r're:^https?://.*\.jpg$', |
50efb383 NJ |
36 | 'timestamp': int, |
37 | 'upload_date': '20131125', | |
38 | }, | |
39 | }, | |
40 | { | |
41 | 'url': 'http://www.tv4play.se/iframe/video/3054113', | |
1f393a32 | 42 | 'md5': 'cb837212f342d77cec06e6dad190e96d', |
50efb383 NJ |
43 | 'info_dict': { |
44 | 'id': '3054113', | |
45 | 'ext': 'mp4', | |
46 | 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder', | |
ec85ded8 | 47 | 'thumbnail': r're:^https?://.*\.jpg$', |
50efb383 NJ |
48 | '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.', |
49 | 'timestamp': int, | |
50 | 'upload_date': '20150130', | |
51 | }, | |
52 | }, | |
53 | { | |
54 | 'url': 'http://www.tv4play.se/sport/3060959', | |
55 | 'only_matching': True, | |
56 | }, | |
57 | { | |
58 | 'url': 'http://www.tv4play.se/film/2378136', | |
59 | 'only_matching': True, | |
60 | }, | |
61 | { | |
62 | 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412', | |
63 | 'only_matching': True, | |
64 | }, | |
65 | ] | |
66 | ||
67 | def _real_extract(self, url): | |
68 | video_id = self._match_id(url) | |
69 | ||
70 | info = self._download_json( | |
9d8985a1 S |
71 | 'http://www.tv4play.se/player/assets/%s.json' % video_id, |
72 | video_id, 'Downloading video info JSON') | |
50efb383 | 73 | |
dfb1b146 | 74 | # If is_geo_restricted is true, it doesn't necessarily mean we can't download it |
9d8985a1 | 75 | if info.get('is_geo_restricted'): |
50efb383 | 76 | self.report_warning('This content might not be available in your country due to licensing restrictions.') |
50efb383 | 77 | |
9d8985a1 | 78 | title = info['title'] |
50efb383 | 79 | |
1f393a32 | 80 | subtitles = {} |
50efb383 | 81 | formats = [] |
9d8985a1 S |
82 | # http formats are linked with unresolvable host |
83 | for kind in ('hls', ''): | |
84 | data = self._download_json( | |
85 | 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id, | |
86 | video_id, 'Downloading sources JSON', query={ | |
87 | 'protocol': kind, | |
1f393a32 | 88 | 'videoFormat': 'MP4+WEBVTT', |
9d8985a1 | 89 | }) |
1f393a32 RA |
90 | items = try_get(data, lambda x: x['playback']['items']['item']) |
91 | if not items: | |
9d8985a1 | 92 | continue |
1f393a32 RA |
93 | if isinstance(items, dict): |
94 | items = [items] | |
95 | for item in items: | |
96 | manifest_url = item.get('url') | |
97 | if not isinstance(manifest_url, compat_str): | |
98 | continue | |
99 | ext = determine_ext(manifest_url) | |
100 | if ext == 'm3u8': | |
101 | formats.extend(self._extract_m3u8_formats( | |
102 | manifest_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
103 | m3u8_id=kind, fatal=False)) | |
104 | elif ext == 'f4m': | |
105 | formats.extend(self._extract_akamai_formats( | |
106 | manifest_url, video_id, { | |
107 | 'hls': 'tv4play-i.akamaihd.net', | |
108 | })) | |
109 | elif ext == 'webvtt': | |
110 | subtitles = self._merge_subtitles( | |
111 | subtitles, { | |
112 | 'sv': [{ | |
113 | 'url': manifest_url, | |
114 | 'ext': 'vtt', | |
115 | }]}) | |
50efb383 NJ |
116 | self._sort_formats(formats) |
117 | ||
118 | return { | |
119 | 'id': video_id, | |
9d8985a1 | 120 | 'title': title, |
50efb383 | 121 | 'formats': formats, |
1f393a32 | 122 | 'subtitles': subtitles, |
50efb383 NJ |
123 | 'description': info.get('description'), |
124 | 'timestamp': parse_iso8601(info.get('broadcast_date_time')), | |
9d8985a1 | 125 | 'duration': int_or_none(info.get('duration')), |
50efb383 | 126 | 'thumbnail': info.get('image'), |
9d8985a1 | 127 | 'is_live': info.get('is_live') is True, |
50efb383 | 128 | } |