Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into mrkolby-vgtv
[youtube-dl] / youtube_dl / extractor / promptfile.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     ExtractorError,
9     determine_ext,
10     compat_urllib_parse,
11     compat_urllib_request,
12 )
13
14
15 class PromptFileIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?promptfile\.com/l/(?P<id>[0-9A-Z\-]+)'
17     _FILE_NOT_FOUND_REGEX = r'<div.+id="not_found_msg".+>.+</div>[^-]'
18     _TEST = {
19         'url': 'http://www.promptfile.com/l/D21B4746E9-F01462F0FF',
20         'md5': 'd1451b6302da7215485837aaea882c4c',
21         'info_dict': {
22             'id': 'D21B4746E9-F01462F0FF',
23             'ext': 'mp4',
24             'title': 'Birds.mp4',
25             'thumbnail': 're:^https?://.*\.jpg$',
26         }
27     }
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         video_id = mobj.group('id')
32         webpage = self._download_webpage(url, video_id)
33
34         if re.search(self._FILE_NOT_FOUND_REGEX, webpage) is not None:
35             raise ExtractorError('Video %s does not exist' % video_id,
36                                  expected=True)
37
38         fields = dict(re.findall(r'''(?x)type="hidden"\s+
39             name="(.+?)"\s+
40             value="(.*?)"
41             ''', webpage))
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(
46             req, video_id, 'Downloading video page')
47
48         url = self._html_search_regex(r'url:\s*\'([^\']+)\'', webpage, 'URL')
49         title = self._html_search_regex(
50             r'<span.+title="([^"]+)">', webpage, 'title')
51         thumbnail = self._html_search_regex(
52             r'<div id="player_overlay">.*button>.*?<img src="([^"]+)"',
53             webpage, 'thumbnail', fatal=False, flags=re.DOTALL)
54
55         formats = [{
56             'format_id': 'sd',
57             'url': url,
58             'ext': determine_ext(title),
59         }]
60         self._sort_formats(formats)
61
62         return {
63             'id': video_id,
64             'title': title,
65             'thumbnail': thumbnail,
66             'formats': formats,
67         }