]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/metacafe.py
Use unescapeHTML for OpenGraph properties
[yt-dlp.git] / youtube_dl / extractor / metacafe.py
CommitLineData
38cbc40a
PH
1import re
2import socket
3
4from .common import InfoExtractor
5from ..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,
896d5b63 12 determine_ext,
38cbc40a
PH
13 ExtractorError,
14)
15
16class 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'
896d5b63 23 _TESTS = [{
83f6f68e
PH
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 }
896d5b63
PH
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 }
42 }]
83f6f68e 43
38cbc40a
PH
44
45 def report_disclaimer(self):
46 """Report disclaimer retrieval."""
47 self.to_screen(u'Retrieving disclaimer')
48
49 def _real_initialize(self):
50 # Retrieve disclaimer
51 request = compat_urllib_request.Request(self._DISCLAIMER)
52 try:
53 self.report_disclaimer()
54 compat_urllib_request.urlopen(request).read()
55 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
56 raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
57
58 # Confirm age
59 disclaimer_form = {
60 'filters': '0',
61 'submit': "Continue - I'm over 18",
62 }
63 request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
64 try:
65 self.report_age_confirmation()
66 compat_urllib_request.urlopen(request).read()
67 except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
68 raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
69
70 def _real_extract(self, url):
71 # Extract id and simplified title from URL
72 mobj = re.match(self._VALID_URL, url)
73 if mobj is None:
74 raise ExtractorError(u'Invalid URL: %s' % url)
75
76 video_id = mobj.group(1)
77
78 # Check if video comes from YouTube
79 mobj2 = re.match(r'^yt-(.*)$', video_id)
80 if mobj2 is not None:
81 return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
82
83 # Retrieve video webpage to extract further information
896d5b63
PH
84 req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
85 req.headers['Cookie'] = 'flashVersion=0;'
86 webpage = self._download_webpage(req, video_id)
38cbc40a
PH
87
88 # Extract URL, uploader and title from webpage
89 self.report_extraction(video_id)
90 mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
91 if mobj is not None:
92 mediaURL = compat_urllib_parse.unquote(mobj.group(1))
896d5b63 93 video_ext = mediaURL[-3:]
38cbc40a
PH
94
95 # Extract gdaKey if available
96 mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
97 if mobj is None:
98 video_url = mediaURL
99 else:
100 gdaKey = mobj.group(1)
101 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
102 else:
896d5b63
PH
103 mobj = re.search(r'<video src="([^"]+)"', webpage)
104 if mobj:
105 video_url = mobj.group(1)
106 video_ext = 'mp4'
107 else:
108 mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
109 if mobj is None:
110 raise ExtractorError(u'Unable to extract media URL')
111 vardict = compat_parse_qs(mobj.group(1))
112 if 'mediaData' not in vardict:
113 raise ExtractorError(u'Unable to extract media URL')
114 mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
115 if mobj is None:
116 raise ExtractorError(u'Unable to extract media URL')
117 mediaURL = mobj.group('mediaURL').replace('\\/', '/')
118 video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
119 video_ext = determine_ext(video_url)
38cbc40a 120
ec00e1d8 121 video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
896d5b63 122 video_uploader = self._html_search_regex(r'submitter=(.*?);|<p class="By">\s*By\s*<a[^>]*>(.*?)</a>', webpage, u'uploader nickname', fatal=False)
38cbc40a
PH
123
124 return [{
896d5b63
PH
125 'id': video_id,
126 'url': video_url,
127 'uploader': video_uploader,
38cbc40a
PH
128 'upload_date': None,
129 'title': video_title,
896d5b63 130 'ext': video_ext,
38cbc40a 131 }]