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