]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/tv4.py
Prepare new release.
[youtubedl] / youtube_dl / extractor / tv4.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7 int_or_none,
8 parse_iso8601,
9 try_get,
10 determine_ext,
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 _GEO_COUNTRIES = ['SE']
28 _TESTS = [
29 {
30 'url': 'http://www.tv4.se/kalla-fakta/klipp/kalla-fakta-5-english-subtitles-2491650',
31 'md5': 'cb837212f342d77cec06e6dad190e96d',
32 'info_dict': {
33 'id': '2491650',
34 'ext': 'mp4',
35 'title': 'Kalla Fakta 5 (english subtitles)',
36 'thumbnail': r're:^https?://.*\.jpg$',
37 'timestamp': int,
38 'upload_date': '20131125',
39 },
40 },
41 {
42 'url': 'http://www.tv4play.se/iframe/video/3054113',
43 'md5': 'cb837212f342d77cec06e6dad190e96d',
44 'info_dict': {
45 'id': '3054113',
46 'ext': 'mp4',
47 'title': 'Så här jobbar ficktjuvarna - se avslöjande bilder',
48 'thumbnail': r're:^https?://.*\.jpg$',
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 },
66 ]
67
68 def _real_extract(self, url):
69 video_id = self._match_id(url)
70
71 info = self._download_json(
72 'http://www.tv4play.se/player/assets/%s.json' % video_id,
73 video_id, 'Downloading video info JSON')
74
75 title = info['title']
76
77 subtitles = {}
78 formats = []
79 # http formats are linked with unresolvable host
80 for kind in ('hls3', ''):
81 data = self._download_json(
82 'https://prima.tv4play.se/api/web/asset/%s/play.json' % video_id,
83 video_id, 'Downloading sources JSON', query={
84 'protocol': kind,
85 'videoFormat': 'MP4+WEBVTT',
86 })
87 items = try_get(data, lambda x: x['playback']['items']['item'])
88 if not items:
89 continue
90 if isinstance(items, dict):
91 items = [items]
92 for item in items:
93 manifest_url = item.get('url')
94 if not isinstance(manifest_url, compat_str):
95 continue
96 ext = determine_ext(manifest_url)
97 if ext == 'm3u8':
98 formats.extend(self._extract_m3u8_formats(
99 manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
100 m3u8_id=kind, fatal=False))
101 elif ext == 'f4m':
102 formats.extend(self._extract_akamai_formats(
103 manifest_url, video_id, {
104 'hls': 'tv4play-i.akamaihd.net',
105 }))
106 elif ext == 'webvtt':
107 subtitles = self._merge_subtitles(
108 subtitles, {
109 'sv': [{
110 'url': manifest_url,
111 'ext': 'vtt',
112 }]})
113
114 if not formats and info.get('is_geo_restricted'):
115 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
116
117 self._sort_formats(formats)
118
119 return {
120 'id': video_id,
121 'title': title,
122 'formats': formats,
123 'subtitles': subtitles,
124 'description': info.get('description'),
125 'timestamp': parse_iso8601(info.get('broadcast_date_time')),
126 'duration': int_or_none(info.get('duration')),
127 'thumbnail': info.get('image'),
128 'is_live': info.get('is_live') is True,
129 }