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