[BehindKink] Minor fixes
[youtube-dl] / youtube_dl / extractor / nosvideo.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     compat_urllib_parse,
9     compat_urllib_request,
10     xpath_with_ns,
11 )
12
13 _x = lambda p: xpath_with_ns(p, {'xspf': 'http://xspf.org/ns/0/'})
14 _find = lambda el, p: el.find(_x(p)).text.strip()
15
16
17 class NosVideoIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?nosvideo\.com/' + \
19                  '(?:embed/|\?v=)(?P<id>[A-Za-z0-9]{12})/?'
20     _PLAYLIST_URL = 'http://nosvideo.com/xml/{xml_id:s}.xml'
21     _TEST = {
22         'url': 'http://nosvideo.com/?v=drlp6s40kg54',
23         'md5': '4b4ac54c6ad5d70ab88f2c2c6ccec71c',
24         'info_dict': {
25             'id': 'drlp6s40kg54',
26             'ext': 'mp4',
27             'title': 'big_buck_bunny_480p_surround-fix.avi.mp4',
28             'thumbnail': 're:^https?://.*\.jpg$',
29         }
30     }
31
32     def _real_extract(self, url):
33         mobj = re.match(self._VALID_URL, url)
34         video_id = mobj.group('id')
35
36         fields = {
37             'id': video_id,
38             'op': 'download1',
39             'method_free': 'Continue to Video',
40         }
41         post = compat_urllib_parse.urlencode(fields)
42         req = compat_urllib_request.Request(url, post)
43         req.add_header('Content-type', 'application/x-www-form-urlencoded')
44         webpage = self._download_webpage(req, video_id,
45                                          'Downloading download page')
46         xml_id = self._search_regex(r'php\|([^\|]+)\|', webpage, 'XML ID')
47         playlist_url = self._PLAYLIST_URL.format(xml_id=xml_id)
48         playlist = self._download_xml(playlist_url, video_id)
49
50         track = playlist.find(_x('.//xspf:track'))
51         title = _find(track, './xspf:title')
52         url = _find(track, './xspf:file')
53         thumbnail = _find(track, './xspf:image')
54
55         formats = [{
56             'format_id': 'sd',
57             'url': url,
58         }]
59
60         return {
61             'id': video_id,
62             'title': title,
63             'thumbnail': thumbnail,
64             'formats': formats,
65         }