aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/firedrive.py
blob: 1d83048e81d66c59f1a2cb375aa3f97bd809b753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    compat_urllib_parse,
    compat_urllib_request,
    determine_ext,
)


class FiredriveIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?firedrive\.com/' + \
                 '(?:file|embed)/(?P<id>[0-9a-zA-Z]+)'
    _FILE_DELETED_REGEX = r'<div class="removed_file_image">'

    _TESTS = [{
        'url': 'https://www.firedrive.com/file/FEB892FA160EBD01',
        'md5': 'd5d4252f80ebeab4dc2d5ceaed1b7970',
        'info_dict': {
            'id': 'FEB892FA160EBD01',
            'ext': 'flv',
            'title': 'bbb_theora_486kbit.flv',
            'thumbnail': 're:http://.*\.jpg',
        },
    }]

    def _real_extract(self, url):
        mobj = re.match(self._VALID_URL, url)
        video_id = mobj.group('id')

        url = 'http://firedrive.com/file/%s' % video_id

        webpage = self._download_webpage(url, video_id)

        if re.search(self._FILE_DELETED_REGEX, webpage) is not None:
            raise ExtractorError(u'Video %s does not exist' % video_id,
                                 expected=True)

        fields = dict(re.findall(r'''(?x)<input\s+
            type="hidden"\s+
            name="([^"]+)"\s+
            (?:id="[^"]+"\s+)?
            value="([^"]*)"
            ''', webpage))

        post = compat_urllib_parse.urlencode(fields)
        req = compat_urllib_request.Request(url, post)
        req.add_header('Content-type', 'application/x-www-form-urlencoded')

        # Apparently, this header is required for confirmation to work.
        req.add_header('Host', 'www.firedrive.com')

        webpage = self._download_webpage(req, video_id,
                                         'Downloading video page')

        title = self._search_regex(r'class="external_title_left">(.+)</div>',
                                   webpage, 'title')
        thumbnail = self._search_regex(r'image:\s?"(//[^\"]+)', webpage,
                                       'thumbnail', fatal=False, default="")
        url = self._search_regex(r'file:\s?\'(http[^\']+)\',',
                                 webpage, 'file url')
        ext = self._search_regex(r'type:\s?\'([^\']+)\',',
                                 webpage, 'extension', fatal=False)

        formats = [{
            'format_id': 'sd',
            'url': url,
            'ext': ext or determine_ext(url),
            'quality': 1,
        }]

        return {
            'id': video_id,
            'title': title,
            'thumbnail': "http:" + thumbnail,
            'formats': formats,
        }