[indavideo] Split in two extractors, extract all formats and fix timestamp
[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         video_id = video['id']
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         formats = [{
64             'url': video_url,
65             'height': self._search_regex(r'\.(\d{3,4})\.mp4$', video_url, 'height', default=None),
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', [])]
79
80         return {
81             'id': 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
95 class IndavideoIE(InfoExtractor):
96     _VALID_URL = r'https?://(?:www\.)?indavideo\.hu/video/(?P<id>[^/#?]+)'
97     _TEST = {
98         'url': 'http://indavideo.hu/video/Vicces_cica_1',
99         'md5': '8c82244ba85d2a2310275b318eb51eac',
100         'info_dict': {
101             'id': '1335611',
102             'display_id': 'Vicces_cica_1',
103             'ext': 'mp4',
104             'title': 'Vicces cica',
105             'description': 'Játszik a tablettel. :D',
106             'thumbnail': 're:^https?://.*\.jpg$',
107             'uploader': 'Jet_Pack',
108             'uploader_id': '491217',
109             'timestamp': 1390821212,
110             'upload_date': '20140127',
111             'duration': 7,
112             'age_limit': 0,
113             'tags': ['vicces', 'macska', 'cica', 'ügyes', 'nevetés', 'játszik', 'Cukiság', 'Jet_Pack'],
114         },
115     }
116
117     def _real_extract(self, url):
118         display_id = self._match_id(url)
119
120         webpage = self._download_webpage(url, display_id)
121         embed_url = self._search_regex(
122             r'<link[^>]+rel="video_src"[^>]+href="(.+?)"', webpage, 'embed url')
123
124         return {
125             '_type': 'url_transparent',
126             'ie_key': 'IndavideoEmbed',
127             'url': embed_url,
128             'display_id': display_id,
129         }