2 from __future__ import unicode_literals
6 from .common import InfoExtractor
10 #compat_urllib_request,
15 class PatreonHTMLParser(compat_html_parser.HTMLParser):
16 _PREFIX = 'http://www.patreon.com'
17 _ATTACH_TAGS = 5 * ['div']
19 'fancyboxhidden', 'box photo double', 'boxwrapper double',
20 'hiddendisplay shareinfo', 'attach'
22 _INFO_TAGS = 4 * ['div']
24 'fancyboxhidden', 'box photo double', 'boxwrapper double',
25 'hiddendisplay shareinfo'
28 def get_creation_info(self, html_data):
31 self.creation_info = {}
34 def handle_starttag(self, tag, attrs):
35 self.tag_stack.append(tag.lower())
36 self.attrs_stack.append(dict(attrs))
38 def handle_endtag(self, tag):
40 self.attrs_stack.pop()
42 def handle_data(self, data):
43 # Check first if this is a creation attachment
44 if self.tag_stack[-6:-1] == self._ATTACH_TAGS:
46 x.get('class', '').lower() for x in self.attrs_stack[-6:-1]
48 if attrs_classes == self._ATTACH_CLASSES:
49 if self.tag_stack[-1] == 'a':
50 url = self._PREFIX + self.attrs_stack[-1].get('href')
51 self.creation_info['url'] = url
53 self.creation_info['ext'] = data.rsplit('.')[-1]
54 # Next, check if this is within the div containing the creation info
55 if self.tag_stack[-5:-1] == self._INFO_TAGS:
57 x.get('class', '').lower() for x in self.attrs_stack[-5:-1]
59 if attrs_classes == self._INFO_CLASSES:
60 if self.attrs_stack[-1].get('class') == 'utitle':
61 self.creation_info['title'] = data.strip()
64 class PatreonIE(InfoExtractor):
66 _VALID_URL = r'https?://(?:www\.)?patreon\.com/creation\?hid=(.+)'
69 'url': 'http://www.patreon.com/creation?hid=743933',
70 'md5': 'e25505eec1053a6e6813b8ed369875cc',
75 'title': 'Episode 166: David Smalley of Dogma Debate',
76 'uploader': 'Cognitive Dissonance Podcast',
81 # Currently Patreon exposes download URL via hidden CSS, so login is not
82 # needed. Keeping this commented for when this inevitably changes.
85 (username, password) = self._get_login_info()
90 'redirectUrl': 'http://www.patreon.com/',
95 request = compat_urllib_request.Request(
96 'https://www.patreon.com/processLogin',
97 compat_urllib_parse.urlencode(login_form).encode('utf-8')
99 login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
101 if re.search(r'onLoginFailed', login_page):
102 raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
104 def _real_initialize(self):
108 def _real_extract(self, url):
109 mobj = re.match(self._VALID_URL, url)
110 video_id = mobj.group(1)
112 info_page = self._download_webpage(url, video_id)
114 ret = {'id': video_id}
116 ret['uploader'] = re.search(
117 r'<strong>(.+)</strong> is creating', info_page
119 except AttributeError:
122 parser = PatreonHTMLParser()
123 parser.get_creation_info(info_page)
124 if not parser.creation_info.get('url'):
125 raise ExtractorError('Unable to retrieve creation URL')
126 ret.update(parser.creation_info)