]>
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 | (?: | |
319fc706 | 21 | (?:program|barn)/(?:[^/]+/|(?:[^\?]+)\?video_id=)| |
50efb383 NJ |
22 | iframe/video/| |
23 | film/| | |
24 | sport/| | |
25 | ) | |
26 | )(?P<id>[0-9]+)''' | |
c58b7ffe | 27 | _GEO_COUNTRIES = ['SE'] |
50efb383 NJ |
28 | _TESTS = [ |
29 | { | |
30 | 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650', | |
1f393a32 | 31 | 'md5': 'cb837212f342d77cec06e6dad190e96d', |
50efb383 NJ |
32 | 'info_dict': { |
33 | 'id': '2491650', | |
34 | 'ext': 'mp4', | |
35 | 'title': 'Kalla Fakta 5 (english subtitles)', | |
ec85ded8 | 36 | 'thumbnail': r're:^https?://.*\.jpg$', |
50efb383 NJ |
37 | 'timestamp': int, |
38 | 'upload_date': '20131125', | |
39 | }, | |
40 | }, | |
41 | { | |
42 | 'url': 'http://www.tv4play.se/iframe/video/3054113', | |
1f393a32 | 43 | 'md5': 'cb837212f342d77cec06e6dad190e96d', |
50efb383 NJ |
44 | 'info_dict': { |
45 | 'id': '3054113', | |
46 | 'ext': 'mp4', | |
47 | 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder', | |
ec85ded8 | 48 | 'thumbnail': r're:^https?://.*\.jpg$', |
50efb383 NJ |
49 | '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.', |
50 | 'timestamp': int, | |
51 | 'upload_date': '20150130', | |
52 | }, | |
53 | }, | |
54 | { | |
55 | 'url': 'http://www.tv4play.se/sport/3060959', | |
56 | 'only_matching': True, | |
57 | }, | |
58 | { | |
59 | 'url': 'http://www.tv4play.se/film/2378136', | |
60 | 'only_matching': True, | |
61 | }, | |
62 | { | |
63 | 'url': 'http://www.tv4play.se/barn/looney-tunes?video_id=3062412', | |
64 | 'only_matching': True, | |
65 | }, | |
319fc706 | 66 | { |
fad9fc53 | 67 | 'url': 'http://www.tv4play.se/program/farang/3922081', |
319fc706 S |
68 | 'only_matching': True, |
69 | } | |
50efb383 NJ |
70 | ] |
71 | ||
72 | def _real_extract(self, url): | |
73 | video_id = self._match_id(url) | |
74 | ||
75 | info = self._download_json( | |
9d8985a1 S |
76 | 'http://www.tv4play.se/player/assets/%s.json' % video_id, |
77 | video_id, 'Downloading video info JSON') | |
50efb383 | 78 | |
9d8985a1 | 79 | title = info['title'] |
50efb383 | 80 | |
1f393a32 | 81 | subtitles = {} |
50efb383 | 82 | formats = [] |
9d8985a1 | 83 | # http formats are linked with unresolvable host |
f1a78ee4 | 84 | for kind in ('hls3', ''): |
9d8985a1 S |
85 | data = self._download_json( |
86 | 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id, | |
87 | video_id, 'Downloading sources JSON', query={ | |
88 | 'protocol': kind, | |
1f393a32 | 89 | 'videoFormat': 'MP4+WEBVTT', |
9d8985a1 | 90 | }) |
1f393a32 RA |
91 | items = try_get(data, lambda x: x['playback']['items']['item']) |
92 | if not items: | |
9d8985a1 | 93 | continue |
1f393a32 RA |
94 | if isinstance(items, dict): |
95 | items = [items] | |
96 | for item in items: | |
97 | manifest_url = item.get('url') | |
98 | if not isinstance(manifest_url, compat_str): | |
99 | continue | |
100 | ext = determine_ext(manifest_url) | |
101 | if ext == 'm3u8': | |
102 | formats.extend(self._extract_m3u8_formats( | |
103 | manifest_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
104 | m3u8_id=kind, fatal=False)) | |
105 | elif ext == 'f4m': | |
106 | formats.extend(self._extract_akamai_formats( | |
107 | manifest_url, video_id, { | |
108 | 'hls': 'tv4play-i.akamaihd.net', | |
109 | })) | |
110 | elif ext == 'webvtt': | |
111 | subtitles = self._merge_subtitles( | |
112 | subtitles, { | |
113 | 'sv': [{ | |
114 | 'url': manifest_url, | |
115 | 'ext': 'vtt', | |
116 | }]}) | |
c58b7ffe S |
117 | |
118 | if not formats and info.get('is_geo_restricted'): | |
119 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES) | |
120 | ||
50efb383 NJ |
121 | self._sort_formats(formats) |
122 | ||
123 | return { | |
124 | 'id': video_id, | |
9d8985a1 | 125 | 'title': title, |
50efb383 | 126 | 'formats': formats, |
1f393a32 | 127 | 'subtitles': subtitles, |
50efb383 NJ |
128 | 'description': info.get('description'), |
129 | 'timestamp': parse_iso8601(info.get('broadcast_date_time')), | |
9d8985a1 | 130 | 'duration': int_or_none(info.get('duration')), |
50efb383 | 131 | 'thumbnail': info.get('image'), |
9d8985a1 | 132 | 'is_live': info.get('is_live') is True, |
50efb383 | 133 | } |