[onionstudios] fix info 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     determine_ext,
9     int_or_none,
10     float_or_none,
11 )
12
13
14 class OnionStudiosIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?onionstudios\.com/(?:videos/[^/]+-|embed\?.*\bid=)(?P<id>\d+)(?!-)'
16
17     _TESTS = [{
18         'url': 'http://www.onionstudios.com/videos/hannibal-charges-forward-stops-for-a-cocktail-2937',
19         'md5': 'e49f947c105b8a78a675a0ee1bddedfe',
20         'info_dict': {
21             'id': '2937',
22             'ext': 'mp4',
23             'title': 'Hannibal charges forward, stops for a cocktail',
24             'thumbnail': 're:^https?://.*\.jpg$',
25             'uploader': 'The A.V. Club',
26             'uploader_id': 'the-av-club',
27         },
28     }, {
29         'url': 'http://www.onionstudios.com/embed?id=2855&autoplay=true',
30         'only_matching': True,
31     }]
32
33     @staticmethod
34     def _extract_url(webpage):
35         mobj = re.search(
36             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?onionstudios\.com/embed.+?)\1', webpage)
37         if mobj:
38             return mobj.group('url')
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42
43         video_data = self._download_json(
44             'http://www.onionstudios.com/video/%s.json' % video_id, video_id)
45
46         title = video_data['title']
47
48         formats = []
49         for source in video_data.get('sources', []):
50             source_url = source.get('url')
51             if not source_url:
52                 continue
53             content_type = source.get('content_type')
54             ext = determine_ext(source_url)
55             if content_type == 'application/x-mpegURL' or ext == 'm3u8':
56                 formats.extend(self._extract_m3u8_formats(
57                     source_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
58             else:
59                 tbr = int_or_none(source.get('bitrate'))
60                 formats.append({
61                     'format_id': ext + ('-%d' % tbr if tbr else ''),
62                     'url': source_url,
63                     'width': int_or_none(source.get('width')),
64                     'tbr': tbr,
65                     'ext': ext,
66                 })
67         self._sort_formats(formats)
68
69         return {
70             'id': video_id,
71             'title': title,
72             'thumbnail': video_data.get('poster_url'),
73             'uploader': video_data.get('channel_name'),
74             'uploader_id': video_data.get('channel_slug'),
75             'duration': float_or_none(video_data.get('duration', 1000)),
76             'tags': video_data.get('tags'),
77             'formats': formats,
78         }