add direct screenwavemedia.com URL support
[youtube-dl] / youtube_dl / extractor / screenwavemedia.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10 )
11
12 class ScreenwaveMediaIE(InfoExtractor):
13     _VALID_URL = r'(?:http://)?(?' \
14         r':(?P<generic>player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=(?P<video_id>.+))' \
15         r'|(?P<cinemassacre>(?:www\.)?cinemassacre\.com/(?P<cm_date_Y>[0-9]{4})/(?P<cm_date_m>[0-9]{2})/(?P<cm_date_d>[0-9]{2})/(?P<cm_display_id>[^?#/]+))' \
16         r')'
17
18     _TESTS = [
19         {
20             'url': 'http://cinemassacre.com/2012/11/10/avgn-the-movie-trailer/',
21             'md5': 'fde81fbafaee331785f58cd6c0d46190',
22             'info_dict': {
23                 'id': 'Cinemasssacre-19911',
24                 'ext': 'mp4',
25                 'upload_date': '20121110',
26                 'title': '“Angry Video Game Nerd: The Movie” – Trailer',
27                 'description': 'md5:fb87405fcb42a331742a0dce2708560b',
28             },
29         },
30         {
31             'url': 'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
32             'md5': 'd72f10cd39eac4215048f62ab477a511',
33             'info_dict': {
34                 'id': 'Cinemasssacre-521be8ef82b16',
35                 'ext': 'mp4',
36                 'upload_date': '20131002',
37                 'title': 'The Mummy’s Hand (1940)',
38             },
39         }
40     ]
41
42     def _cinemassacre_get_info(self, url):
43         mobj = re.match(self._VALID_URL, url)
44         display_id = mobj.group('cm_display_id')
45
46         webpage = self._download_webpage(url, display_id)
47         video_date = mobj.group('cm_date_Y') + mobj.group('cm_date_m') + mobj.group('cm_date_d')
48         mobj = re.search(r'src="(?P<embed_url>http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=.+?)"', webpage)
49         if not mobj:
50             raise ExtractorError('Can\'t extract embed url and video id')
51         playerdata_url = mobj.group('embed_url')
52
53         video_title = self._html_search_regex(
54             r'<title>(?P<title>.+?)\|', webpage, 'title')
55         video_description = self._html_search_regex(
56             r'<div class="entry-content">(?P<description>.+?)</div>',
57             webpage, 'description', flags=re.DOTALL, fatal=False)
58         video_thumbnail = self._og_search_thumbnail(webpage)
59
60         return {
61             'title': video_title,
62             'description': video_description,
63             'upload_date': video_date,
64             'thumbnail': video_thumbnail,
65             '_embed_url': playerdata_url,
66         }
67
68     def _screenwavemedia_get_info(self, url):
69         mobj = re.match(self._VALID_URL, url)
70         if not mobj:
71             raise ExtractorError('Can\'t extract embed url and video id')
72         video_id = mobj.group('video_id')
73
74         playerdata = self._download_webpage(url, video_id, 'Downloading player webpage')
75
76         vidtitle = self._search_regex(
77             r'\'vidtitle\'\s*:\s*"([^\']+)"', playerdata, 'vidtitle').replace('\\/', '/')
78         vidurl = self._search_regex(
79             r'\'vidurl\'\s*:\s*"([^\']+)"', playerdata, 'vidurl').replace('\\/', '/')
80         pageurl = self._search_regex(
81             r'\'pageurl\'\s*:\s*"([^\']+)"', playerdata, 'pageurl', fatal=False).replace('\\/', '/')
82
83         videolist_url = None
84
85         mobj = re.search(r"'videoserver'\s*:\s*'(?P<videoserver>[^']+)'", playerdata)
86         if mobj:
87             videoserver = mobj.group('videoserver')
88             mobj = re.search(r'\'vidid\'\s*:\s*"(?P<vidid>[^\']+)"', playerdata)
89             vidid = mobj.group('vidid') if mobj else video_id
90             videolist_url = 'http://%s/vod/smil:%s.smil/jwplayer.smil' % (videoserver, vidid)
91         else:
92             mobj = re.search(r"file\s*:\s*'(?P<smil>http.+?/jwplayer\.smil)'", playerdata)
93             if mobj:
94                 videolist_url = mobj.group('smil')
95
96         if videolist_url:
97             videolist = self._download_xml(videolist_url, video_id, 'Downloading videolist XML')
98             formats = []
99             baseurl = vidurl[:vidurl.rfind('/') + 1]
100             for video in videolist.findall('.//video'):
101                 src = video.get('src')
102                 if not src:
103                     continue
104                 file_ = src.partition(':')[-1]
105                 width = int_or_none(video.get('width'))
106                 height = int_or_none(video.get('height'))
107                 bitrate = int_or_none(video.get('system-bitrate'))
108                 format = {
109                     'url': baseurl + file_,
110                     'format_id': src.rpartition('.')[0].rpartition('_')[-1],
111                 }
112                 if width or height:
113                     format.update({
114                         'tbr': bitrate // 1000 if bitrate else None,
115                         'width': width,
116                         'height': height,
117                     })
118                 else:
119                     format.update({
120                         'abr': bitrate // 1000 if bitrate else None,
121                         'vcodec': 'none',
122                     })
123                 formats.append(format)
124             self._sort_formats(formats)
125         else:
126             formats = [{
127                 'url': vidurl,
128             }]
129
130         return {
131             'id': video_id,
132             'title': vidtitle,
133             'formats': formats,
134             '_episode_page': pageurl,
135         }
136
137     def _real_extract(self, url):
138         mobj = re.match(self._VALID_URL, url)
139
140         swm_info = None
141         site_info = None
142
143         if mobj.group('generic'):
144             swm_info = self._screenwavemedia_get_info(url)
145             url = swm_info['_episode_page']
146             mobj = re.match(self._VALID_URL, url)
147
148         if mobj:
149             if mobj.group('cinemassacre'):
150                 site_info = self._cinemassacre_get_info(url)
151
152         if not swm_info:
153             if site_info:
154                 swm_info = self._screenwavemedia_get_info(site_info['_embed_url'])
155
156         if not swm_info:
157             raise ExtractorError("Failed to extract metadata for this URL")
158
159         if site_info:
160             swm_info.update(site_info)
161
162         return swm_info