[vgtv] Add new extractor
[youtube-dl] / youtube_dl / extractor / funnyordie.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import ExtractorError
8
9
10 class FunnyOrDieIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?funnyordie\.com/(?P<type>embed|videos)/(?P<id>[0-9a-f]+)(?:$|[?#/])'
12     _TESTS = [{
13         'url': 'http://www.funnyordie.com/videos/0732f586d7/heart-shaped-box-literal-video-version',
14         'md5': 'bcd81e0c4f26189ee09be362ad6e6ba9',
15         'info_dict': {
16             'id': '0732f586d7',
17             'ext': 'mp4',
18             'title': 'Heart-Shaped Box: Literal Video Version',
19             'description': 'md5:ea09a01bc9a1c46d9ab696c01747c338',
20             'thumbnail': 're:^http:.*\.jpg$',
21         },
22     }, {
23         'url': 'http://www.funnyordie.com/embed/e402820827',
24         'md5': 'ff4d83318f89776ed0250634cfaa8d36',
25         'info_dict': {
26             'id': 'e402820827',
27             'ext': 'mp4',
28             'title': 'Please Use This Song (Jon Lajoie)',
29             'description': 'md5:2ed27d364f5a805a6dba199faaf6681d',
30             'thumbnail': 're:^http:.*\.jpg$',
31         },
32     }]
33
34     def _real_extract(self, url):
35         mobj = re.match(self._VALID_URL, url)
36
37         video_id = mobj.group('id')
38         webpage = self._download_webpage(url, video_id)
39
40         links = re.findall(r'<source src="([^"]+/v)\d+\.([^"]+)" type=\'video', webpage)
41         if not links:
42             raise ExtractorError('No media links available for %s' % video_id)
43
44         links.sort(key=lambda link: 1 if link[1] == 'mp4' else 0)
45
46         bitrates = self._html_search_regex(r'<source src="[^"]+/v,((?:\d+,)+)\.mp4\.csmil', webpage, 'video bitrates')
47         bitrates = [int(b) for b in bitrates.rstrip(',').split(',')]
48         bitrates.sort()
49
50         formats = []
51
52         for bitrate in bitrates:
53             for link in links:
54                 formats.append({
55                     'url': '%s%d.%s' % (link[0], bitrate, link[1]),
56                     'format_id': '%s-%d' % (link[1], bitrate),
57                     'vbr': bitrate,
58                 })
59
60         post_json = self._search_regex(
61             r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details')
62         post = json.loads(post_json)
63
64         return {
65             'id': video_id,
66             'title': post['name'],
67             'description': post.get('description'),
68             'thumbnail': post.get('picture'),
69             'formats': formats,
70         }