039add86b2d99aa73e886e2e00bab97748a33ded
[youtube-dl] / youtube_dl / extractor / videa.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     int_or_none,
7     mimetype2ext,
8     parse_codecs,
9     xpath_element,
10     xpath_text,
11 )
12
13
14 class VideaIE(InfoExtractor):
15     _VALID_URL = r'''(?x)
16                     https?://
17                         videa\.hu/
18                         (?:
19                             videok/(?:[^/]+/)*[^?#&]+-|
20                             player\?.*?\bv=|
21                             player/v/
22                         )
23                         (?P<id>[^?#&]+)
24                     '''
25     _TESTS = [{
26         'url': 'http://videa.hu/videok/allatok/az-orult-kigyasz-285-kigyot-kigyo-8YfIAjxwWGwT8HVQ',
27         'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
28         'info_dict': {
29             'id': '8YfIAjxwWGwT8HVQ',
30             'ext': 'mp4',
31             'title': 'Az őrült kígyász 285 kígyót enged szabadon',
32             'thumbnail': 'http://videa.hu/static/still/1.4.1.1007274.1204470.3',
33             'duration': 21,
34         },
35     }, {
36         'url': 'http://videa.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
37         'only_matching': True,
38     }, {
39         'url': 'http://videa.hu/player?v=8YfIAjxwWGwT8HVQ',
40         'only_matching': True,
41     }, {
42         'url': 'http://videa.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
43         'only_matching': True,
44     }]
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48
49         info = self._download_xml(
50             'http://videa.hu/videaplayer_get_xml.php', video_id,
51             query={'v': video_id})
52
53         video = xpath_element(info, './/video', 'video', fatal=True)
54         sources = xpath_element(info, './/video_sources', 'sources', fatal=True)
55
56         title = xpath_text(video, './title', fatal=True)
57
58         formats = []
59         for source in sources.findall('./video_source'):
60             source_url = source.text
61             if not source_url:
62                 continue
63             f = parse_codecs(source.get('codecs'))
64             f.update({
65                 'url': source_url,
66                 'ext': mimetype2ext(source.get('mimetype')) or 'mp4',
67                 'format_id': source.get('name'),
68                 'width': int_or_none(source.get('width')),
69                 'height': int_or_none(source.get('height')),
70             })
71             formats.append(f)
72         self._sort_formats(formats)
73
74         thumbnail = xpath_text(video, './poster_src')
75         duration = int_or_none(xpath_text(video, './duration'))
76
77         age_limit = None
78         is_adult = xpath_text(video, './is_adult_content', default=None)
79         if is_adult:
80             age_limit = 18 if is_adult == '1' else 0
81
82         return {
83             'id': video_id,
84             'title': title,
85             'thumbnail': thumbnail,
86             'duration': duration,
87             'age_limit': age_limit,
88             'formats': formats,
89         }