Merge branch 'qqmusic-album-fix' of https://github.com/ping/youtube-dl into ping...
[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     @staticmethod
31     def _extract_url(webpage):
32         mobj = re.search(
33             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?onionstudios\.com/embed.+?)\1', webpage)
34         if mobj:
35             return mobj.group('url')
36
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39
40         webpage = self._download_webpage(
41             'http://www.onionstudios.com/embed?id=%s' % video_id, video_id)
42
43         formats = []
44         for src in re.findall(r'<source[^>]+src="([^"]+)"', webpage):
45             if determine_ext(src) != 'm3u8':  # m3u8 always results in 403
46                 formats.append({
47                     'url': src,
48                 })
49         self._sort_formats(formats)
50
51         title = self._search_regex(
52             r'share_title\s*=\s*"([^"]+)"', webpage, 'title')
53         description = self._search_regex(
54             r'share_description\s*=\s*"([^"]+)"', webpage,
55             'description', default=None)
56         thumbnail = self._search_regex(
57             r'poster="([^"]+)"', webpage, 'thumbnail', default=False)
58
59         uploader_id = self._search_regex(
60             r'twitter_handle\s*=\s*"([^"]+)"',
61             webpage, 'uploader id', fatal=False)
62         uploader = self._search_regex(
63             r'window\.channelName\s*=\s*"Embedded:([^"]+)"',
64             webpage, 'uploader', default=False)
65
66         return {
67             'id': video_id,
68             'title': title,
69             'description': description,
70             'thumbnail': thumbnail,
71             'uploader': uploader,
72             'uploader_id': uploader_id,
73             'formats': formats,
74         }