[onionstudios] Add extractor
[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 determine_ext
8
9
10 class OnionStudiosIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?onionstudios\.com/(?:videos/[^/]+-|embed\?.*\bid=)(?P<id>\d+)(?!-)'
12
13     _TESTS = [{
14         'url': 'http://www.onionstudios.com/videos/hannibal-charges-forward-stops-for-a-cocktail-2937',
15         'md5': 'd4851405d31adfadf71cd7a487b765bb',
16         'info_dict': {
17             'id': '2937',
18             'ext': 'mp4',
19             'title': 'Hannibal charges forward, stops for a cocktail',
20             'description': 'md5:545299bda6abf87e5ec666548c6a9448',
21             'thumbnail': 're:^https?://.*\.jpg$',
22             'uploader': 'The A.V. Club',
23             'uploader_id': 'TheAVClub',
24         },
25     }, {
26         'url': 'http://www.onionstudios.com/embed?id=2855&autoplay=true',
27         'only_matching': True,
28     }]
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32
33         webpage = self._download_webpage(
34             'http://www.onionstudios.com/embed?id=%s' % video_id, video_id)
35
36         formats = []
37         for src in re.findall(r'<source[^>]+src="([^"]+)"', webpage):
38             if determine_ext(src) != 'm3u8':  # m3u8 always results in 403
39                 formats.append({
40                     'url': src,
41                 })
42         self._sort_formats(formats)
43
44         title = self._search_regex(
45             r'share_title\s*=\s*"([^"]+)"', webpage, 'title')
46         description = self._search_regex(
47             r'share_description\s*=\s*"([^"]+)"', webpage,
48             'description', default=None)
49         thumbnail = self._search_regex(
50             r'poster="([^"]+)"', webpage, 'thumbnail', default=False)
51
52         uploader_id = self._search_regex(
53             r'twitter_handle\s*=\s*"([^"]+)"',
54             webpage, 'uploader id', fatal=False)
55         uploader = self._search_regex(
56             r'window\.channelName\s*=\s*"Embedded:([^"]+)"',
57             webpage, 'uploader', default=False)
58
59         return {
60             'id': video_id,
61             'title': title,
62             'description': description,
63             'thumbnail': thumbnail,
64             'uploader': uploader,
65             'uploader_id': uploader_id,
66             'formats': formats,
67         }