forked from ShengRang/njoj-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
56 lines (47 loc) · 1.6 KB
/
webhook.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
#!/usr/bin/env python
# encoding: utf-8
import os
import hmac
from hashlib import sha1
import tornado.web
import tornado.options
import tornado.ioloop
import tornado.httpserver
import tornado.autoreload
from tornado.options import define, options
define('port', default=8080, help='tornado webhook port', type=int)
define('secret', default='njoj-blog', help='hook\'s secret')
def hook():
os.system('git pull')
os.system('hexo g')
class WebHookHandler(tornado.web.RequestHandler):
def post(self):
hub_signature = self.request.headers.get('X-Hub-Signature')
digestmod, hexdigest = hub_signature.split('=')
if digestmod != 'sha1':
raise tornado.web.HTTPError(404)
github_event = self.request.headers.get('X-Github-Event')
delivery_id = self.request.headers.get('X-Github-Delivery')
payload = self.request.body
if hmac.new(options.secret, payload, sha1).hexdigest() != hexdigest:
raise tornado.web.HTTPError(403)
else:
hook()
def get(self):
self.write('hello')
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/refresh', WebHookHandler),
]
settings = dict(
debug = True
)
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == '__main__':
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
instance = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(instance)
instance.start()