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