Added subtitles for FunnyOrDie
[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|articles|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         'info_dict': {
25             'id': 'e402820827',
26             'ext': 'mp4',
27             'title': 'Please Use This Song (Jon Lajoie)',
28             'description': 'Please use this to sell something.  www.jonlajoie.com',
29             'thumbnail': 're:^http:.*\.jpg$',
30         },
31     }, {
32         'url': 'http://www.funnyordie.com/articles/ebf5e34fc8/10-hours-of-walking-in-nyc-as-a-man',
33         'only_matching': True,
34     }]
35
36     def _real_extract(self, url):
37         mobj = re.match(self._VALID_URL, url)
38
39         video_id = mobj.group('id')
40         webpage = self._download_webpage(url, video_id)
41
42         links = re.findall(r'<source src="([^"]+/v)[^"]+\.([^"]+)" type=\'video', webpage)
43         if not links:
44             raise ExtractorError('No media links available for %s' % video_id)
45
46         links.sort(key=lambda link: 1 if link[1] == 'mp4' else 0)
47
48         bitrates = self._html_search_regex(r'<source src="[^"]+/v,((?:\d+,)+)\.mp4\.csmil', webpage, 'video bitrates')
49         bitrates = [int(b) for b in bitrates.rstrip(',').split(',')]
50         bitrates.sort()
51
52         formats = []
53         for bitrate in bitrates:
54             for link in links:
55                 formats.append({
56                     'url': '%s%d.%s' % (link[0], bitrate, link[1]),
57                     'format_id': '%s-%d' % (link[1], bitrate),
58                     'vbr': bitrate,
59                 })
60
61         subtitles={}
62         subtitle_matches=re.findall(r'<track kind="captions" src="([^"]+)" srclang="([^"]+)"', webpage)
63         for match in subtitle_matches:
64           (suburl,sublang)=match
65           if not sublang in subtitles.keys():
66             subtitles[sublang]=[]
67           subext=suburl.split('/')[-1]
68           print subext
69           subtitles[sublang].append({'url': suburl,'ext': subext})
70
71         post_json = self._search_regex(
72             r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details')
73         post = json.loads(post_json)
74
75         return {
76             'id': video_id,
77             'title': post['name'],
78             'description': post.get('description'),
79             'thumbnail': post.get('picture'),
80             'formats': formats,
81             'subtitles': subtitles,
82         }