[vgtv] Add new extractor
[youtube-dl] / youtube_dl / extractor / firedrive.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     compat_urllib_parse,
10     compat_urllib_request,
11     determine_ext,
12 )
13
14
15 class FiredriveIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?firedrive\.com/' + \
17                  '(?:file|embed)/(?P<id>[0-9a-zA-Z]+)'
18     _FILE_DELETED_REGEX = r'<div class="removed_file_image">'
19
20     _TESTS = [{
21         'url': 'https://www.firedrive.com/file/FEB892FA160EBD01',
22         'md5': 'd5d4252f80ebeab4dc2d5ceaed1b7970',
23         'info_dict': {
24             'id': 'FEB892FA160EBD01',
25             'ext': 'flv',
26             'title': 'bbb_theora_486kbit.flv',
27             'thumbnail': 're:^http://.*\.jpg$',
28         },
29     }]
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('id')
34
35         url = 'http://firedrive.com/file/%s' % video_id
36
37         webpage = self._download_webpage(url, video_id)
38
39         if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
40             raise ExtractorError('Video %s does not exist' % video_id,
41                                  expected=True)
42
43         fields = dict(re.findall(r'''(?x)<input\s+
44             type="hidden"\s+
45             name="([^"]+)"\s+
46             (?:id="[^"]+"\s+)?
47             value="([^"]*)"
48             ''', webpage))
49
50         post = compat_urllib_parse.urlencode(fields)
51         req = compat_urllib_request.Request(url, post)
52         req.add_header('Content-type', 'application/x-www-form-urlencoded')
53
54         # Apparently, this header is required for confirmation to work.
55         req.add_header('Host', 'www.firedrive.com')
56
57         webpage = self._download_webpage(req, video_id,
58                                          'Downloading video page')
59
60         title = self._search_regex(r'class="external_title_left">(.+)</div>',
61                                    webpage, 'title')
62         thumbnail = self._search_regex(r'image:\s?"(//[^\"]+)', webpage,
63                                        'thumbnail', fatal=False)
64         if thumbnail is not None:
65             thumbnail = 'http:' + thumbnail
66
67         ext = self._search_regex(r'type:\s?\'([^\']+)\',',
68                                  webpage, 'extension', fatal=False)
69         video_url = self._search_regex(
70             r'file:\s?\'(http[^\']+)\',', webpage, 'file url')
71
72         formats = [{
73             'format_id': 'sd',
74             'url': video_url,
75             'ext': ext,
76         }]
77
78         return {
79             'id': video_id,
80             'title': title,
81             'thumbnail': thumbnail,
82             'formats': formats,
83         }