Verified with pep8 and pyflakes
[youtube-dl] / youtube_dl / extractor / vimple.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 import re
4 import zlib
5 import base64
6 import xml.etree.ElementTree
7
8 from .common import InfoExtractor
9
10
11 class VimpleIE(InfoExtractor):
12     IE_DESC = 'Vimple.ru'
13     _VALID_URL = r'https?://(player.vimple.ru/iframe|vimple.ru)/(?P<id>[a-f0-9]{10,})'
14     _TESTS = [
15         # Quality: Large, from iframe
16         {
17             'url': 'http://player.vimple.ru/iframe/b132bdfd71b546d3972f9ab9a25f201c',
18             'info_dict': {
19                 'id': 'b132bdfd71b546d3972f9ab9a25f201c',
20                 'title': 'great-escape-minecraft.flv',
21                 'ext': 'mp4',
22                 'duration': 352,
23                 'webpage_url': 'http://vimple.ru/b132bdfd71b546d3972f9ab9a25f201c',
24             },
25         },
26         # Quality: Medium, from mainpage
27         {
28             'url': 'http://vimple.ru/a15950562888453b8e6f9572dc8600cd',
29             'info_dict': {
30                 'id': 'a15950562888453b8e6f9572dc8600cd',
31                 'title': 'DB 01',
32                 'ext': 'flv',
33                 'duration': 1484,
34                 'webpage_url': 'http://vimple.ru/a15950562888453b8e6f9572dc8600cd',
35             }
36         },
37     ]
38
39     # http://jsunpack-n.googlecode.com/svn-history/r63/trunk/swf.py
40
41     def _real_extract(self, url):
42         mobj = re.match(self._VALID_URL, url)
43         video_id = mobj.group('id')
44
45         iframe_url = 'http://player.vimple.ru/iframe/%s' % video_id
46
47         iframe = self._download_webpage(iframe_url, video_id, note='Downloading iframe', errnote='unable to fetch iframe')
48         player_url = self._html_search_regex(r'"(http://player.vimple.ru/flash/.+?)"', iframe, 'player url')
49
50         player = self._request_webpage(player_url, video_id, note='Downloading swf player').read()
51
52         # http://stackoverflow.com/a/6804758
53         # http://stackoverflow.com/a/12073686
54         player = zlib.decompress(player[8:])
55
56         xml_pieces = re.findall(b'([a-zA-Z0-9 =+/]{500})', player)
57         xml_pieces = [piece[1:-1] for piece in xml_pieces]
58
59         xml_data = b''.join(xml_pieces)
60         xml_data = base64.b64decode(xml_data)
61
62         xml_data = xml.etree.ElementTree.fromstring(xml_data)
63
64         video = xml_data.find('Video')
65         quality = video.get('quality')
66         q_tag = video.find(quality.capitalize())
67
68         formats = [
69             {
70                 'url': q_tag.get('url'),
71                 'tbr': int(q_tag.get('bitrate')),
72                 'filesize': int(q_tag.get('filesize')),
73                 'format_id': quality,
74             },
75         ]
76
77         return {
78             'id': video_id,
79             'title': video.find('Title').text,
80             'formats': formats,
81             'thumbnail': video.find('Poster').get('url'),
82             'duration': int(video.get('duration')),
83             'webpage_url': video.find('Share').get('videoPageUrl'),
84         }