[screenwavemedia] use the true JS value instead of a comment
[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     int_or_none,
9     unified_strdate,
10     js_to_json,
11 )
12
13
14 class ScreenwaveMediaIE(InfoExtractor):
15     _VALID_URL = r'http://player\d?\.screenwavemedia\.com/(?:play/)?[a-zA-Z]+\.php\?[^"]*\bid=(?P<id>.+)'
16
17     _TESTS = [{
18         'url': 'http://player.screenwavemedia.com/play/play.php?playerdiv=videoarea&companiondiv=squareAd&id=Cinemassacre-19911',
19         'only_matching': True,
20     }]
21
22     def _real_extract(self, url):
23         video_id = self._match_id(url)
24
25         playerdata = self._download_webpage(
26             'http://player.screenwavemedia.com/player.php?id=%s' % video_id,
27             video_id, 'Downloading player webpage')
28
29         vidtitle = self._search_regex(
30             r'\'vidtitle\'\s*:\s*"([^"]+)"', playerdata, 'vidtitle').replace('\\/', '/')
31
32         playerconfig = self._download_webpage(
33             'http://player.screenwavemedia.com/player.js',
34             video_id, 'Downloading playerconfig webpage')
35
36         videoserver = self._search_regex(r'SWMServer\s*=\s*"([\d\.]+)"', playerdata, 'videoserver')
37
38         sources = self._parse_json(
39             js_to_json(
40                 re.sub(
41                     r'(?s)/\*.*?\*/', '',
42                     self._search_regex(
43                         r"sources\s*:\s*(\[[^\]]+?\])", playerconfig,
44                         'sources',
45                     ).replace(
46                         "' + thisObj.options.videoserver + '",
47                         videoserver
48                     ).replace(
49                         "' + playerVidId + '",
50                         video_id
51                     )
52                 )
53             ),
54             video_id, fatal=False
55         )
56
57         # Fallback to hardcoded sources if JS changes again
58         if not sources:
59             sources = [{
60                 'file': 'http://%s/vod/%s_%s.mp4' % (videoserver, video_id, format_id),
61                 'type': 'mp4',
62                 'label': format_label,
63             } for format_id, format_label in (
64                 ('low', '144p Low'), ('med', '160p Med'), ('high', '360p High'), ('hd1', '720p HD1'))]
65             sources.append({
66                 'file': 'http://%s/vod/smil:%s.smil/playlist.m3u8' % (videoserver, video_id),
67                 'type': 'hls',
68             })
69
70         formats = []
71         for source in sources:
72             if source['type'] == 'hls':
73                 formats.extend(self._extract_m3u8_formats(source['file'], video_id))
74             else:
75                 file_ = source.get('file')
76                 if not file_:
77                     continue
78                 format_label = source.get('label')
79                 format_id = self._search_regex(
80                     r'_(.+?)\.[^.]+$', file_, 'format id', default=None)
81                 height = int_or_none(self._search_regex(
82                     r'^(\d+)[pP]', format_label, 'height', default=None))
83                 formats.append({
84                     'url': source['file'],
85                     'format_id': format_id,
86                     'format': format_label,
87                     'ext': source.get('type'),
88                     'height': height,
89                 })
90         self._sort_formats(formats)
91
92         return {
93             'id': video_id,
94             'title': vidtitle,
95             'formats': formats,
96         }
97
98
99 class TeamFourIE(InfoExtractor):
100     _VALID_URL = r'https?://(?:www\.)?teamfourstar\.com/video/(?P<id>[a-z0-9\-]+)/?'
101     _TEST = {
102         'url': 'http://teamfourstar.com/video/a-moment-with-tfs-episode-4/',
103         'info_dict': {
104             'id': 'TeamFourStar-5292a02f20bfa',
105             'ext': 'mp4',
106             'upload_date': '20130401',
107             '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',
108             'title': 'A Moment With TFS Episode 4',
109         }
110     }
111
112     def _real_extract(self, url):
113         display_id = self._match_id(url)
114         webpage = self._download_webpage(url, display_id)
115
116         playerdata_url = self._search_regex(
117             r'src="(http://player\d?\.screenwavemedia\.com/(?:play/)?[a-zA-Z]+\.php\?[^"]*\bid=.+?)"',
118             webpage, 'player data URL')
119
120         video_title = self._html_search_regex(
121             r'<div class="heroheadingtitle">(?P<title>.+?)</div>',
122             webpage, 'title')
123         video_date = unified_strdate(self._html_search_regex(
124             r'<div class="heroheadingdate">(?P<date>.+?)</div>',
125             webpage, 'date', fatal=False))
126         video_description = self._html_search_regex(
127             r'(?s)<div class="postcontent">(?P<description>.+?)</div>',
128             webpage, 'description', fatal=False)
129         video_thumbnail = self._og_search_thumbnail(webpage)
130
131         return {
132             '_type': 'url_transparent',
133             'display_id': display_id,
134             'title': video_title,
135             'description': video_description,
136             'upload_date': video_date,
137             'thumbnail': video_thumbnail,
138             'url': playerdata_url,
139         }