[indavideo] Sign download URLs
[youtube-dl] / youtube_dl / extractor / indavideo.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_age_limit,
9     parse_iso8601,
10     update_url_query,
11 )
12
13
14 class IndavideoEmbedIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:(?:embed\.)?indavideo\.hu/player/video/|assets\.indavideo\.hu/swf/player\.swf\?.*\b(?:v(?:ID|id))=)(?P<id>[\da-f]+)'
16     _TESTS = [{
17         'url': 'http://indavideo.hu/player/video/1bdc3c6d80/',
18         'md5': 'f79b009c66194acacd40712a6778acfa',
19         'info_dict': {
20             'id': '1837039',
21             'ext': 'mp4',
22             'title': 'Cicatánc',
23             'description': '',
24             'thumbnail': r're:^https?://.*\.jpg$',
25             'uploader': 'cukiajanlo',
26             'uploader_id': '83729',
27             'timestamp': 1439193826,
28             'upload_date': '20150810',
29             'duration': 72,
30             'age_limit': 0,
31             'tags': ['tánc', 'cica', 'cuki', 'cukiajanlo', 'newsroom'],
32         },
33     }, {
34         'url': 'http://embed.indavideo.hu/player/video/1bdc3c6d80?autostart=1&hide=1',
35         'only_matching': True,
36     }, {
37         'url': 'http://assets.indavideo.hu/swf/player.swf?v=fe25e500&vID=1bdc3c6d80&autostart=1&hide=1&i=1',
38         'only_matching': True,
39     }]
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43
44         video = self._download_json(
45             'http://amfphp.indavideo.hu/SYm0json.php/player.playerHandler.getVideoData/%s' % video_id,
46             video_id)['data']
47
48         title = video['title']
49
50         video_urls = video.get('video_files', [])
51         video_file = video.get('video_file')
52         if video:
53             video_urls.append(video_file)
54         video_urls = list(set(video_urls))
55
56         video_prefix = video_urls[0].rsplit('/', 1)[0]
57
58         for flv_file in video.get('flv_files', []):
59             flv_url = '%s/%s' % (video_prefix, flv_file)
60             if flv_url not in video_urls:
61                 video_urls.append(flv_url)
62
63         filesh = video.get('filesh')
64         formats = [
65             self.video_url_to_format(video_url, filesh)
66             for video_url in video_urls]
67         self._sort_formats(formats)
68
69         timestamp = video.get('date')
70         if timestamp:
71             # upload date is in CEST
72             timestamp = parse_iso8601(timestamp + ' +0200', ' ')
73
74         thumbnails = [{
75             'url': self._proto_relative_url(thumbnail)
76         } for thumbnail in video.get('thumbnails', [])]
77
78         tags = [tag['title'] for tag in video.get('tags') or []]
79
80         return {
81             'id': video.get('id') or video_id,
82             'title': title,
83             'description': video.get('description'),
84             'thumbnails': thumbnails,
85             'uploader': video.get('user_name'),
86             'uploader_id': video.get('user_id'),
87             'timestamp': timestamp,
88             'duration': int_or_none(video.get('length')),
89             'age_limit': parse_age_limit(video.get('age_limit')),
90             'tags': tags,
91             'formats': formats,
92         }
93
94     def video_url_to_format(self, video_url, filesh):
95         height = int_or_none(self._search_regex(
96             r'\.(\d{3,4})\.mp4(?:\?|$)', video_url, 'height', default=None))
97         if height and filesh:
98             token = filesh.get(compat_str(height))
99             if token is not None:
100                 video_url = update_url_query(video_url, {'token': token})
101         return {
102             'url': video_url,
103             'height': height,
104         }
105
106
107 class IndavideoIE(InfoExtractor):
108     _VALID_URL = r'https?://(?:.+?\.)?indavideo\.hu/video/(?P<id>[^/#?]+)'
109     _TESTS = [{
110         'url': 'http://indavideo.hu/video/Vicces_cica_1',
111         'md5': '8c82244ba85d2a2310275b318eb51eac',
112         'info_dict': {
113             'id': '1335611',
114             'display_id': 'Vicces_cica_1',
115             'ext': 'mp4',
116             'title': 'Vicces cica',
117             'description': 'Játszik a tablettel. :D',
118             'thumbnail': r're:^https?://.*\.jpg$',
119             'uploader': 'Jet_Pack',
120             'uploader_id': '491217',
121             'timestamp': 1390821212,
122             'upload_date': '20140127',
123             'duration': 7,
124             'age_limit': 0,
125             'tags': ['vicces', 'macska', 'cica', 'ügyes', 'nevetés', 'játszik', 'Cukiság', 'Jet_Pack'],
126         },
127     }, {
128         'url': 'http://index.indavideo.hu/video/2015_0728_beregszasz',
129         'only_matching': True,
130     }, {
131         'url': 'http://auto.indavideo.hu/video/Sajat_utanfutoban_a_kis_tacsko',
132         'only_matching': True,
133     }, {
134         'url': 'http://erotika.indavideo.hu/video/Amator_tini_punci',
135         'only_matching': True,
136     }, {
137         'url': 'http://film.indavideo.hu/video/f_hrom_nagymamm_volt',
138         'only_matching': True,
139     }, {
140         'url': 'http://palyazat.indavideo.hu/video/Embertelen_dal_Dodgem_egyuttes',
141         'only_matching': True,
142     }]
143
144     def _real_extract(self, url):
145         display_id = self._match_id(url)
146
147         webpage = self._download_webpage(url, display_id)
148         embed_url = self._search_regex(
149             r'<link[^>]+rel="video_src"[^>]+href="(.+?)"', webpage, 'embed url')
150
151         return {
152             '_type': 'url_transparent',
153             'ie_key': 'IndavideoEmbed',
154             'url': embed_url,
155             'display_id': display_id,
156         }