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