]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/metacafe.py
4c3f81b989ed3ff51dda83909d7da88a2a2f6eb8
[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
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 _TEST = {
24 u"add_ie": ["Youtube"],
25 u"url": u"http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/",
26 u"file": u"_aUehQsCQtM.flv",
27 u"info_dict": {
28 u"upload_date": u"20090102",
29 u"title": u"The Electric Company | \"Short I\" | PBS KIDS GO!",
30 u"description": u"md5:2439a8ef6d5a70e380c22f5ad323e5a8",
31 u"uploader": u"PBS",
32 u"uploader_id": u"PBS"
33 }
34 }
35
36
37 def report_disclaimer(self):
38 """Report disclaimer retrieval."""
39 self.to_screen(u'Retrieving disclaimer')
40
41 def _real_initialize(self):
42 # Retrieve disclaimer
43 request = compat_urllib_request.Request(self._DISCLAIMER)
44 try:
45 self.report_disclaimer()
46 compat_urllib_request.urlopen(request).read()
47 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
48 raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
49
50 # Confirm age
51 disclaimer_form = {
52 'filters': '0',
53 'submit': "Continue - I'm over 18",
54 }
55 request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
56 try:
57 self.report_age_confirmation()
58 compat_urllib_request.urlopen(request).read()
59 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
60 raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
61
62 def _real_extract(self, url):
63 # Extract id and simplified title from URL
64 mobj = re.match(self._VALID_URL, url)
65 if mobj is None:
66 raise ExtractorError(u'Invalid URL: %s' % url)
67
68 video_id = mobj.group(1)
69
70 # Check if video comes from YouTube
71 mobj2 = re.match(r'^yt-(.*)$', video_id)
72 if mobj2 is not None:
73 return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
74
75 # Retrieve video webpage to extract further information
76 webpage = self._download_webpage('http://www.metacafe.com/watch/%s/' % video_id, video_id)
77
78 # Extract URL, uploader and title from webpage
79 self.report_extraction(video_id)
80 mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
81 if mobj is not None:
82 mediaURL = compat_urllib_parse.unquote(mobj.group(1))
83 video_extension = mediaURL[-3:]
84
85 # Extract gdaKey if available
86 mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
87 if mobj is None:
88 video_url = mediaURL
89 else:
90 gdaKey = mobj.group(1)
91 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
92 else:
93 mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
94 if mobj is None:
95 raise ExtractorError(u'Unable to extract media URL')
96 vardict = compat_parse_qs(mobj.group(1))
97 if 'mediaData' not in vardict:
98 raise ExtractorError(u'Unable to extract media URL')
99 mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
100 if mobj is None:
101 raise ExtractorError(u'Unable to extract media URL')
102 mediaURL = mobj.group('mediaURL').replace('\\/', '/')
103 video_extension = mediaURL[-3:]
104 video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
105
106 mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
107 if mobj is None:
108 raise ExtractorError(u'Unable to extract title')
109 video_title = mobj.group(1).decode('utf-8')
110
111 mobj = re.search(r'submitter=(.*?);', webpage)
112 if mobj is None:
113 raise ExtractorError(u'Unable to extract uploader nickname')
114 video_uploader = mobj.group(1)
115
116 return [{
117 'id': video_id.decode('utf-8'),
118 'url': video_url.decode('utf-8'),
119 'uploader': video_uploader.decode('utf-8'),
120 'upload_date': None,
121 'title': video_title,
122 'ext': video_extension.decode('utf-8'),
123 }]