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