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