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