[funnyordie] Correct JSON interpretation
[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
8
9 class FunnyOrDieIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?funnyordie\.com/(?P<type>embed|videos)/(?P<id>[0-9a-f]+)(?:$|[?#/])'
11     _TEST = {
12         'url': 'http://www.funnyordie.com/videos/0732f586d7/heart-shaped-box-literal-video-version',
13         'file': '0732f586d7.mp4',
14         'md5': 'f647e9e90064b53b6e046e75d0241fbd',
15         'info_dict': {
16             'description': ('Lyrics changed to match the video. Spoken cameo '
17                 'by Obscurus Lupa (from ThatGuyWithTheGlasses.com). Based on a '
18                 'concept by Dustin McLean (DustFilms.com). Performed, edited, '
19                 'and written by David A. Scott.'),
20             'title': 'Heart-Shaped Box: Literal Video Version',
21         },
22     }
23
24     def _real_extract(self, url):
25         mobj = re.match(self._VALID_URL, url)
26
27         video_id = mobj.group('id')
28         webpage = self._download_webpage(url, video_id)
29
30         video_url = self._search_regex(
31             [r'type="video/mp4" src="(.*?)"', r'src="([^>]*?)" type=\'video/mp4\''],
32             webpage, 'video URL', flags=re.DOTALL)
33
34         if mobj.group('type') == 'embed':
35             post_json = self._search_regex(
36                 r'fb_post\s*=\s*(\{.*?\});', webpage, 'post details')
37             post = json.loads(post_json)
38             title = post['name']
39             description = post.get('description')
40             thumbnail = post.get('picture')
41         else:
42             title = self._og_search_title(webpage)
43             description = self._og_search_description(webpage)
44             thumbnail = None
45
46         return {
47             'id': video_id,
48             'url': video_url,
49             'ext': 'mp4',
50             'title': title,
51             'description': description,
52             'thumbnail': thumbnail,
53         }