]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/funnyordie.py
Prepare to release.
[youtubedl] / 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': r'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': r're:^http:.*\.jpg$',
30 },
31 'params': {
32 'skip_download': True,
33 },
34 }, {
35 'url': 'http://www.funnyordie.com/articles/ebf5e34fc8/10-hours-of-walking-in-nyc-as-a-man',
36 'only_matching': True,
37 }]
38
39 def _real_extract(self, url):
40 mobj = re.match(self._VALID_URL, url)
41
42 video_id = mobj.group('id')
43 webpage = self._download_webpage(url, video_id)
44
45 links = re.findall(r'<source src="([^"]+/v)[^"]+\.([^"]+)" type=\'video', webpage)
46 if not links:
47 raise ExtractorError('No media links available for %s' % video_id)
48
49 links.sort(key=lambda link: 1 if link[1] == 'mp4' else 0)
50
51 m3u8_url = self._search_regex(
52 r'<source[^>]+src=(["\'])(?P<url>.+?/master\.m3u8[^"\']*)\1',
53 webpage, 'm3u8 url', group='url')
54
55 formats = []
56
57 m3u8_formats = self._extract_m3u8_formats(
58 m3u8_url, video_id, 'mp4', 'm3u8_native',
59 m3u8_id='hls', fatal=False)
60 source_formats = list(filter(
61 lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
62 m3u8_formats))
63
64 bitrates = [int(bitrate) for bitrate in re.findall(r'[,/]v(\d+)(?=[,/])', m3u8_url)]
65 bitrates.sort()
66
67 if source_formats:
68 self._sort_formats(source_formats)
69
70 for bitrate, f in zip(bitrates, source_formats or [{}] * len(bitrates)):
71 for path, ext in links:
72 ff = f.copy()
73 if ff:
74 if ext != 'mp4':
75 ff = dict(
76 [(k, v) for k, v in ff.items()
77 if k in ('height', 'width', 'format_id')])
78 ff.update({
79 'format_id': ff['format_id'].replace('hls', ext),
80 'ext': ext,
81 'protocol': 'http',
82 })
83 else:
84 ff.update({
85 'format_id': '%s-%d' % (ext, bitrate),
86 'vbr': bitrate,
87 })
88 ff['url'] = self._proto_relative_url(
89 '%s%d.%s' % (path, bitrate, ext))
90 formats.append(ff)
91 self._check_formats(formats, video_id)
92
93 formats.extend(m3u8_formats)
94 self._sort_formats(
95 formats, field_preference=('height', 'width', 'tbr', 'format_id'))
96
97 subtitles = {}
98 for src, src_lang in re.findall(r'<track kind="captions" src="([^"]+)" srclang="([^"]+)"', webpage):
99 subtitles[src_lang] = [{
100 'ext': src.split('/')[-1],
101 'url': 'http://www.funnyordie.com%s' % src,
102 }]
103
104 post_json = self._search_regex(
105 r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details')
106 post = json.loads(post_json)
107
108 return {
109 'id': video_id,
110 'title': post['name'],
111 'description': post.get('description'),
112 'thumbnail': post.get('picture'),
113 'formats': formats,
114 'subtitles': subtitles,
115 }