Merge branch 'ceskatelevizesrt' of https://github.com/oskar456/youtube-dl into oskar4...
[youtube-dl] / youtube_dl / extractor / vimple.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import re
6 import xml.etree.ElementTree
7 import zlib
8
9 from .common import InfoExtractor
10 from ..utils import int_or_none
11
12
13 class VimpleIE(InfoExtractor):
14     IE_DESC = 'Vimple.ru'
15     _VALID_URL = r'https?://(player.vimple.ru/iframe|vimple.ru)/(?P<id>[a-f0-9]{10,})'
16     _TESTS = [
17         {
18             'url': 'http://vimple.ru/c0f6b1687dcd4000a97ebe70068039cf',
19             'md5': '2e750a330ed211d3fd41821c6ad9a279',
20             'info_dict': {
21                 'id': 'c0f6b1687dcd4000a97ebe70068039cf',
22                 'ext': 'mp4',
23                 'title': 'Sunset',
24                 'duration': 20,
25                 'thumbnail': 're:https?://.*?\.jpg',
26             },
27         },
28     ]
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32         video_id = mobj.group('id')
33
34         iframe_url = 'http://player.vimple.ru/iframe/%s' % video_id
35
36         iframe = self._download_webpage(
37             iframe_url, video_id,
38             note='Downloading iframe', errnote='unable to fetch iframe')
39         player_url = self._html_search_regex(
40             r'"(http://player.vimple.ru/flash/.+?)"', iframe, 'player url')
41
42         player = self._request_webpage(
43             player_url, video_id, note='Downloading swf player').read()
44
45         player = zlib.decompress(player[8:])
46
47         xml_pieces = re.findall(b'([a-zA-Z0-9 =+/]{500})', player)
48         xml_pieces = [piece[1:-1] for piece in xml_pieces]
49
50         xml_data = b''.join(xml_pieces)
51         xml_data = base64.b64decode(xml_data)
52
53         xml_data = xml.etree.ElementTree.fromstring(xml_data)
54
55         video = xml_data.find('Video')
56         quality = video.get('quality')
57         q_tag = video.find(quality.capitalize())
58
59         formats = [
60             {
61                 'url': q_tag.get('url'),
62                 'tbr': int(q_tag.get('bitrate')),
63                 'filesize': int(q_tag.get('filesize')),
64                 'format_id': quality,
65             },
66         ]
67
68         return {
69             'id': video_id,
70             'title': video.find('Title').text,
71             'formats': formats,
72             'thumbnail': video.find('Poster').get('url'),
73             'duration': int_or_none(video.get('duration')),
74             'webpage_url': video.find('Share').get('videoPageUrl'),
75         }