f5ef856db0155dd84f10d5db4a8cef8e6c08213c
[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 ..compat import (
8     compat_urllib_request,
9 )
10 from ..utils import (
11     ExtractorError,
12     urlencode_postdata,
13     xpath_text,
14     xpath_with_ns,
15 )
16
17 _x = lambda p: xpath_with_ns(p, {'xspf': 'http://xspf.org/ns/0/'})
18
19
20 class NosVideoIE(InfoExtractor):
21     _VALID_URL = r'https?://(?:www\.)?nosvideo\.com/' + \
22                  '(?:embed/|\?v=)(?P<id>[A-Za-z0-9]{12})/?'
23     _PLAYLIST_URL = 'http://nosvideo.com/xml/{xml_id:s}.xml'
24     _FILE_DELETED_REGEX = r'<b>File Not Found</b>'
25     _TEST = {
26         'url': 'http://nosvideo.com/?v=mu8fle7g7rpq',
27         'md5': '6124ed47130d8be3eacae635b071e6b6',
28         'info_dict': {
29             'id': 'mu8fle7g7rpq',
30             'ext': 'mp4',
31             'title': 'big_buck_bunny_480p_surround-fix.avi.mp4',
32             'thumbnail': 're:^https?://.*\.jpg$',
33         }
34     }
35
36     def _real_extract(self, url):
37         video_id = self._match_id(url)
38
39         fields = {
40             'id': video_id,
41             'op': 'download1',
42             'method_free': 'Continue to Video',
43         }
44         req = compat_urllib_request.Request(url, urlencode_postdata(fields))
45         req.add_header('Content-type', 'application/x-www-form-urlencoded')
46         webpage = self._download_webpage(req, video_id,
47                                          'Downloading download page')
48         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
49             raise ExtractorError('Video %s does not exist' % video_id,
50                                  expected=True)
51
52         xml_id = self._search_regex(r'php\|([^\|]+)\|', webpage, 'XML ID')
53         playlist_url = self._PLAYLIST_URL.format(xml_id=xml_id)
54         playlist = self._download_xml(playlist_url, video_id)
55
56         track = playlist.find(_x('.//xspf:track'))
57         if track is None:
58             raise ExtractorError(
59                 'XML playlist is missing the \'track\' element',
60                 expected=True)
61         title = xpath_text(track, _x('./xspf:title'), 'title')
62         url = xpath_text(track, _x('./xspf:file'), 'URL', fatal=True)
63         thumbnail = xpath_text(track, _x('./xspf:image'), 'thumbnail')
64         if title is not None:
65             title = title.strip()
66
67         formats = [{
68             'format_id': 'sd',
69             'url': url,
70         }]
71
72         return {
73             'id': video_id,
74             'title': title,
75             'thumbnail': thumbnail,
76             'formats': formats,
77         }