[snagfilms] Improve and simplify
[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     def _real_extract(self, url):
31         video_id = self._match_id(url)
32
33         webpage = self._download_webpage(url, video_id)
34
35         formats = []
36         for source in self._parse_json(js_to_json(self._search_regex(
37                 r'(?s)sources:\s*(\[.+?\]),', webpage, 'json')), video_id):
38             file_ = source.get('file')
39             if not file_:
40                 continue
41             type_ = source.get('type')
42             format_id = source.get('label')
43             ext = determine_ext(file_)
44             if any(_ == 'm3u8' for _ in (type_, ext)):
45                 formats.extend(self._extract_m3u8_formats(
46                     file_, video_id, 'mp4', m3u8_id='hls'))
47             else:
48                 bitrate = int_or_none(self._search_regex(
49                     r'(\d+)kbps', file_, 'bitrate', default=None))
50                 height = int_or_none(self._search_regex(
51                     r'^(\d+)[pP]$', format_id, 'height', default=None))
52                 formats.append({
53                     'url': file_,
54                     'format_id': format_id,
55                     'tbr': bitrate,
56                     'height': height,
57                 })
58         self._sort_formats(formats)
59
60         title = self._search_regex(
61             [r"title\s*:\s*'([^']+)'", r'<title>([^<]+)</title>'],
62             webpage, 'title')
63
64         return {
65             'id': video_id,
66             'title': title,
67             'formats': formats,
68         }
69
70
71 class SnagFilmsIE(InfoExtractor):
72     _VALID_URL = r'https?://(?:www\.)?snagfilms\.com/films/title/(?P<id>[^/]+)'
73     _TEST = {
74         'url': 'http://www.snagfilms.com/films/title/lost_for_life',
75         'md5': '19844f897b35af219773fd63bdec2942',
76         'info_dict': {
77             'id': '0000014c-de2f-d5d6-abcf-ffef58af0017',
78             'display_id': 'lost_for_life',
79             'ext': 'mp4',
80             'title': 'Lost for Life',
81             'description': 'md5:fbdacc8bb6b455e464aaf98bc02e1c82',
82             'thumbnail': 're:^https?://.*\.jpg',
83             'duration': 4489,
84             'categories': ['Documentary', 'Crime', 'Award Winning', 'Festivals']
85         }
86     }
87
88     def _real_extract(self, url):
89         display_id = self._match_id(url)
90
91         webpage = self._download_webpage(url, display_id)
92
93         film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')
94
95         snag = self._parse_json(
96             self._search_regex(
97                 'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag'),
98             display_id)
99
100         for item in snag:
101             if item.get('data', {}).get('film', {}).get('id') == film_id:
102                 data = item['data']['film']
103                 title = data['title']
104                 description = clean_html(data.get('synopsis'))
105                 thumbnail = data.get('image')
106                 duration = int_or_none(data.get('duration') or data.get('runtime'))
107                 categories = [
108                     category['title'] for category in data.get('categories', [])
109                     if category.get('title')]
110                 break
111         else:
112             title = self._search_regex(
113                 r'itemprop="title">([^<]+)<', webpage, 'title')
114             description = self._html_search_regex(
115                 r'(?s)<div itemprop="description" class="film-synopsis-inner ">(.+?)</div>',
116                 webpage, 'description', default=None) or self._og_search_description(webpage)
117             thumbnail = self._og_search_thumbnail(webpage)
118             duration = parse_duration(self._search_regex(
119                 r'<span itemprop="duration" class="film-duration strong">([^<]+)<',
120                 webpage, 'duration', fatal=False))
121             categories = re.findall(r'<a href="/movies/[^"]+">([^<]+)</a>', webpage)
122
123         return {
124             '_type': 'url_transparent',
125             'url': 'http://embed.snagfilms.com/embed/player?filmId=%s' % film_id,
126             'id': film_id,
127             'display_id': display_id,
128             'title': title,
129             'description': description,
130             'thumbnail': thumbnail,
131             'duration': duration,
132             'categories': categories,
133         }