Merge pull request #1632 from rzhxeo/cinemassacre
[youtube-dl] / youtube_dl / extractor / cinemassacre.py
1 # encoding: utf-8
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7 )
8
9
10 class CinemassacreIE(InfoExtractor):
11     _VALID_URL = r'(?:http://)?(?:www\.)?(?P<url>cinemassacre\.com/(?P<date_Y>[0-9]{4})/(?P<date_m>[0-9]{2})/(?P<date_d>[0-9]{2})/.+?)(?:[/?].*)?'
12     _TESTS = [{
13         u'url': u'http://cinemassacre.com/2012/11/10/avgn-the-movie-trailer/',
14         u'file': u'19911.flv',
15         u'md5': u'f9bb7ede54d1229c9846e197b4737e06',
16         u'info_dict': {
17             u'upload_date': u'20121110',
18             u'title': u'“Angry Video Game Nerd: The Movie” – Trailer',
19             u'description': u'md5:fb87405fcb42a331742a0dce2708560b',
20         },
21         u'params': {
22             # rtmp download
23             u'skip_download': True,
24         },
25     },
26     {
27         u'url': u'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
28         u'file': u'521be8ef82b16.flv',
29         u'md5': u'91b248e1e2473d5bff55d6010518111f',
30         u'info_dict': {
31             u'upload_date': u'20131002',
32             u'title': u'The Mummy’s Hand (1940)',
33         },
34         u'params': {
35             # rtmp download
36             u'skip_download': True,
37         },
38     }]
39
40     def _real_extract(self, url):
41         mobj = re.match(self._VALID_URL, url)
42
43         webpage_url = u'http://' + mobj.group('url')
44         webpage = self._download_webpage(webpage_url, None) # Don't know video id yet
45         video_date = mobj.group('date_Y') + mobj.group('date_m') + mobj.group('date_d')
46         mobj = re.search(r'src="(?P<embed_url>http://player\.screenwavemedia\.com/play/(?:embed|player)\.php\?id=(?:Cinemassacre-)?(?P<video_id>.+?))"', webpage)
47         if not mobj:
48             raise ExtractorError(u'Can\'t extract embed url and video id')
49         playerdata_url = mobj.group(u'embed_url')
50         video_id = mobj.group(u'video_id')
51
52         video_title = self._html_search_regex(r'<title>(?P<title>.+?)\|',
53             webpage, u'title')
54         video_description = self._html_search_regex(r'<div class="entry-content">(?P<description>.+?)</div>',
55             webpage, u'description', flags=re.DOTALL, fatal=False)
56         if len(video_description) == 0:
57             video_description = None
58
59         playerdata = self._download_webpage(playerdata_url, video_id)
60         url = self._html_search_regex(r'\'streamer\': \'(?P<url>[^\']+)\'', playerdata, u'url')
61         player_url = self._html_search_regex(r'\'flashplayer\': \'(?P<player_url>[^\']+)\'', playerdata, u'player_url')
62         page_url = re.split(r'(?<=[^/])/([^/]|$)', player_url)[0]
63
64         sd_file = self._html_search_regex(r'\'file\': \'(?P<sd_file>[^\']+)\'', playerdata, u'sd_file')
65         hd_file = self._html_search_regex(r'\'?file\'?: "(?P<hd_file>[^"]+)"', playerdata, u'hd_file')
66         video_thumbnail = self._html_search_regex(r'\'image\': \'(?P<thumbnail>[^\']+)\'', playerdata, u'thumbnail', fatal=False)
67
68         formats = [
69             {
70                 'url': url,
71                 'player_url': player_url,
72                 'page_url': page_url,
73                 'play_path': 'mp4:' + sd_file,
74                 'ext': 'flv',
75                 'format': 'sd',
76                 'format_id': 'sd',
77             },
78             {
79                 'url': url,
80                 'player_url': player_url,
81                 'page_url': page_url,
82                 'play_path': 'mp4:' + hd_file,
83                 'ext': 'flv',
84                 'format': 'hd',
85                 'format_id': 'hd',
86             },
87         ]
88
89         info = {
90             'id': video_id,
91             'title': video_title,
92             'formats': formats,
93             'description': video_description,
94             'upload_date': video_date,
95             'thumbnail': video_thumbnail,
96         }
97         # TODO: Remove when #980 has been merged
98         info.update(formats[-1])
99         return info