[patreon] Modernize
[youtube-dl] / youtube_dl / extractor / patreon.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     js_to_json,
10 )
11
12
13 class PatreonIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?patreon\.com/creation\?hid=(?P<id>[^&#]+)'
15     _TESTS = [
16         {
17             'url': 'http://www.patreon.com/creation?hid=743933',
18             'md5': 'e25505eec1053a6e6813b8ed369875cc',
19             'info_dict': {
20                 'id': '743933',
21                 'ext': 'mp3',
22                 'title': 'Episode 166: David Smalley of Dogma Debate',
23                 'uploader': 'Cognitive Dissonance Podcast',
24                 'thumbnail': 're:^https?://.*$',
25             },
26         },
27         {
28             'url': 'http://www.patreon.com/creation?hid=754133',
29             'md5': '3eb09345bf44bf60451b8b0b81759d0a',
30             'info_dict': {
31                 'id': '754133',
32                 'ext': 'mp3',
33                 'title': 'CD 167 Extra',
34                 'uploader': 'Cognitive Dissonance Podcast',
35                 'thumbnail': 're:^https?://.*$',
36             },
37         },
38     ]
39
40     # Currently Patreon exposes download URL via hidden CSS, so login is not
41     # needed. Keeping this commented for when this inevitably changes.
42     '''
43     def _login(self):
44         (username, password) = self._get_login_info()
45         if username is None:
46             return
47
48         login_form = {
49             'redirectUrl': 'http://www.patreon.com/',
50             'email': username,
51             'password': password,
52         }
53
54         request = compat_urllib_request.Request(
55             'https://www.patreon.com/processLogin',
56             compat_urllib_parse.urlencode(login_form).encode('utf-8')
57         )
58         login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
59
60         if re.search(r'onLoginFailed', login_page):
61             raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
62
63     def _real_initialize(self):
64         self._login()
65     '''
66
67     def _real_extract(self, url):
68         video_id = self._match_id(url)
69         webpage = self._download_webpage(url, video_id)
70         title = self._og_search_title(webpage).strip()
71
72         attach_fn = self._html_search_regex(
73             r'<div class="attach"><a target="_blank" href="([^"]+)">',
74             webpage, 'attachment URL', default=None)
75         if attach_fn is not None:
76             video_url = 'http://www.patreon.com' + attach_fn
77             thumbnail = self._og_search_thumbnail(webpage)
78             uploader = self._html_search_regex(
79                 r'<strong>(.*?)</strong> is creating', webpage, 'uploader')
80         else:
81             playlist = self._parse_json(self._search_regex(
82                 r'(?s)new\s+jPlayerPlaylist\(\s*\{\s*[^}]*},\s*(\[.*?,?\s*\])',
83                 webpage, 'playlist JSON'),
84                 video_id, transform_source=js_to_json)
85             data = playlist[0]
86             video_url = self._proto_relative_url(data['mp3'])
87             thumbnail = self._proto_relative_url(data.get('cover'))
88             uploader = data.get('artist')
89
90         return {
91             'id': video_id,
92             'url': video_url,
93             'ext': 'mp3',
94             'title': title,
95             'uploader': uploader,
96             'thumbnail': thumbnail,
97         }