This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
github.py
65 lines (56 loc) · 2.12 KB
/
github.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
import config
import hashlib
import urllib.parse
import tornado.auth
import tornado.escape
import tornado.gen
import tornado.httpclient
import tornado.httputil
class GithubMixin(tornado.auth.OAuth2Mixin):
_OAUTH_AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'
_OAUTH_ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
def authorize_redirect(self, **kwargs):
kwargs['client_id'] = config.web.github_client_id
super(GithubMixin, self).authorize_redirect(**kwargs)
@tornado.gen.coroutine
def get_authenticated_user(self, redirect_uri, code):
url = self._oauth_request_token_url(
redirect_uri=redirect_uri,
code=code,
client_id=config.web.github_client_id,
client_secret=config.web.github_client_secret,
)
response = yield self._http(url)
data = tornado.escape.json_decode(response.body)
access_token = data['access_token']
user = yield self.github_request('/user', access_token)
user['access_token'] = access_token
return user
@tornado.gen.coroutine
def github_request(self, path, access_token=None, method='GET', headers={}, body=None, **args):
args['access_token'] = access_token
url = tornado.httputil.url_concat('https://api.github.com' + path, args)
if body is not None:
body = tornado.escape.json_encode(body)
response = yield self._http(url, method=method, headers=headers, body=body)
return tornado.escape.json_decode(response.body)
@staticmethod
@tornado.gen.coroutine
def _http(*args, **kwargs):
headers = {
'Accept': 'application/json',
'User-Agent': 'raylu', # http://developer.github.com/v3/#user-agent-required
}
headers.update(kwargs.get('headers', {}))
kwargs['headers'] = headers
response = yield tornado.httpclient.AsyncHTTPClient().fetch(*args, **kwargs)
if response.error:
raise Exception('%s\n%s' % (response.error, response.body))
return response
@staticmethod
def avatar_url(username, email):
default = 'https://identicons.github.com/%s.png' % username
if not email:
return default
md5 = hashlib.md5(email.encode('utf-8'))
return 'http://www.gravatar.com/avatar/%s?s=200&d=%s' % (md5.hexdigest(), urllib.parse.quote(default))