]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/metacafe.py
91480ba875d5fff781ce08a47c41a3824e94e910
[youtubedl] / youtube_dl / extractor / metacafe.py
1 import re
2 import socket
3
4 from .common import InfoExtractor
5 from ..utils import (
6 compat_http_client,
7 compat_parse_qs,
8 compat_urllib_error,
9 compat_urllib_parse,
10 compat_urllib_request,
11 compat_str,
12 determine_ext,
13 ExtractorError,
14 )
15
16 class MetacafeIE(InfoExtractor):
17 """Information Extractor for metacafe.com."""
18
19 _VALID_URL = r'(?:http://)?(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
20 _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
21 _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
22 IE_NAME = u'metacafe'
23 _TESTS = [
24 # Youtube video
25 {
26 u"add_ie": ["Youtube"],
27 u"url": u"http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/",
28 u"file": u"_aUehQsCQtM.mp4",
29 u"info_dict": {
30 u"upload_date": u"20090102",
31 u"title": u"The Electric Company | \"Short I\" | PBS KIDS GO!",
32 u"description": u"md5:2439a8ef6d5a70e380c22f5ad323e5a8",
33 u"uploader": u"PBS",
34 u"uploader_id": u"PBS"
35 }
36 },
37 # Normal metacafe video
38 {
39 u'url': u'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
40 u'md5': u'6e0bca200eaad2552e6915ed6fd4d9ad',
41 u'info_dict': {
42 u'id': u'11121940',
43 u'ext': u'mp4',
44 u'title': u'News: Stuff You Won\'t Do with Your PlayStation 4',
45 u'uploader': u'ign',
46 u'description': u'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
47 },
48 },
49 # AnyClip video
50 {
51 u"url": u"http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/",
52 u"file": u"an-dVVXnuY7Jh77J.mp4",
53 u"info_dict": {
54 u"title": u"The Andromeda Strain (1971): Stop the Bomb Part 3",
55 u"uploader": u"anyclip",
56 u"description": u"md5:38c711dd98f5bb87acf973d573442e67",
57 },
58 },
59 # age-restricted video
60 {
61 u'url': u'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
62 u'md5': u'98dde7c1a35d02178e8ab7560fe8bd09',
63 u'info_dict': {
64 u'id': u'5186653',
65 u'ext': u'mp4',
66 u'title': u'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
67 u'uploader': u'Dwayne Pipe',
68 u'description': u'md5:950bf4c581e2c059911fa3ffbe377e4b',
69 u'age_limit': 18,
70 },
71 },
72 ]
73
74
75 def report_disclaimer(self):
76 """Report disclaimer retrieval."""
77 self.to_screen(u'Retrieving disclaimer')
78
79 def _real_initialize(self):
80 # Retrieve disclaimer
81 request = compat_urllib_request.Request(self._DISCLAIMER)
82 try:
83 self.report_disclaimer()
84 compat_urllib_request.urlopen(request).read()
85 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
86 raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
87
88 # Confirm age
89 disclaimer_form = {
90 'filters': '0',
91 'submit': "Continue - I'm over 18",
92 }
93 request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
94 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
95 try:
96 self.report_age_confirmation()
97 compat_urllib_request.urlopen(request).read()
98 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
99 raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
100
101 def _real_extract(self, url):
102 # Extract id and simplified title from URL
103 mobj = re.match(self._VALID_URL, url)
104 if mobj is None:
105 raise ExtractorError(u'Invalid URL: %s' % url)
106
107 video_id = mobj.group(1)
108
109 # Check if video comes from YouTube
110 mobj2 = re.match(r'^yt-(.*)$', video_id)
111 if mobj2 is not None:
112 return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
113
114 # Retrieve video webpage to extract further information
115 req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
116
117 # AnyClip videos require the flashversion cookie so that we get the link
118 # to the mp4 file
119 mobj_an = re.match(r'^an-(.*?)$', video_id)
120 if mobj_an:
121 req.headers['Cookie'] = 'flashVersion=0;'
122 webpage = self._download_webpage(req, video_id)
123
124 # Extract URL, uploader and title from webpage
125 self.report_extraction(video_id)
126 mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
127 if mobj is not None:
128 mediaURL = compat_urllib_parse.unquote(mobj.group(1))
129 video_ext = mediaURL[-3:]
130
131 # Extract gdaKey if available
132 mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
133 if mobj is None:
134 video_url = mediaURL
135 else:
136 gdaKey = mobj.group(1)
137 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
138 else:
139 mobj = re.search(r'<video src="([^"]+)"', webpage)
140 if mobj:
141 video_url = mobj.group(1)
142 video_ext = 'mp4'
143 else:
144 mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
145 if mobj is None:
146 raise ExtractorError(u'Unable to extract media URL')
147 vardict = compat_parse_qs(mobj.group(1))
148 if 'mediaData' not in vardict:
149 raise ExtractorError(u'Unable to extract media URL')
150 mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
151 if mobj is None:
152 raise ExtractorError(u'Unable to extract media URL')
153 mediaURL = mobj.group('mediaURL').replace('\\/', '/')
154 video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
155 video_ext = determine_ext(video_url)
156
157 video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
158 description = self._og_search_description(webpage)
159 video_uploader = self._html_search_regex(
160 r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
161 webpage, u'uploader nickname', fatal=False)
162
163 if re.search(r'"contentRating":"restricted"', webpage) is not None:
164 age_limit = 18
165 else:
166 age_limit = 0
167
168 return {
169 '_type': 'video',
170 'id': video_id,
171 'url': video_url,
172 'description': description,
173 'uploader': video_uploader,
174 'upload_date': None,
175 'title': video_title,
176 'ext': video_ext,
177 'age_limit': age_limit,
178 }