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', 'boxwrapper',
20 'hiddendisplay shareinfo', 'attach'
22 _INFO_TAGS = 4 * ['div']
24 'fancyboxhidden', 'box photo', 'boxwrapper',
25 'hiddendisplay shareinfo'
28 def _match(self, attrs_classes, desired):
29 if attrs_classes == desired:
31 elif len(attrs_classes) == len(desired):
34 for x, y in zip(attrs_classes, desired)
38 def get_creation_info(self, html_data):
41 self.creation_info = {}
44 def handle_starttag(self, tag, attrs):
45 self.tag_stack.append(tag.lower())
46 self.attrs_stack.append(dict(attrs))
48 def handle_endtag(self, tag):
50 self.attrs_stack.pop()
52 def handle_data(self, data):
53 # Check first if this is a creation attachment
54 if self.tag_stack[-6:-1] == self._ATTACH_TAGS:
56 x.get('class', '').lower() for x in self.attrs_stack[-6:-1]
58 if self._match(attrs_classes, self._ATTACH_CLASSES):
59 if self.tag_stack[-1] == 'a':
60 url = self._PREFIX + self.attrs_stack[-1].get('href')
61 self.creation_info['url'] = url
63 self.creation_info['ext'] = data.rsplit('.')[-1]
64 # Next, check if this is within the div containing the creation info
65 if self.tag_stack[-5:-1] == self._INFO_TAGS:
67 x.get('class', '').lower() for x in self.attrs_stack[-5:-1]
69 if self._match(attrs_classes, self._INFO_CLASSES):
70 if self.attrs_stack[-1].get('class') == 'utitle':
71 self.creation_info['title'] = data.strip()
74 class PatreonIE(InfoExtractor):
76 _VALID_URL = r'https?://(?:www\.)?patreon\.com/creation\?hid=(.+)'
78 # CSS names with "double" in the name, i.e. "boxwrapper double"
80 'url': 'http://www.patreon.com/creation?hid=743933',
81 'md5': 'e25505eec1053a6e6813b8ed369875cc',
85 'title': 'Episode 166: David Smalley of Dogma Debate',
86 'uploader': 'Cognitive Dissonance Podcast',
90 'url': 'http://www.patreon.com/creation?hid=754133',
91 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
95 'title': 'CD 167 Extra',
96 'uploader': 'Cognitive Dissonance Podcast',
101 # Currently Patreon exposes download URL via hidden CSS, so login is not
102 # needed. Keeping this commented for when this inevitably changes.
105 (username, password) = self._get_login_info()
110 'redirectUrl': 'http://www.patreon.com/',
112 'password': password,
115 request = compat_urllib_request.Request(
116 'https://www.patreon.com/processLogin',
117 compat_urllib_parse.urlencode(login_form).encode('utf-8')
119 login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
121 if re.search(r'onLoginFailed', login_page):
122 raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
124 def _real_initialize(self):
128 def _real_extract(self, url):
129 mobj = re.match(self._VALID_URL, url)
130 video_id = mobj.group(1)
132 info_page = self._download_webpage(url, video_id)
134 ret = {'id': video_id}
136 ret['uploader'] = re.search(
137 r'<strong>(.+)</strong> is creating', info_page
139 except AttributeError:
142 parser = PatreonHTMLParser()
143 parser.get_creation_info(info_page)
144 if not parser.creation_info.get('url'):
145 raise ExtractorError('Unable to retrieve creation URL')
146 ret.update(parser.creation_info)