[indavideo] Fix extraction (closes #11221)
[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': 'c8a507a1c7410685f83a06eaeeaafeab',
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 = []
51
52         video_files = video.get('video_files')
53         if isinstance(video_files, list):
54             video_urls.extend(video_files)
55         elif isinstance(video_files, dict):
56             video_urls.extend(video_files.values())
57
58         video_file = video.get('video_file')
59         if video:
60             video_urls.append(video_file)
61         video_urls = list(set(video_urls))
62
63         video_prefix = video_urls[0].rsplit('/', 1)[0]
64
65         for flv_file in video.get('flv_files', []):
66             flv_url = '%s/%s' % (video_prefix, flv_file)
67             if flv_url not in video_urls:
68                 video_urls.append(flv_url)
69
70         filesh = video.get('filesh')
71
72         formats = []
73         for video_url in video_urls:
74             height = int_or_none(self._search_regex(
75                 r'\.(\d{3,4})\.mp4(?:\?|$)', video_url, 'height', default=None))
76             if filesh:
77                 if not height:
78                     continue
79                 token = filesh.get(compat_str(height))
80                 if token is None:
81                     continue
82                 video_url = update_url_query(video_url, {'token': token})
83             formats.append({
84                 'url': video_url,
85                 'height': height,
86             })
87         self._sort_formats(formats)
88
89         timestamp = video.get('date')
90         if timestamp:
91             # upload date is in CEST
92             timestamp = parse_iso8601(timestamp + ' +0200', ' ')
93
94         thumbnails = [{
95             'url': self._proto_relative_url(thumbnail)
96         } for thumbnail in video.get('thumbnails', [])]
97
98         tags = [tag['title'] for tag in video.get('tags') or []]
99
100         return {
101             'id': video.get('id') or video_id,
102             'title': title,
103             'description': video.get('description'),
104             'thumbnails': thumbnails,
105             'uploader': video.get('user_name'),
106             'uploader_id': video.get('user_id'),
107             'timestamp': timestamp,
108             'duration': int_or_none(video.get('length')),
109             'age_limit': parse_age_limit(video.get('age_limit')),
110             'tags': tags,
111             'formats': formats,
112         }
113
114
115 class IndavideoIE(InfoExtractor):
116     _VALID_URL = r'https?://(?:.+?\.)?indavideo\.hu/video/(?P<id>[^/#?]+)'
117     _TESTS = [{
118         'url': 'http://indavideo.hu/video/Vicces_cica_1',
119         'md5': '8c82244ba85d2a2310275b318eb51eac',
120         'info_dict': {
121             'id': '1335611',
122             'display_id': 'Vicces_cica_1',
123             'ext': 'mp4',
124             'title': 'Vicces cica',
125             'description': 'Játszik a tablettel. :D',
126             'thumbnail': r're:^https?://.*\.jpg$',
127             'uploader': 'Jet_Pack',
128             'uploader_id': '491217',
129             'timestamp': 1390821212,
130             'upload_date': '20140127',
131             'duration': 7,
132             'age_limit': 0,
133             'tags': list,
134         },
135     }, {
136         'url': 'http://index.indavideo.hu/video/2015_0728_beregszasz',
137         'only_matching': True,
138     }, {
139         'url': 'http://auto.indavideo.hu/video/Sajat_utanfutoban_a_kis_tacsko',
140         'only_matching': True,
141     }, {
142         'url': 'http://erotika.indavideo.hu/video/Amator_tini_punci',
143         'only_matching': True,
144     }, {
145         'url': 'http://film.indavideo.hu/video/f_hrom_nagymamm_volt',
146         'only_matching': True,
147     }, {
148         'url': 'http://palyazat.indavideo.hu/video/Embertelen_dal_Dodgem_egyuttes',
149         'only_matching': True,
150     }]
151
152     def _real_extract(self, url):
153         display_id = self._match_id(url)
154
155         webpage = self._download_webpage(url, display_id)
156         embed_url = self._search_regex(
157             (r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//embed\.indavideo\.hu/player/video/.+?)\1',
158              r'<link[^>]+rel="video_src"[^>]+href="(?P<url>.+?)"'),
159             webpage, 'embed url', group='url')
160
161         return {
162             '_type': 'url_transparent',
163             'ie_key': 'IndavideoEmbed',
164             'url': embed_url,
165             'display_id': display_id,
166         }