[cinemassacre] Fix extraction
[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.mp4',
15         u'md5': u'fde81fbafaee331785f58cd6c0d46190',
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     },
22     {
23         u'url': u'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
24         u'file': u'521be8ef82b16.mp4',
25         u'md5': u'd72f10cd39eac4215048f62ab477a511',
26         u'info_dict': {
27             u'upload_date': u'20131002',
28             u'title': u'The Mummy’s Hand (1940)',
29         },
30     }]
31
32     def _real_extract(self, url):
33         mobj = re.match(self._VALID_URL, url)
34
35         webpage_url = u'http://' + mobj.group('url')
36         webpage = self._download_webpage(webpage_url, None) # Don't know video id yet
37         video_date = mobj.group('date_Y') + mobj.group('date_m') + mobj.group('date_d')
38         mobj = re.search(r'src="(?P<embed_url>http://player\.screenwavemedia\.com/play/[a-zA-Z]+\.php\?id=(?:Cinemassacre-)?(?P<video_id>.+?))"', webpage)
39         if not mobj:
40             raise ExtractorError(u'Can\'t extract embed url and video id')
41         playerdata_url = mobj.group(u'embed_url')
42         video_id = mobj.group(u'video_id')
43
44         video_title = self._html_search_regex(r'<title>(?P<title>.+?)\|',
45             webpage, u'title')
46         video_description = self._html_search_regex(r'<div class="entry-content">(?P<description>.+?)</div>',
47             webpage, u'description', flags=re.DOTALL, fatal=False)
48         if len(video_description) == 0:
49             video_description = None
50
51         playerdata = self._download_webpage(playerdata_url, video_id)
52
53         sd_url = self._html_search_regex(r'file: \'(?P<sd_file>[^\']+)\', label: \'SD\'', playerdata, u'sd_file')
54         hd_url= self._html_search_regex(r'file: \'(?P<hd_file>[^\']+)\', label: \'HD\'', playerdata, u'hd_file')
55         video_thumbnail = self._html_search_regex(r'image: \'(?P<thumbnail>[^\']+)\'', playerdata, u'thumbnail', fatal=False)
56
57         formats = [
58             {
59                 'url': sd_url,
60                 'ext': 'mp4',
61                 'format': 'sd',
62                 'format_id': 'sd',
63             },
64             {
65                 'url': hd_url,
66                 'ext': 'mp4',
67                 'format': 'hd',
68                 'format_id': 'hd',
69             },
70         ]
71
72         return {
73             'id': video_id,
74             'title': video_title,
75             'formats': formats,
76             'description': video_description,
77             'upload_date': video_date,
78             'thumbnail': video_thumbnail,
79         }