[GorillaVid] improve extractor
[youtube-dl] / youtube_dl / extractor / gorillavid.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     determine_ext,
9     compat_urllib_parse,
10     compat_urllib_request,
11 )
12
13
14 class GorillaVidIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?gorillavid\.in/(?:embed-)?(?P<id>[0-9a-zA-Z]+)(?:-[0-9]+x[0-9]+\.html)?'
16
17     _TESTS = [{
18         'url': 'http://gorillavid.in/06y9juieqpmi',
19         'md5': '5ae4a3580620380619678ee4875893ba',
20         'info_dict': {
21             'id': '06y9juieqpmi',
22             'ext': 'flv',
23             'title': 'Rebecca Black My Moment Official Music Video Reaction',
24             'thumbnail': 're:http://.*\.jpg',
25         },
26     }, {
27         'url': 'http://gorillavid.in/embed-z08zf8le23c6-960x480.html',
28         'md5': 'c9e293ca74d46cad638e199c3f3fe604',
29         'info_dict': {
30             'id': 'z08zf8le23c6',
31             'ext': 'mp4',
32             'title': 'Say something nice',
33             'thumbnail': 're:http://.*\.jpg',
34         },
35     }]
36
37     def _real_extract(self, url):
38         mobj = re.match(self._VALID_URL, url)
39         video_id = mobj.group('id')
40
41         url = 'http://gorillavid.in/%s' % video_id
42
43         webpage = self._download_webpage(url, video_id)
44
45         fields = dict(re.findall(r'''(?x)<input\s+
46             type="hidden"\s+
47             name="([^"]+)"\s+
48             (?:id="[^"]+"\s+)?
49             value="([^"]*)"
50             ''', webpage))
51         
52         if fields['op'] == 'download1':
53             post = compat_urllib_parse.urlencode(fields)
54
55             req = compat_urllib_request.Request(url, post)
56             req.add_header('Content-type', 'application/x-www-form-urlencoded')
57
58             webpage = self._download_webpage(req, video_id, 'Downloading video page')
59
60         title = self._search_regex(r'style="z-index: [0-9]+;">([0-9a-zA-Z ]+)(?:-.+)?</span>', webpage, 'title')
61         thumbnail = self._search_regex(r'image:\'(http[^\']+)\',', webpage, 'thumbnail')
62         url = self._search_regex(r'file: \'(http[^\']+)\',', webpage, 'file url')
63
64         formats = [{
65             'format_id': 'sd',
66             'url': url,
67             'ext': determine_ext(url),
68             'quality': 1,
69         }]
70
71         return {
72             'id': video_id,
73             'title': title,
74             'thumbnail': thumbnail,
75             'formats': formats,
76         }