forked from youtify/youtify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flattr.py
158 lines (132 loc) · 5.95 KB
/
flattr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import logging
import urllib
import base64
from google.appengine.api import urlfetch
import webapp2
from google.appengine.ext.webapp import util
import json as simplejson
from model import get_current_youtify_user_model
from activities import create_flattr_activity
try:
import config
except ImportError:
import config_template as config
VALIDATE_CERTIFICATE = True
FLATTR_SCOPE = 'flattr thing'
class ClickHandler(webapp2.RequestHandler):
"""Flattrs a specified thing"""
def post(self):
thing_id = self.request.get('thing_id')
video_title = self.request.get('videoTitle')
url = 'https://api.flattr.com/rest/v2/things/' + thing_id + '/flattr'
user = get_current_youtify_user_model()
headers = {
'Authorization': 'Bearer %s' % user.flattr_access_token
}
response = urlfetch.fetch(url=url, method=urlfetch.POST, headers=headers, validate_certificate=VALIDATE_CERTIFICATE)
json = simplejson.loads(response.content)
if json.get('message') == 'ok' and 'thing' in json:
thing_id = str(json['thing'].get('id'))
create_flattr_activity(user, thing_id, video_title)
user.nr_of_flattrs += 1
user.save()
else:
logging.error('Error creating flattr click. Response: %s' % response.content)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(response.content)
class AutoSubmitHandler(webapp2.RequestHandler):
"""Flattrs a specified URL even though it may not be on flattr yet"""
def post(self):
url_to_submit = self.request.get('url')
video_title = self.request.get('videoTitle')
url = 'https://api.flattr.com/rest/v2/flattr'
user = get_current_youtify_user_model()
headers = {
'Authorization': 'Bearer %s' % user.flattr_access_token,
'Content-Type': 'application/json',
}
data = simplejson.dumps({
'url': url_to_submit,
})
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=headers, validate_certificate=VALIDATE_CERTIFICATE)
json = simplejson.loads(response.content)
if json.get('message') == 'ok' and 'thing' in json:
thing_id = str(json['thing'].get('id'))
create_flattr_activity(user, thing_id, video_title)
user.nr_of_flattrs += 1
user.save()
elif json.get('error') == 'flattr_once':
pass
else:
logging.error('Error creating flattr click. Response: %s' % response.content)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(response.content)
class DisconnectHandler(webapp2.RequestHandler):
"""Remove the current users access token"""
def get(self):
redirect_uri = self.request.get('redirect_uri', '/')
user = get_current_youtify_user_model()
user.flattr_access_token = None
user.flattr_user_name = None
user.save()
self.redirect(redirect_uri)
class ConnectHandler(webapp2.RequestHandler):
"""Initiate the OAuth dance"""
def get(self):
redirect_uri = self.request.get('redirect_uri')
if redirect_uri and redirect_uri != 'deleted':
self.response.headers['Set-Cookie'] = 'redirect_uri=' + redirect_uri
url = 'https://flattr.com/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s&scope=%s' % (config.CLIENT_ID, urllib.quote(config.REDIRECT_URL), urllib.quote(FLATTR_SCOPE))
self.redirect(url)
def update_fattr_user_info(user):
"""Note, this function does not save the user model"""
url = 'https://api.flattr.com/rest/v2/user'
headers = {
'Authorization': 'Bearer %s' % user.flattr_access_token,
}
response = urlfetch.fetch(url=url, method=urlfetch.GET, headers=headers, validate_certificate=VALIDATE_CERTIFICATE)
response = simplejson.loads(response.content)
if 'error_description' in response:
raise Exception('Failed to update flattr user info for user %s - %s' % (user.google_user2.email(), response['error_description']))
else:
user.flattr_user_name = response['username']
class BackHandler(webapp2.RequestHandler):
"""Retrieve the access token"""
def get(self):
code = self.request.get('code')
url = 'https://flattr.com/oauth/token'
headers = {
'Authorization': 'Basic %s' % base64.b64encode(config.CLIENT_ID + ":" + config.CLIENT_SECRET),
'Content-Type': 'application/json',
}
data = simplejson.dumps({
'code': code,
'redirect_uri': config.REDIRECT_URL,
'grant_type': 'authorization_code',
})
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=headers, validate_certificate=VALIDATE_CERTIFICATE)
response = simplejson.loads(response.content)
if 'access_token' in response:
user = get_current_youtify_user_model()
user.flattr_access_token = response['access_token']
user.flattr_scope = FLATTR_SCOPE
update_fattr_user_info(user)
user.save()
redirect_uri = self.request.cookies.get('redirect_uri')
if redirect_uri:
self.response.headers['Set-Cookie'] = 'redirect_uri=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT'
self.redirect(redirect_uri)
else:
self.redirect('/')
else:
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Flattr connection failed')
self.response.out.write('\n\n')
self.response.out.write(str(response))
app = webapp2.WSGIApplication([
('/flattrdisconnect', DisconnectHandler),
('/flattrconnect', ConnectHandler),
('/flattrback', BackHandler),
('/flattrclick', ClickHandler),
('/flattrautosubmit', AutoSubmitHandler),
], debug=True)