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