-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
152 lines (127 loc) 路 4.5 KB
/
app.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
"""
server.py
Simple Tornado API for GitHub contributions.
"""
import datetime
import json
import os
from urllib.parse import urlsplit
import tornado.autoreload
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
from tornado.options import define, options
from bson import json_util
from contributions import (get_contributions_daily, get_contributions_monthly,
get_contributions_today, get_contributions_weekly)
from dotenv import load_dotenv
from pymongo import MongoClient
define("port", default=8889, help="run on the given port", type=int)
load_dotenv()
MONGODB_URI = os.getenv('MONGODB_URI', 'mongodb://localhost:27017/gh_contribs')
INTERVALS = (
('today', get_contributions_today),
('daily', get_contributions_daily),
('weekly', get_contributions_weekly),
('monthly', get_contributions_monthly)
)
class BaseHandler(tornado.web.RequestHandler):
"""
Inherit from this class to enable CORS.
"""
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', '*')
class IndexHandler(BaseHandler):
"""
Serves the homepage for the project.
URL: /
"""
def get(self):
self.render("index.html", **{})
class StatsHandler(BaseHandler):
"""
Serves GitHub Contribution data via JSON API.
URL: /api/stats/<today|daily|weekly|monthly>/<username>/
"""
def get(self, interval, username):
try:
get_data_function = dict(INTERVALS)[interval]
self.write(get_data_function(username))
except KeyError:
self.write(
f"Could not find feed named '{interval}'.\
Please try 'today', 'daily', 'weekly', or 'monthly.'")
except:
self.write({'error': 'An error has occurred.'})
class ScrapeHandler(BaseHandler):
"""
Scrapes and saves the results to a MongoDB collection.
URL: /api/scrape/<username>/
"""
def _get_db_connection(self):
parsed = urlsplit(MONGODB_URI)
db_name = parsed.path[1:]
db = MongoClient(MONGODB_URI)[db_name]
if '@' in MONGODB_URI:
user, password = parsed.netloc.split('@')[0].split(':')
db.authenticate(user, password)
return db
def _fetch_contribs_for_user(self, username):
db = self._get_db_connection()
db_contribs = db.users.find_one({"username": username})
return_json = json.dumps(
db_contribs, indent=4, default=json_util.default)
return return_json
def get(self, username):
""" Fetch user contribs from MongoDB. """
return_json = self._fetch_contribs_for_user(username)
self.write(return_json)
self.set_header('Content-Type', 'application/json')
def post(self, username):
""" Initialize a new user. """
daily_contribs = get_contributions_daily(username)
weekly_contribs = get_contributions_weekly(username)
monthly_contribs = get_contributions_monthly(username)
now = datetime.datetime.utcnow()
db = self._get_db_connection()
db.users.update_one({'username': username}, {
'$setOnInsert': {
'insertion_date': now
},
'$set': {
'username': username,
'daily': daily_contribs['contributions'],
'weekly': weekly_contribs['contributions'],
'monthly': monthly_contribs['contributions'],
'last_updated': now
}
}, upsert=True)
return_json = self._fetch_contribs_for_user(username)
self.write(return_json)
self.set_header('Content-Type', 'application/json')
def run_server():
""" Start Tornado. """
tornado.options.parse_command_line()
app = tornado.wsgi.WSGIApplication([
(r"/$", IndexHandler),
(r"/api/scrape/(\w+)+/$", ScrapeHandler),
(r"/api/stats/([daily|weekly|monthly|today]+)/(\w+)+/$", StatsHandler),
], **{
'debug': True,
})
http_server = tornado.httpserver.HTTPServer(
tornado.wsgi.WSGIContainer(app)
)
http_server.listen(options.port)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)
try:
io_loop.start()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
print(f"[LISTENING] http://localhost:{options.port}")
run_server()