[comcarcoff] Improve json data regex and modernize
[youtube-dl] / youtube_dl / extractor / comcarcoff.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import parse_iso8601
8
9
10 class ComCarCoffIE(InfoExtractor):
11     _VALID_URL = r'http://(?:www\.)?comediansincarsgettingcoffee\.com/(?P<id>[a-z0-9\-]*)'
12     _TESTS = [{
13         'url': 'http://comediansincarsgettingcoffee.com/miranda-sings-happy-thanksgiving-miranda/',
14         'info_dict': {
15             'id': 'miranda-sings-happy-thanksgiving-miranda',
16             'ext': 'mp4',
17             'upload_date': '20141127',
18             'timestamp': 1417107600,
19             'title': 'Happy Thanksgiving Miranda',
20             'description': 'Jerry Seinfeld and his special guest Miranda Sings cruise around town in search of coffee, complaining and apologizing along the way.',
21             'thumbnail': 'http://ccc.crackle.com/images/s5e4_thumb.jpg',
22         },
23         'params': {
24             'skip_download': 'requires ffmpeg',
25         }
26     }]
27
28     def _real_extract(self, url):
29         display_id = self._match_id(url)
30         if not display_id:
31             display_id = 'comediansincarsgettingcoffee.com'
32         webpage = self._download_webpage(url, display_id)
33
34         full_data = self._parse_json(
35             self._search_regex(
36                 r'window\.app\s*=\s*({.+?});\n', webpage, 'full data json'),
37             display_id)['videoData']
38
39         video_id = full_data['activeVideo']['video']
40         video_data = full_data.get('videos', {}).get(video_id) or full_data['singleshots'][video_id]
41         thumbnails = [{
42             'url': video_data['images']['thumb'],
43         }, {
44             'url': video_data['images']['poster'],
45         }]
46         formats = self._extract_m3u8_formats(
47             video_data['mediaUrl'], video_id, ext='mp4')
48
49         return {
50             'id': video_id,
51             'display_id': display_id,
52             'title': video_data['title'],
53             'description': video_data.get('description'),
54             'timestamp': parse_iso8601(video_data.get('pubDate')),
55             'thumbnails': thumbnails,
56             'formats': formats,
57             'webpage_url': 'http://comediansincarsgettingcoffee.com/%s' % (video_data.get('urlSlug', video_data.get('slug'))),
58         }