[screenwavemedia] Simplify (#3766)
[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     month_by_name,
11     unified_strdate,
12 )
13
14
15 class ScreenwaveMediaIE(InfoExtractor):
16     _VALID_URL = r'http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=(?P<id>.+)'
17
18     _TESTS = [{
19         'url': 'http://player.screenwavemedia.com/play/play.php?playerdiv=videoarea&companiondiv=squareAd&id=Cinemassacre-19911',
20         'only_matching': True,
21     }]
22
23     def _real_extract(self, url):
24         video_id = self._match_id(url)
25         playerdata = self._download_webpage(url, video_id, 'Downloading player webpage')
26
27         vidtitle = self._search_regex(
28             r'\'vidtitle\'\s*:\s*"([^"]+)"', playerdata, 'vidtitle').replace('\\/', '/')
29         vidurl = self._search_regex(
30             r'\'vidurl\'\s*:\s*"([^"]+)"', playerdata, 'vidurl').replace('\\/', '/')
31
32         videolist_url = None
33
34         mobj = re.search(r"'videoserver'\s*:\s*'(?P<videoserver>[^']+)'", playerdata)
35         if mobj:
36             videoserver = mobj.group('videoserver')
37             mobj = re.search(r'\'vidid\'\s*:\s*"(?P<vidid>[^\']+)"', playerdata)
38             vidid = mobj.group('vidid') if mobj else video_id
39             videolist_url = 'http://%s/vod/smil:%s.smil/jwplayer.smil' % (videoserver, vidid)
40         else:
41             mobj = re.search(r"file\s*:\s*'(?P<smil>http.+?/jwplayer\.smil)'", playerdata)
42             if mobj:
43                 videolist_url = mobj.group('smil')
44
45         if videolist_url:
46             videolist = self._download_xml(videolist_url, video_id, 'Downloading videolist XML')
47             formats = []
48             baseurl = vidurl[:vidurl.rfind('/') + 1]
49             for video in videolist.findall('.//video'):
50                 src = video.get('src')
51                 if not src:
52                     continue
53                 file_ = src.partition(':')[-1]
54                 width = int_or_none(video.get('width'))
55                 height = int_or_none(video.get('height'))
56                 bitrate = int_or_none(video.get('system-bitrate'), scale=1000)
57                 format = {
58                     'url': baseurl + file_,
59                     'format_id': src.rpartition('.')[0].rpartition('_')[-1],
60                 }
61                 if width or height:
62                     format.update({
63                         'tbr': bitrate,
64                         'width': width,
65                         'height': height,
66                     })
67                 else:
68                     format.update({
69                         'abr': bitrate,
70                         'vcodec': 'none',
71                     })
72                 formats.append(format)
73         else:
74             formats = [{
75                 'url': vidurl,
76             }]
77         self._sort_formats(formats)
78
79         return {
80             'id': video_id,
81             'title': vidtitle,
82             'formats': formats,
83         }
84
85
86 class CinemassacreIE(InfoExtractor):
87     _VALID_URL = 'https?://(?:www\.)?cinemassacre\.com/(?P<date_y>[0-9]{4})/(?P<date_m>[0-9]{2})/(?P<date_d>[0-9]{2})/(?P<display_id>[^?#/]+)'
88     _TESTS = [
89         {
90             'url': 'http://cinemassacre.com/2012/11/10/avgn-the-movie-trailer/',
91             'md5': 'fde81fbafaee331785f58cd6c0d46190',
92             'info_dict': {
93                 'id': 'Cinemassacre-19911',
94                 'ext': 'mp4',
95                 'upload_date': '20121110',
96                 'title': '“Angry Video Game Nerd: The Movie” – Trailer',
97                 'description': 'md5:fb87405fcb42a331742a0dce2708560b',
98             },
99         },
100         {
101             'url': 'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
102             'md5': 'd72f10cd39eac4215048f62ab477a511',
103             'info_dict': {
104                 'id': 'Cinemassacre-521be8ef82b16',
105                 'ext': 'mp4',
106                 'upload_date': '20131002',
107                 'title': 'The Mummy’s Hand (1940)',
108             },
109         }
110     ]
111
112     def _real_extract(self, url):
113         mobj = re.match(self._VALID_URL, url)
114         display_id = mobj.group('display_id')
115         video_date = mobj.group('date_y') + mobj.group('date_m') + mobj.group('date_d')
116
117         webpage = self._download_webpage(url, display_id)
118
119         playerdata_url = self._search_regex(
120             r'src="(http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=.+?)"',
121             webpage, 'player data URL')
122         video_title = self._html_search_regex(
123             r'<title>(?P<title>.+?)\|', webpage, 'title')
124         video_description = self._html_search_regex(
125             r'<div class="entry-content">(?P<description>.+?)</div>',
126             webpage, 'description', flags=re.DOTALL, fatal=False)
127         video_thumbnail = self._og_search_thumbnail(webpage)
128
129         return {
130             '_type': 'url_transparent',
131             'display_id': display_id,
132             'title': video_title,
133             'description': video_description,
134             'upload_date': video_date,
135             'thumbnail': video_thumbnail,
136             'url': playerdata_url,
137         }
138
139
140 class TeamFourIE(InfoExtractor):
141     _VALID_URL = r'https?://(?:www\.)?teamfourstar\.com/video/(?P<id>[a-z0-9\-]+)/?'
142     _TEST = {
143         'url': 'http://teamfourstar.com/video/a-moment-with-tfs-episode-4/',
144         'info_dict': {
145             'id': 'TeamFourStar-5292a02f20bfa',
146             'ext': 'mp4',
147             'upload_date': '20130401',
148             'description': 'Check out this and more on our website: http://teamfourstar.com\nTFS Store: http://sharkrobot.com/team-four-star\nFollow on Twitter: http://twitter.com/teamfourstar\nLike on FB: http://facebook.com/teamfourstar',
149             'title': 'A Moment With TFS Episode 4',
150         }
151     }
152
153     def _real_extract(self, url):
154         display_id = self._match_id(url)
155         webpage = self._download_webpage(url, display_id)
156
157         playerdata_url = self._search_regex(
158             r'src="(http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=.+?)"',
159             webpage, 'player data URL')
160
161         video_title = self._html_search_regex(
162             r'<div class="heroheadingtitle">(?P<title>.+?)</div>',
163             webpage, 'title')
164         video_date = unified_strdate(self._html_search_regex(
165             r'<div class="heroheadingdate">(?P<date>.+?)</div>',
166             webpage, 'date', fatal=False))
167         video_description = self._html_search_regex(
168             r'(?s)<div class="postcontent">(?P<description>.+?)</div>',
169             webpage, 'description', fatal=False)
170         video_thumbnail = self._og_search_thumbnail(webpage)
171
172         return {
173             '_type': 'url_transparent',
174             'display_id': display_id,
175             'title': video_title,
176             'description': video_description,
177             'upload_date': video_date,
178             'thumbnail': video_thumbnail,
179             'url': playerdata_url,
180         }