]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/metacafe.py
Imported Upstream version 2013.08.02
[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 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 u"url": u"http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/",
37 u"file": u"an-dVVXnuY7Jh77J.mp4",
38 u"info_dict": {
39 u"title": u"The Andromeda Strain (1971): Stop the Bomb Part 3",
40 u"uploader": u"anyclip",
41 u"description": u"md5:38c711dd98f5bb87acf973d573442e67"
42 }
43 }]
44
45
46 def report_disclaimer(self):
47 """Report disclaimer retrieval."""
48 self.to_screen(u'Retrieving disclaimer')
49
50 def _real_initialize(self):
51 # Retrieve disclaimer
52 request = compat_urllib_request.Request(self._DISCLAIMER)
53 try:
54 self.report_disclaimer()
55 compat_urllib_request.urlopen(request).read()
56 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
57 raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
58
59 # Confirm age
60 disclaimer_form = {
61 'filters': '0',
62 'submit': "Continue - I'm over 18",
63 }
64 request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
65 try:
66 self.report_age_confirmation()
67 compat_urllib_request.urlopen(request).read()
68 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
69 raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
70
71 def _real_extract(self, url):
72 # Extract id and simplified title from URL
73 mobj = re.match(self._VALID_URL, url)
74 if mobj is None:
75 raise ExtractorError(u'Invalid URL: %s' % url)
76
77 video_id = mobj.group(1)
78
79 # Check if video comes from YouTube
80 mobj2 = re.match(r'^yt-(.*)$', video_id)
81 if mobj2 is not None:
82 return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
83
84 # Retrieve video webpage to extract further information
85 req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
86 req.headers['Cookie'] = 'flashVersion=0;'
87 webpage = self._download_webpage(req, video_id)
88
89 # Extract URL, uploader and title from webpage
90 self.report_extraction(video_id)
91 mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
92 if mobj is not None:
93 mediaURL = compat_urllib_parse.unquote(mobj.group(1))
94 video_ext = mediaURL[-3:]
95
96 # Extract gdaKey if available
97 mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
98 if mobj is None:
99 video_url = mediaURL
100 else:
101 gdaKey = mobj.group(1)
102 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
103 else:
104 mobj = re.search(r'<video src="([^"]+)"', webpage)
105 if mobj:
106 video_url = mobj.group(1)
107 video_ext = 'mp4'
108 else:
109 mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
110 if mobj is None:
111 raise ExtractorError(u'Unable to extract media URL')
112 vardict = compat_parse_qs(mobj.group(1))
113 if 'mediaData' not in vardict:
114 raise ExtractorError(u'Unable to extract media URL')
115 mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
116 if mobj is None:
117 raise ExtractorError(u'Unable to extract media URL')
118 mediaURL = mobj.group('mediaURL').replace('\\/', '/')
119 video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
120 video_ext = determine_ext(video_url)
121
122 video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
123 description = self._og_search_description(webpage)
124 video_uploader = self._html_search_regex(
125 r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("channel","([^"]+)"\);',
126 webpage, u'uploader nickname', fatal=False)
127
128 return {
129 '_type': 'video',
130 'id': video_id,
131 'url': video_url,
132 'description': description,
133 'uploader': video_uploader,
134 'upload_date': None,
135 'title': video_title,
136 'ext': video_ext,
137 }