fix exception
[youtube-dl] / youtube_dl / extractor / patreon.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     compat_html_parser,
10     #compat_urllib_request,
11     #compat_urllib_parse,
12 )
13
14
15 class PatreonHTMLParser(compat_html_parser.HTMLParser):
16     _PREFIX = 'http://www.patreon.com'
17     _ATTACH_TAGS = 5 * ['div']
18     _ATTACH_CLASSES = [
19         'fancyboxhidden', 'box photo double', 'boxwrapper double',
20         'hiddendisplay shareinfo', 'attach'
21     ]
22     _INFO_TAGS = 4 * ['div']
23     _INFO_CLASSES = [
24         'fancyboxhidden', 'box photo double', 'boxwrapper double',
25         'hiddendisplay shareinfo'
26     ]
27
28     def get_creation_info(self, html_data):
29         self.tag_stack = []
30         self.attrs_stack = []
31         self.creation_info = {}
32         self.feed(html_data)
33
34     def handle_starttag(self, tag, attrs):
35         self.tag_stack.append(tag.lower())
36         self.attrs_stack.append(dict(attrs))
37
38     def handle_endtag(self, tag):
39         self.tag_stack.pop()
40         self.attrs_stack.pop()
41
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:
45             attrs_classes = [
46                 x.get('class', '').lower() for x in self.attrs_stack[-6:-1]
47             ]
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
52                     if '.' in data:
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:
56             attrs_classes = [
57                 x.get('class', '').lower() for x in self.attrs_stack[-5:-1]
58             ]
59             if attrs_classes == self._INFO_CLASSES:
60                 if self.attrs_stack[-1].get('class') == 'utitle':
61                     self.creation_info['title'] = data.strip()
62
63
64 class PatreonIE(InfoExtractor):
65     IE_NAME = 'patreon'
66     _VALID_URL = r'https?://(?:www\.)?patreon\.com/creation\?hid=(.+)'
67     _TESTS = [
68         {
69             'url': 'http://www.patreon.com/creation?hid=743933',
70             'md5': 'e25505eec1053a6e6813b8ed369875cc',
71             'name': 'Patreon',
72             'info_dict': {
73                 'id': '743933',
74                 'ext': 'mp3',
75                 'title': 'Episode 166: David Smalley of Dogma Debate',
76                 'uploader': 'Cognitive Dissonance Podcast',
77             },
78         },
79     ]
80
81     # Currently Patreon exposes download URL via hidden CSS, so login is not
82     # needed. Keeping this commented for when this inevitably changes.
83     '''
84     def _login(self):
85         (username, password) = self._get_login_info()
86         if username is None:
87             return
88
89         login_form = {
90             'redirectUrl': 'http://www.patreon.com/',
91             'email': username,
92             'password': password,
93         }
94
95         request = compat_urllib_request.Request(
96             'https://www.patreon.com/processLogin',
97             compat_urllib_parse.urlencode(login_form).encode('utf-8')
98         )
99         login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
100
101         if re.search(r'onLoginFailed', login_page):
102             raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
103
104     def _real_initialize(self):
105         self._login()
106     '''
107
108     def _real_extract(self, url):
109         mobj = re.match(self._VALID_URL, url)
110         video_id = mobj.group(1)
111
112         info_page = self._download_webpage(url, video_id)
113
114         ret = {'id': video_id}
115         try:
116             ret['uploader'] = re.search(
117                 r'<strong>(.+)</strong> is creating', info_page
118             ).group(1)
119         except AttributeError:
120             pass
121
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)
127         return ret