[CinemassacreIE] Use MD5 to check in TEST description
[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 class CinemassacreIE(InfoExtractor):
10     _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})/.+?)(?:[/?].*)?'
11     _TESTS = [{
12         u'url': u'http://cinemassacre.com/2012/11/10/avgn-the-movie-trailer/',
13         u'file': u'19911.mp4',
14         u'info_dict': {
15             u'upload_date': u'20121110', 
16             u'title': u'“Angry Video Game Nerd: The Movie” – Trailer',
17             u'description': u'md5:fb87405fcb42a331742a0dce2708560b',
18         },
19         u'params': {
20             u'skip_download': True,
21         },
22     },
23     {
24         u'url': u'http://cinemassacre.com/2013/10/02/the-mummys-hand-1940',
25         u'file': u'521be8ef82b16.mp4',
26         u'info_dict': {
27             u'upload_date': u'20131002', 
28             u'title': u'The Mummy’s Hand (1940)',
29         },
30         u'params': {
31             u'skip_download': True,
32         },
33     }]
34
35     def _real_extract(self,url):
36         mobj = re.match(self._VALID_URL, url)
37
38         webpage_url = u'http://' + mobj.group('url')
39         webpage = self._download_webpage(webpage_url, None) # Don't know video id yet
40         video_date = mobj.group('date_Y') + mobj.group('date_m') + mobj.group('date_d')
41         mobj = re.search(r'src="(?P<embed_url>http://player\.screenwavemedia\.com/play/(?:embed|player)\.php\?id=(?:Cinemassacre-)?(?P<video_id>.+?))"', webpage)
42         if not mobj:
43             raise ExtractorError(u'Can\'t extract embed url and video id')
44         playerdata_url = mobj.group(u'embed_url')
45         video_id = mobj.group(u'video_id')
46
47         video_title = self._html_search_regex(r'<title>(?P<title>.+?)\|',
48             webpage, u'title')
49         video_description = self._html_search_regex(r'<div class="entry-content">(?P<description>.+?)</div>',
50             webpage, u'description', flags=re.DOTALL, fatal=False)
51         if len(video_description) == 0:
52             video_description = None
53         
54         playerdata = self._download_webpage(playerdata_url, video_id)
55         base_url = self._html_search_regex(r'\'streamer\': \'(?P<base_url>rtmp://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/(?:vod|Cinemassacre)\'',
56             playerdata, u'base_url')
57         base_url += '/Cinemassacre/'
58          # Important: The file names in playerdata are not used by the player and even wrong for some videos
59         sd_file = 'Cinemassacre-%s_high.mp4' % video_id
60         hd_file = 'Cinemassacre-%s.mp4' % video_id
61         video_thumbnail = 'http://image.screenwavemedia.com/Cinemassacre/Cinemassacre-%s_thumb_640x360.jpg' % video_id
62         
63         formats = [{
64             'id':          video_id,
65             'url':         base_url + hd_file,
66             'format':      'hd',
67             'ext':         'mp4',
68             'title':       video_title,
69             'description': video_description,
70             'upload_date': video_date,
71             'thumbnail':   video_thumbnail,
72         },
73         {
74             'id':          video_id,
75             'url':         base_url + sd_file,
76             'ext':         'mp4',
77             'format':      'sd',
78             'title':       video_title,
79             'description': video_description,
80             'upload_date': video_date,
81             'thumbnail':   video_thumbnail,
82         }]
83         
84         if self._downloader.params.get('listformats', None):
85             self._print_formats(formats)
86             return
87
88         req_format = self._downloader.params.get('format', 'best')
89         self.to_screen(u'Format: %s' % req_format)
90
91         if req_format is None or req_format == 'best':
92             return [formats[0]]
93         elif req_format == 'worst':
94             return [formats[-1]]
95         elif req_format in ('-1', 'all'):
96             return formats
97         else:
98             format = self._specific( req_format, formats )
99             if format is None:
100                 raise ExtractorError(u'Requested format not available')
101             return [format]
102
103     def _print_formats(self, formats):
104         """Print all available formats"""
105         print(u'Available formats:')
106         print(u'ext\t\tformat')
107         print(u'---------------------------------')
108         for format in formats:
109             print(u'%s\t\t%s'  % (format['ext'], format['format']))
110
111     def _specific(self, req_format, formats):
112         for x in formats:
113             if x["format"] == req_format:
114                 return x
115         return None