Merge branch 'remitamine-snagfilms'
[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/(?P<id>[^/]+)'
80     _TEST = {
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
95     def _real_extract(self, url):
96         display_id = self._match_id(url)
97
98         webpage = self._download_webpage(url, display_id)
99
100         film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')
101
102         snag = self._parse_json(
103             self._search_regex(
104                 'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag'),
105             display_id)
106
107         for item in snag:
108             if item.get('data', {}).get('film', {}).get('id') == film_id:
109                 data = item['data']['film']
110                 title = data['title']
111                 description = clean_html(data.get('synopsis'))
112                 thumbnail = data.get('image')
113                 duration = int_or_none(data.get('duration') or data.get('runtime'))
114                 categories = [
115                     category['title'] for category in data.get('categories', [])
116                     if category.get('title')]
117                 break
118         else:
119             title = self._search_regex(
120                 r'itemprop="title">([^<]+)<', webpage, 'title')
121             description = self._html_search_regex(
122                 r'(?s)<div itemprop="description" class="film-synopsis-inner ">(.+?)</div>',
123                 webpage, 'description', default=None) or self._og_search_description(webpage)
124             thumbnail = self._og_search_thumbnail(webpage)
125             duration = parse_duration(self._search_regex(
126                 r'<span itemprop="duration" class="film-duration strong">([^<]+)<',
127                 webpage, 'duration', fatal=False))
128             categories = re.findall(r'<a href="/movies/[^"]+">([^<]+)</a>', webpage)
129
130         return {
131             '_type': 'url_transparent',
132             'url': 'http://embed.snagfilms.com/embed/player?filmId=%s' % film_id,
133             'id': film_id,
134             'display_id': display_id,
135             'title': title,
136             'description': description,
137             'thumbnail': thumbnail,
138             'duration': duration,
139             'categories': categories,
140         }