[gorillavid] Fix title extraction and make thumbnail optional (Closes #3884)
[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     ExtractorError,
9     determine_ext,
10     compat_urllib_parse,
11     compat_urllib_request,
12 )
13
14
15 class GorillaVidIE(InfoExtractor):
16     IE_DESC = 'GorillaVid.in, daclips.in and movpod.in'
17     _VALID_URL = r'''(?x)
18         https?://(?P<host>(?:www\.)?
19             (?:daclips\.in|gorillavid\.in|movpod\.in))/
20         (?:embed-)?(?P<id>[0-9a-zA-Z]+)(?:-[0-9]+x[0-9]+\.html)?
21     '''
22
23     _TESTS = [{
24         'url': 'http://gorillavid.in/06y9juieqpmi',
25         'md5': '5ae4a3580620380619678ee4875893ba',
26         'info_dict': {
27             'id': '06y9juieqpmi',
28             'ext': 'flv',
29             'title': 'Rebecca Black My Moment Official Music Video Reaction-6GK87Rc8bzQ',
30             'thumbnail': 're:http://.*\.jpg',
31         },
32     }, {
33         'url': 'http://gorillavid.in/embed-z08zf8le23c6-960x480.html',
34         'md5': 'c9e293ca74d46cad638e199c3f3fe604',
35         'info_dict': {
36             'id': 'z08zf8le23c6',
37             'ext': 'mp4',
38             'title': 'Say something nice',
39             'thumbnail': 're:http://.*\.jpg',
40         },
41     }, {
42         'url': 'http://daclips.in/3rso4kdn6f9m',
43         'md5': '1ad8fd39bb976eeb66004d3a4895f106',
44         'info_dict': {
45             'id': '3rso4kdn6f9m',
46             'ext': 'mp4',
47             'title': 'Micro Pig piglets ready on 16th July 2009',
48             'thumbnail': 're:http://.*\.jpg',
49         },
50     }, {
51         'url': 'http://movpod.in/0wguyyxi1yca',
52         'only_matching': True,
53     }]
54
55     def _real_extract(self, url):
56         mobj = re.match(self._VALID_URL, url)
57         video_id = mobj.group('id')
58
59         webpage = self._download_webpage('http://%s/%s' % (mobj.group('host'), video_id), video_id)
60
61         fields = dict(re.findall(r'''(?x)<input\s+
62             type="hidden"\s+
63             name="([^"]+)"\s+
64             (?:id="[^"]+"\s+)?
65             value="([^"]*)"
66             ''', webpage))
67         
68         if fields['op'] == 'download1':
69             post = compat_urllib_parse.urlencode(fields)
70
71             req = compat_urllib_request.Request(url, post)
72             req.add_header('Content-type', 'application/x-www-form-urlencoded')
73
74             webpage = self._download_webpage(req, video_id, 'Downloading video page')
75
76         title = self._search_regex(r'style="z-index: [0-9]+;">([^<]+)</span>', webpage, 'title')
77         video_url = self._search_regex(r'file\s*:\s*\'(http[^\']+)\',', webpage, 'file url')
78         thumbnail = self._search_regex(r'image\s*:\s*\'(http[^\']+)\',', webpage, 'thumbnail', fatal=False)
79
80         formats = [{
81             'format_id': 'sd',
82             'url': video_url,
83             'ext': determine_ext(video_url),
84             'quality': 1,
85         }]
86
87         return {
88             'id': video_id,
89             'title': title,
90             'thumbnail': thumbnail,
91             'formats': formats,
92         }