]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/viewlift.py
Fix "invalid escape sequences" error on Python 3.6
[yt-dlp.git] / youtube_dl / extractor / viewlift.py
CommitLineData
654fd03c
S
1from __future__ import unicode_literals
2
3import re
4
03339b7b 5from .common import InfoExtractor
654fd03c 6from ..utils import (
9fbfc9bd 7 ExtractorError,
654fd03c
S
8 clean_html,
9 determine_ext,
10 int_or_none,
11 js_to_json,
12 parse_duration,
13)
03339b7b 14
654fd03c 15
67167920 16class ViewLiftBaseIE(InfoExtractor):
ec85ded8 17 _DOMAINS_REGEX = r'(?:snagfilms|snagxtreme|funnyforfree|kiddovid|winnersview|monumentalsportsnetwork|vayafilm)\.com|kesari\.tv'
67167920 18
19
20class ViewLiftEmbedIE(ViewLiftBaseIE):
21 _VALID_URL = r'https?://(?:(?:www|embed)\.)?(?:%s)/embed/player\?.*\bfilmId=(?P<id>[\da-f-]{36})' % ViewLiftBaseIE._DOMAINS_REGEX
7d7d4690 22 _TESTS = [{
654fd03c
S
23 'url': 'http://embed.snagfilms.com/embed/player?filmId=74849a00-85a9-11e1-9660-123139220831&w=500',
24 'md5': '2924e9215c6eff7a55ed35b72276bd93',
25 'info_dict': {
7d7d4690 26 'id': '74849a00-85a9-11e1-9660-123139220831',
7d7d4690 27 'ext': 'mp4',
28 'title': '#whilewewatch',
7d7d4690 29 }
496ce6b3
S
30 }, {
31 # invalid labels, 360p is better that 480p
32 'url': 'http://www.snagfilms.com/embed/player?filmId=17ca0950-a74a-11e0-a92a-0026bb61d036',
33 'md5': '882fca19b9eb27ef865efeeaed376a48',
34 'info_dict': {
35 'id': '17ca0950-a74a-11e0-a92a-0026bb61d036',
36 'ext': 'mp4',
37 'title': 'Life in Limbo',
38 }
654fd03c
S
39 }, {
40 'url': 'http://www.snagfilms.com/embed/player?filmId=0000014c-de2f-d5d6-abcf-ffef58af0017',
41 'only_matching': True,
7d7d4690 42 }]
03339b7b 43
7c197ad9
S
44 @staticmethod
45 def _extract_url(webpage):
46 mobj = re.search(
67167920 47 r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:embed\.)?(?:%s)/embed/player.+?)\1' % ViewLiftBaseIE._DOMAINS_REGEX,
9fbfc9bd 48 webpage)
7c197ad9
S
49 if mobj:
50 return mobj.group('url')
51
7e0480ae 52 def _real_extract(self, url):
654fd03c
S
53 video_id = self._match_id(url)
54
55 webpage = self._download_webpage(url, video_id)
03339b7b 56
9fbfc9bd
S
57 if '>This film is not playable in your area.<' in webpage:
58 raise ExtractorError(
a42a1bb0 59 'Film %s is not playable in your area.' % video_id, expected=True)
9fbfc9bd 60
7e0480ae 61 formats = []
67167920 62 has_bitrate = False
654fd03c
S
63 for source in self._parse_json(js_to_json(self._search_regex(
64 r'(?s)sources:\s*(\[.+?\]),', webpage, 'json')), video_id):
65 file_ = source.get('file')
66 if not file_:
67 continue
68 type_ = source.get('type')
654fd03c 69 ext = determine_ext(file_)
496ce6b3 70 format_id = source.get('label') or ext
67167920 71 if all(v == 'm3u8' or v == 'hls' for v in (type_, ext)):
654fd03c
S
72 formats.extend(self._extract_m3u8_formats(
73 file_, video_id, 'mp4', m3u8_id='hls'))
7e0480ae 74 else:
654fd03c 75 bitrate = int_or_none(self._search_regex(
496ce6b3
S
76 [r'(\d+)kbps', r'_\d{1,2}x\d{1,2}_(\d{3,})\.%s' % ext],
77 file_, 'bitrate', default=None))
67167920 78 if not has_bitrate and bitrate:
79 has_bitrate = True
654fd03c
S
80 height = int_or_none(self._search_regex(
81 r'^(\d+)[pP]$', format_id, 'height', default=None))
82 formats.append({
83 'url': file_,
67167920 84 'format_id': 'http-%s%s' % (format_id, ('-%dk' % bitrate if bitrate else '')),
654fd03c
S
85 'tbr': bitrate,
86 'height': height,
87 })
67167920 88 field_preference = None if has_bitrate else ('height', 'tbr', 'format_id')
89 self._sort_formats(formats, field_preference)
03339b7b 90
654fd03c
S
91 title = self._search_regex(
92 [r"title\s*:\s*'([^']+)'", r'<title>([^<]+)</title>'],
93 webpage, 'title')
94
7e0480ae 95 return {
96 'id': video_id,
654fd03c
S
97 'title': title,
98 'formats': formats,
99 }
100
101
67167920 102class ViewLiftIE(ViewLiftBaseIE):
103 _VALID_URL = r'https?://(?:www\.)?(?P<domain>%s)/(?:films/title|show|(?:news/)?videos?)/(?P<id>[^?#]+)' % ViewLiftBaseIE._DOMAINS_REGEX
242a998b 104 _TESTS = [{
654fd03c
S
105 'url': 'http://www.snagfilms.com/films/title/lost_for_life',
106 'md5': '19844f897b35af219773fd63bdec2942',
107 'info_dict': {
108 'id': '0000014c-de2f-d5d6-abcf-ffef58af0017',
109 'display_id': 'lost_for_life',
110 'ext': 'mp4',
111 'title': 'Lost for Life',
112 'description': 'md5:fbdacc8bb6b455e464aaf98bc02e1c82',
ec85ded8 113 'thumbnail': r're:^https?://.*\.jpg',
654fd03c
S
114 'duration': 4489,
115 'categories': ['Documentary', 'Crime', 'Award Winning', 'Festivals']
116 }
242a998b
S
117 }, {
118 'url': 'http://www.snagfilms.com/show/the_world_cut_project/india',
119 'md5': 'e6292e5b837642bbda82d7f8bf3fbdfd',
120 'info_dict': {
121 'id': '00000145-d75c-d96e-a9c7-ff5c67b20000',
122 'display_id': 'the_world_cut_project/india',
123 'ext': 'mp4',
124 'title': 'India',
125 'description': 'md5:5c168c5a8f4719c146aad2e0dfac6f5f',
ec85ded8 126 'thumbnail': r're:^https?://.*\.jpg',
242a998b
S
127 'duration': 979,
128 'categories': ['Documentary', 'Sports', 'Politics']
129 }
a9de9517
S
130 }, {
131 # Film is not playable in your area.
132 'url': 'http://www.snagfilms.com/films/title/inside_mecca',
133 'only_matching': True,
134 }, {
135 # Film is not available.
136 'url': 'http://www.snagfilms.com/show/augie_alone/flirting',
137 'only_matching': True,
67167920 138 }, {
139 'url': 'http://www.winnersview.com/videos/the-good-son',
140 'only_matching': True,
141 }, {
142 'url': 'http://www.kesari.tv/news/video/1461919076414',
143 'only_matching': True,
28bab133
YCH
144 }, {
145 # Was once Kaltura embed
146 'url': 'https://www.monumentalsportsnetwork.com/videos/john-carlson-postgame-2-25-15',
147 'only_matching': True,
242a998b 148 }]
654fd03c
S
149
150 def _real_extract(self, url):
67167920 151 domain, display_id = re.match(self._VALID_URL, url).groups()
654fd03c
S
152
153 webpage = self._download_webpage(url, display_id)
154
a42a1bb0
S
155 if ">Sorry, the Film you're looking for is not available.<" in webpage:
156 raise ExtractorError(
157 'Film %s is not available.' % display_id, expected=True)
158
654fd03c
S
159 film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')
160
161 snag = self._parse_json(
162 self._search_regex(
ec85ded8 163 r'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag'),
654fd03c
S
164 display_id)
165
166 for item in snag:
167 if item.get('data', {}).get('film', {}).get('id') == film_id:
168 data = item['data']['film']
169 title = data['title']
170 description = clean_html(data.get('synopsis'))
171 thumbnail = data.get('image')
172 duration = int_or_none(data.get('duration') or data.get('runtime'))
173 categories = [
174 category['title'] for category in data.get('categories', [])
175 if category.get('title')]
176 break
177 else:
178 title = self._search_regex(
179 r'itemprop="title">([^<]+)<', webpage, 'title')
180 description = self._html_search_regex(
181 r'(?s)<div itemprop="description" class="film-synopsis-inner ">(.+?)</div>',
182 webpage, 'description', default=None) or self._og_search_description(webpage)
183 thumbnail = self._og_search_thumbnail(webpage)
184 duration = parse_duration(self._search_regex(
185 r'<span itemprop="duration" class="film-duration strong">([^<]+)<',
186 webpage, 'duration', fatal=False))
187 categories = re.findall(r'<a href="/movies/[^"]+">([^<]+)</a>', webpage)
188
189 return {
190 '_type': 'url_transparent',
67167920 191 'url': 'http://%s/embed/player?filmId=%s' % (domain, film_id),
654fd03c 192 'id': film_id,
7e0480ae 193 'display_id': display_id,
194 'title': title,
7e0480ae 195 'description': description,
7d7d4690 196 'thumbnail': thumbnail,
654fd03c
S
197 'duration': duration,
198 'categories': categories,
67167920 199 'ie_key': 'ViewLiftEmbed',
7e0480ae 200 }