[onionstudios] fix extraction
[youtube-dl] / youtube_dl / extractor / onionstudios.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_str,
9     int_or_none,
10     js_to_json,
11     parse_iso8601,
12     try_get,
13 )
14
15
16 class OnionStudiosIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:www\.)?onionstudios\.com/(?:video(?:s/[^/]+-|/)|embed\?.*\bid=)(?P<id>\d+)(?!-)'
18
19     _TESTS = [{
20         'url': 'http://www.onionstudios.com/videos/hannibal-charges-forward-stops-for-a-cocktail-2937',
21         'md5': '5a118d466d62b5cd03647cf2c593977f',
22         'info_dict': {
23             'id': '2937',
24             'ext': 'mp4',
25             'title': 'Hannibal charges forward, stops for a cocktail',
26             'description': 'md5:545299bda6abf87e5ec666548c6a9448',
27             'thumbnail': r're:^https?://.*\.jpg$',
28             'uploader': 'a.v. club',
29             'upload_date': '20150619',
30             'timestamp': 1434728546,
31         },
32     }, {
33         'url': 'http://www.onionstudios.com/embed?id=2855&autoplay=true',
34         'only_matching': True,
35     }, {
36         'url': 'http://www.onionstudios.com/video/6139.json',
37         'only_matching': True,
38     }]
39
40     @staticmethod
41     def _extract_url(webpage):
42         mobj = re.search(
43             r'(?s)<(?:iframe|bulbs-video)[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?onionstudios\.com/(?:embed.+?|video/\d+\.json))\1', webpage)
44         if mobj:
45             return mobj.group('url')
46
47     def _real_extract(self, url):
48         video_id = self._match_id(url)
49
50         webpage = self._download_webpage(
51             'http://onionstudios.com/embed/dc94dc2899fe644c0e7241fa04c1b732.js',
52             video_id)
53         mcp_id = compat_str(self._parse_json(self._search_regex(
54             r'window\.mcpMapping\s*=\s*({.+?});', webpage,
55             'MCP Mapping'), video_id, js_to_json)[video_id]['mcp_id'])
56         video_data = self._download_json(
57             'https://api.vmh.univision.com/metadata/v1/content/' + mcp_id,
58             mcp_id)['videoMetadata']
59         iptc = video_data['photoVideoMetadataIPTC']
60         title = iptc['title']['en']
61         fmg = video_data.get('photoVideoMetadata_fmg') or {}
62         tvss_domain = fmg.get('tvssDomain') or 'https://auth.univision.com'
63         data = self._download_json(
64             tvss_domain + '/api/v3/video-auth/url-signature-tokens',
65             mcp_id, query={'mcpids': mcp_id})['data'][0]
66         formats = []
67
68         rendition_url = data.get('renditionUrl')
69         if rendition_url:
70             formats = self._extract_m3u8_formats(
71                 rendition_url, mcp_id, 'mp4',
72                 'm3u8_native', m3u8_id='hls', fatal=False)
73
74         fallback_rendition_url = data.get('fallbackRenditionUrl')
75         if fallback_rendition_url:
76             formats.append({
77                 'format_id': 'fallback',
78                 'tbr': int_or_none(self._search_regex(
79                     r'_(\d+)\.mp4', fallback_rendition_url,
80                     'bitrate', default=None)),
81                 'url': fallback_rendition_url,
82             })
83
84         self._sort_formats(formats)
85
86         return {
87             'id': video_id,
88             'title': title,
89             'thumbnail': try_get(iptc, lambda x: x['cloudinaryLink']['link'], compat_str),
90             'uploader': fmg.get('network'),
91             'duration': int_or_none(iptc.get('fileDuration')),
92             'formats': formats,
93             'description': try_get(iptc, lambda x: x['description']['en'], compat_str),
94             'timestamp': parse_iso8601(iptc.get('dateReleased')),
95         }