]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/patreon.py
Tidy the changelog.
[youtubedl] / 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=(.+)'
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 mobj = re.match(self._VALID_URL, url)
69 video_id = mobj.group(1)
70
71 webpage = self._download_webpage(url, video_id)
72 title = self._og_search_title(webpage).strip()
73
74 attach_fn = self._html_search_regex(
75 r'<div class="attach"><a target="_blank" href="([^"]+)">',
76 webpage, 'attachment URL', default=None)
77 if attach_fn is not None:
78 video_url = 'http://www.patreon.com' + attach_fn
79 thumbnail = self._og_search_thumbnail(webpage)
80 uploader = self._html_search_regex(
81 r'<strong>(.*?)</strong> is creating', webpage, 'uploader')
82 else:
83 playlist_js = self._search_regex(
84 r'(?s)new\s+jPlayerPlaylist\(\s*\{\s*[^}]*},\s*(\[.*?,?\s*\])',
85 webpage, 'playlist JSON')
86 playlist_json = js_to_json(playlist_js)
87 playlist = json.loads(playlist_json)
88 data = playlist[0]
89 video_url = self._proto_relative_url(data['mp3'])
90 thumbnail = self._proto_relative_url(data.get('cover'))
91 uploader = data.get('artist')
92
93 return {
94 'id': video_id,
95 'url': video_url,
96 'ext': 'mp3',
97 'title': title,
98 'uploader': uploader,
99 'thumbnail': thumbnail,
100 }