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