]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/viewlift.py
[nrk] Add X-Forwarded-For HTTP header in info dict
[youtube-dl] / youtube_dl / extractor / viewlift.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 ViewLiftBaseIE(InfoExtractor):
17     _DOMAINS_REGEX = '(?:snagfilms|snagxtreme|funnyforfree|kiddovid|winnersview|monumentalsportsnetwork|vayafilm)\.com|kesari\.tv'
18
19
20 class ViewLiftEmbedIE(ViewLiftBaseIE):
21     _VALID_URL = r'https?://(?:(?:www|embed)\.)?(?:%s)/embed/player\?.*\bfilmId=(?P<id>[\da-f-]{36})' % ViewLiftBaseIE._DOMAINS_REGEX
22     _TESTS = [{
23         'url': 'http://embed.snagfilms.com/embed/player?filmId=74849a00-85a9-11e1-9660-123139220831&w=500',
24         'md5': '2924e9215c6eff7a55ed35b72276bd93',
25         'info_dict': {
26             'id': '74849a00-85a9-11e1-9660-123139220831',
27             'ext': 'mp4',
28             'title': '#whilewewatch',
29         }
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         }
39     }, {
40         'url': 'http://www.snagfilms.com/embed/player?filmId=0000014c-de2f-d5d6-abcf-ffef58af0017',
41         'only_matching': True,
42     }]
43
44     @staticmethod
45     def _extract_url(webpage):
46         mobj = re.search(
47             r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:embed\.)?(?:%s)/embed/player.+?)\1' % ViewLiftBaseIE._DOMAINS_REGEX,
48             webpage)
49         if mobj:
50             return mobj.group('url')
51
52     def _real_extract(self, url):
53         video_id = self._match_id(url)
54
55         webpage = self._download_webpage(url, video_id)
56
57         if '>This film is not playable in your area.<' in webpage:
58             raise ExtractorError(
59                 'Film %s is not playable in your area.' % video_id, expected=True)
60
61         formats = []
62         has_bitrate = False
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')
69             ext = determine_ext(file_)
70             format_id = source.get('label') or ext
71             if all(v == 'm3u8' or v == 'hls' for v in (type_, ext)):
72                 formats.extend(self._extract_m3u8_formats(
73                     file_, video_id, 'mp4', m3u8_id='hls'))
74             else:
75                 bitrate = int_or_none(self._search_regex(
76                     [r'(\d+)kbps', r'_\d{1,2}x\d{1,2}_(\d{3,})\.%s' % ext],
77                     file_, 'bitrate', default=None))
78                 if not has_bitrate and bitrate:
79                     has_bitrate = True
80                 height = int_or_none(self._search_regex(
81                     r'^(\d+)[pP]$', format_id, 'height', default=None))
82                 formats.append({
83                     'url': file_,
84                     'format_id': 'http-%s%s' % (format_id, ('-%dk' % bitrate if bitrate else '')),
85                     'tbr': bitrate,
86                     'height': height,
87                 })
88         field_preference = None if has_bitrate else ('height', 'tbr', 'format_id')
89         self._sort_formats(formats, field_preference)
90
91         title = self._search_regex(
92             [r"title\s*:\s*'([^']+)'", r'<title>([^<]+)</title>'],
93             webpage, 'title')
94
95         return {
96             'id': video_id,
97             'title': title,
98             'formats': formats,
99         }
100
101
102 class ViewLiftIE(ViewLiftBaseIE):
103     _VALID_URL = r'https?://(?:www\.)?(?P<domain>%s)/(?:films/title|show|(?:news/)?videos?)/(?P<id>[^?#]+)' % ViewLiftBaseIE._DOMAINS_REGEX
104     _TESTS = [{
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',
113             'thumbnail': 're:^https?://.*\.jpg',
114             'duration': 4489,
115             'categories': ['Documentary', 'Crime', 'Award Winning', 'Festivals']
116         }
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',
126             'thumbnail': 're:^https?://.*\.jpg',
127             'duration': 979,
128             'categories': ['Documentary', 'Sports', 'Politics']
129         }
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,
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,
144     }, {
145         # Was once Kaltura embed
146         'url': 'https://www.monumentalsportsnetwork.com/videos/john-carlson-postgame-2-25-15',
147         'only_matching': True,
148     }]
149
150     def _real_extract(self, url):
151         domain, display_id = re.match(self._VALID_URL, url).groups()
152
153         webpage = self._download_webpage(url, display_id)
154
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
159         film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')
160
161         snag = self._parse_json(
162             self._search_regex(
163                 'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag'),
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',
191             'url': 'http://%s/embed/player?filmId=%s' % (domain, film_id),
192             'id': film_id,
193             'display_id': display_id,
194             'title': title,
195             'description': description,
196             'thumbnail': thumbnail,
197             'duration': duration,
198             'categories': categories,
199             'ie_key': 'ViewLiftEmbed',
200         }