31fe906b448669ae94c185546a5402314b975224
[youtube-dl] / youtube_dl / extractor / cinemassacre.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10 )
11
12
13 class CinemassacreIE(InfoExtractor):
14     _VALID_URL = r'http://(?:www\.)?cinemassacre\.com/(?P<date_Y>[0-9]{4})/(?P<date_m>[0-9]{2})/(?P<date_d>[0-9]{2})/(?P<display_id>[^?#/]+)'
15     _TESTS = [
16         {
17             'url': 'http://cinemassacre.com/2012/11/10/avgn-the-movie-trailer/',
18             'md5': 'fde81fbafaee331785f58cd6c0d46190',
19             'info_dict': {
20                 'id': '19911',
21                 'ext': 'mp4',
22                 'upload_date': '20121110',
23                 'title': '“Angry Video Game Nerd: The Movie” – Trailer',
24                 'description': 'md5:fb87405fcb42a331742a0dce2708560b',
25             },
26         },
27         {
28             'url': 'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
29             'md5': 'd72f10cd39eac4215048f62ab477a511',
30             'info_dict': {
31                 'id': '521be8ef82b16',
32                 'ext': 'mp4',
33                 'upload_date': '20131002',
34                 'title': 'The Mummy’s Hand (1940)',
35             },
36         }
37     ]
38
39     def _real_extract(self, url):
40         mobj = re.match(self._VALID_URL, url)
41         display_id = mobj.group('display_id')
42
43         webpage = self._download_webpage(url, display_id)
44         video_date = mobj.group('date_Y') + mobj.group('date_m') + mobj.group('date_d')
45         mobj = re.search(r'src="(?P<embed_url>http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?[^"]*\bid=(?P<full_video_id>(?:Cinemassacre-)?(?P<video_id>.+?)))"', webpage)
46         if not mobj:
47             raise ExtractorError('Can\'t extract embed url and video id')
48         playerdata_url = mobj.group('embed_url')
49         video_id = mobj.group('video_id')
50         full_video_id = mobj.group('full_video_id')
51
52         video_title = self._html_search_regex(
53             r'<title>(?P<title>.+?)\|', webpage, 'title')
54         video_description = self._html_search_regex(
55             r'<div class="entry-content">(?P<description>.+?)</div>',
56             webpage, 'description', flags=re.DOTALL, fatal=False)
57         video_thumbnail = self._og_search_thumbnail(webpage)
58
59         playerdata = self._download_webpage(playerdata_url, video_id, 'Downloading player webpage')
60
61         vidurl = self._search_regex(
62             r'\'vidurl\'\s*:\s*"([^\']+)"', playerdata, 'vidurl').replace('\\/', '/')
63
64         videolist_url = None
65
66         mobj = re.search(r"'videoserver'\s*:\s*'(?P<videoserver>[^']+)'", playerdata)
67         if mobj:
68             videoserver = mobj.group('videoserver')
69             mobj = re.search(r'\'vidid\'\s*:\s*"(?P<vidid>[^\']+)"', playerdata)
70             vidid = mobj.group('vidid') if mobj else full_video_id
71             videolist_url = 'http://%s/vod/smil:%s.smil/jwplayer.smil' % (videoserver, vidid)
72         else:
73             mobj = re.search(r"file\s*:\s*'(?P<smil>http.+?/jwplayer\.smil)'", playerdata)
74             if mobj:
75                 videolist_url = mobj.group('smil')
76
77         if videolist_url:
78             videolist = self._download_xml(videolist_url, video_id, 'Downloading videolist XML')
79             formats = []
80             baseurl = vidurl[:vidurl.rfind('/')+1]
81             for video in videolist.findall('.//video'):
82                 src = video.get('src')
83                 if not src:
84                     continue
85                 file_ = src.partition(':')[-1]
86                 width = int_or_none(video.get('width'))
87                 height = int_or_none(video.get('height'))
88                 bitrate = int_or_none(video.get('system-bitrate'))
89                 format = {
90                     'url': baseurl + file_,
91                     'format_id': src.rpartition('.')[0].rpartition('_')[-1],
92                 }
93                 if width or height:
94                     format.update({
95                         'tbr': bitrate // 1000 if bitrate else None,
96                         'width': width,
97                         'height': height,
98                     })
99                 else:
100                     format.update({
101                         'abr': bitrate // 1000 if bitrate else None,
102                         'vcodec': 'none',
103                     })
104                 formats.append(format)
105             self._sort_formats(formats)
106         else:
107             formats = [{
108                 'url': vidurl,
109             }]
110
111         return {
112             'id': video_id,
113             'title': video_title,
114             'formats': formats,
115             'description': video_description,
116             'upload_date': video_date,
117             'thumbnail': video_thumbnail,
118         }