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