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