2018-04-08 23:48:07 -05:00
|
|
|
#!/usr/bin/env python3
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-10 14:32:36 -05:00
|
|
|
Handle API requests to the database
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-17 15:40:51 -05:00
|
|
|
|
2018-04-08 23:48:07 -05:00
|
|
|
import json
|
2018-04-25 13:51:11 -05:00
|
|
|
import random
|
|
|
|
import string
|
2018-04-08 23:48:07 -05:00
|
|
|
import sqlite3
|
2018-04-17 13:43:45 -05:00
|
|
|
from os import path
|
2018-04-08 23:48:07 -05:00
|
|
|
|
2018-04-19 14:08:15 -05:00
|
|
|
DATABASE = '/usr/local/www/mocha-server/db.sqlite3'
|
2018-04-17 13:43:45 -05:00
|
|
|
if not path.exists(DATABASE):
|
2018-04-19 14:08:15 -05:00
|
|
|
DATABASE = 'db.sqlite3'
|
2018-04-17 10:17:17 -05:00
|
|
|
|
2018-04-10 15:24:15 -05:00
|
|
|
# TODO: Add ability to store and retrieve avatars (as image files?)
|
2018-04-08 23:48:07 -05:00
|
|
|
|
2018-04-25 13:51:11 -05:00
|
|
|
# TODO: Add authentication generation
|
|
|
|
# TODO: Allow updating data with authorization
|
|
|
|
|
|
|
|
|
|
|
|
def make_new_account(username):
|
|
|
|
'''
|
|
|
|
Returns a new API key if the provided username is unique
|
|
|
|
'''
|
|
|
|
conn = sqlite3.connect(DATABASE)
|
|
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
output = cursor.execute(
|
|
|
|
'SELECT user_id FROM users ORDER BY user_id DESC').fetchall()
|
|
|
|
existing_ids = [str(dict(row)['user_id']) for row in output]
|
|
|
|
|
|
|
|
user_id = str(random.randint(1, 1000000))
|
|
|
|
while user_id in existing_ids:
|
|
|
|
user_id = str(random.randint(1, 1000000))
|
|
|
|
|
|
|
|
score = 0
|
|
|
|
|
|
|
|
token = ''.join([
|
|
|
|
random.choice(string.ascii_letters + string.digits + '+=')
|
|
|
|
for _ in range(30)
|
|
|
|
])
|
|
|
|
|
|
|
|
cursor.execute(
|
2018-04-26 12:53:33 -05:00
|
|
|
'INSERT INTO users (user_id, username, score, token) VALUES (?, \'?\', ?, ?)',
|
2018-04-25 13:51:11 -05:00
|
|
|
(user_id, username, score, token))
|
|
|
|
|
|
|
|
return json.dumps([{'key': token}])
|
|
|
|
|
2018-04-19 13:46:51 -05:00
|
|
|
|
2018-04-17 15:14:52 -05:00
|
|
|
def fetch_users(user_ids):
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-17 15:40:51 -05:00
|
|
|
Gets a list of users searching by name.
|
|
|
|
This can also easily be done by user_id.
|
|
|
|
'''
|
2018-04-17 14:38:59 -05:00
|
|
|
conn = sqlite3.connect(DATABASE)
|
2018-04-17 14:41:13 -05:00
|
|
|
conn.row_factory = sqlite3.Row
|
2018-04-17 14:38:59 -05:00
|
|
|
cursor = conn.cursor()
|
2018-04-17 12:31:13 -05:00
|
|
|
output = []
|
2018-04-17 15:14:52 -05:00
|
|
|
for user_id in user_ids:
|
2018-04-25 13:51:11 -05:00
|
|
|
cursor.execute(
|
|
|
|
'SELECT user_id, username, score FROM users WHERE user_id=(?)',
|
|
|
|
(user_id, ))
|
2018-04-17 12:31:13 -05:00
|
|
|
output += cursor.fetchall()
|
|
|
|
|
|
|
|
output = json.dumps([dict(row) for row in output])
|
|
|
|
conn.commit()
|
2018-04-17 14:38:59 -05:00
|
|
|
conn.close()
|
2018-04-17 12:31:13 -05:00
|
|
|
|
|
|
|
if output == '[]':
|
|
|
|
output = None
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-04-17 10:17:17 -05:00
|
|
|
# add new parameters as needed
|
2018-04-17 14:59:35 -05:00
|
|
|
def update_row(_user_id, _updated_username):
|
|
|
|
'''
|
|
|
|
WIP.
|
|
|
|
Will add a new user.
|
|
|
|
'''
|
2018-04-17 10:17:17 -05:00
|
|
|
print()
|
|
|
|
|
2018-04-17 12:31:13 -05:00
|
|
|
|
2018-04-17 10:17:17 -05:00
|
|
|
def insert_row(user_id, username):
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-17 14:50:46 -05:00
|
|
|
Inserts a row for a NEW user with given parameters
|
2018-04-17 15:40:51 -05:00
|
|
|
This may work better with AUTOINCREMENT to avoid arbitrary ids and
|
|
|
|
duplicates.
|
|
|
|
|
|
|
|
Alternatively, use randomized unique IDs.
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-17 10:17:17 -05:00
|
|
|
|
2018-04-17 14:38:59 -05:00
|
|
|
conn = sqlite3.connect(DATABASE)
|
2018-04-17 14:41:13 -05:00
|
|
|
conn.row_factory = sqlite3.Row
|
2018-04-17 14:38:59 -05:00
|
|
|
cursor = conn.cursor()
|
2018-04-24 14:18:42 -05:00
|
|
|
cursor.execute('INSERT INTO users VALUES (?,?)', (user_id, username))
|
2018-04-17 10:17:17 -05:00
|
|
|
conn.commit()
|
2018-04-17 14:38:59 -05:00
|
|
|
conn.close()
|
2018-04-17 10:17:17 -05:00
|
|
|
|
2018-04-10 15:24:15 -05:00
|
|
|
|
|
|
|
def fetch_user(user_id):
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-10 14:32:36 -05:00
|
|
|
Returns a JSON object containing the requested user
|
2018-04-17 14:59:35 -05:00
|
|
|
Also can return a list of all users if user_id == '*'
|
|
|
|
'''
|
2018-04-17 14:38:59 -05:00
|
|
|
conn = sqlite3.connect(DATABASE)
|
2018-04-17 14:41:13 -05:00
|
|
|
conn.row_factory = sqlite3.Row
|
2018-04-17 14:38:59 -05:00
|
|
|
cursor = conn.cursor()
|
2018-04-17 14:50:46 -05:00
|
|
|
if user_id != '*': # must use (?), (item,) format
|
2018-04-19 14:12:51 -05:00
|
|
|
cursor.execute(
|
2018-04-25 13:51:11 -05:00
|
|
|
'SELECT user_id, username, score FROM users WHERE user_id=(?) ORDER BY score DESC',
|
2018-04-19 14:12:51 -05:00
|
|
|
(user_id, ))
|
2018-04-08 23:48:07 -05:00
|
|
|
else:
|
2018-04-25 13:51:11 -05:00
|
|
|
cursor.execute(
|
|
|
|
'SELECT user_id, username, score FROM users ORDER BY score DESC')
|
2018-04-08 23:48:07 -05:00
|
|
|
|
2018-04-10 14:26:40 -05:00
|
|
|
output = cursor.fetchall()
|
2018-04-08 23:48:07 -05:00
|
|
|
output = json.dumps([dict(row) for row in output])
|
2018-04-17 12:31:13 -05:00
|
|
|
conn.commit()
|
2018-04-17 14:38:59 -05:00
|
|
|
conn.close()
|
2018-04-08 23:48:07 -05:00
|
|
|
|
2018-04-10 15:29:00 -05:00
|
|
|
if output == '[]':
|
|
|
|
output = None
|
|
|
|
|
2018-04-08 23:48:07 -05:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-04-17 14:59:35 -05:00
|
|
|
def fetch_top_n(num):
|
|
|
|
'''
|
|
|
|
Retrieves the top n users by score.
|
|
|
|
'''
|
2018-04-19 13:46:51 -05:00
|
|
|
conn = sqlite3.connect(DATABASE)
|
|
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
cursor = conn.cursor()
|
2018-04-25 13:51:11 -05:00
|
|
|
cursor.execute(
|
|
|
|
'SELECT user_id, username, score FROM users ORDER BY score DESC')
|
2018-04-19 13:46:51 -05:00
|
|
|
output = cursor.fetchall()
|
|
|
|
|
2018-04-19 13:48:02 -05:00
|
|
|
num = int(num)
|
2018-04-19 13:46:51 -05:00
|
|
|
output = json.dumps([dict(row) for row in output][:num])
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
if output == '[]':
|
|
|
|
output = None
|
|
|
|
|
2018-04-17 14:24:04 -05:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2018-04-26 11:52:09 -05:00
|
|
|
def update(user_id, score):
|
|
|
|
conn = sqlite3.connect(DATABASE)
|
|
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute('UPDATE users SET score = (?) WHERE user_id = (?)',
|
|
|
|
(score, user_id))
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
2018-04-26 12:03:22 -05:00
|
|
|
return '[]'
|
2018-04-26 11:52:09 -05:00
|
|
|
|
|
|
|
|
2018-04-08 23:48:07 -05:00
|
|
|
def process_request(uri):
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-10 14:32:36 -05:00
|
|
|
Handles the API endpoint.
|
|
|
|
Currently supports:
|
2018-04-26 11:52:09 -05:00
|
|
|
- /mocha/users/{user_id} Returns JSON of the specified user
|
|
|
|
- /mocha/users/* Returns JSON list of all users
|
|
|
|
- /mocha/top/n Returns JSON list of top n users by score
|
|
|
|
- /mocha/register/{username} Returns JSON of token for new user
|
|
|
|
- /mocha/update/{user_id}/{score}
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-08 23:48:07 -05:00
|
|
|
parts = uri.split('/')[1:]
|
|
|
|
assert parts[0] == 'mocha'
|
|
|
|
|
2018-04-10 15:29:00 -05:00
|
|
|
output = None
|
2018-04-08 23:48:07 -05:00
|
|
|
|
2018-04-17 15:41:09 -05:00
|
|
|
if parts[1] == 'users' and len(parts) > 2:
|
2018-04-17 15:14:52 -05:00
|
|
|
if ',' in parts[2]:
|
|
|
|
output = fetch_users(parts[2].split(','))
|
|
|
|
else:
|
|
|
|
output = fetch_user(parts[2])
|
2018-04-17 15:03:35 -05:00
|
|
|
elif parts[1] == 'top' and len(parts) > 2:
|
2018-04-17 14:24:04 -05:00
|
|
|
output = fetch_top_n(parts[2])
|
2018-04-26 11:52:09 -05:00
|
|
|
elif parts[1] == 'register' and len(parts) > 2:
|
2018-04-25 13:51:11 -05:00
|
|
|
output = make_new_account(parts[2])
|
2018-04-26 11:52:09 -05:00
|
|
|
elif parts[1] == 'update' and len(parts) > 3:
|
|
|
|
output = update(parts[2], parts[3])
|
2018-04-17 14:35:29 -05:00
|
|
|
else:
|
|
|
|
output = None
|
2018-04-10 15:24:15 -05:00
|
|
|
|
|
|
|
return output
|
2018-04-08 23:48:07 -05:00
|
|
|
|
|
|
|
|
2018-04-06 00:31:31 -05:00
|
|
|
def application(environ, start_response):
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-10 14:32:36 -05:00
|
|
|
mod_wsgi entry point
|
2018-04-17 14:59:35 -05:00
|
|
|
'''
|
2018-04-06 00:31:31 -05:00
|
|
|
status = '200 OK'
|
2018-04-10 15:24:15 -05:00
|
|
|
output = process_request(environ['REQUEST_URI'])
|
|
|
|
|
|
|
|
if output is None:
|
|
|
|
status = '404 Not Found'
|
|
|
|
output = ''
|
|
|
|
|
|
|
|
output = output.encode('UTF-8')
|
2018-04-06 00:31:31 -05:00
|
|
|
|
2018-04-08 23:48:07 -05:00
|
|
|
response_headers = [('Content-type', 'application/json'),
|
2018-04-06 00:31:31 -05:00
|
|
|
('Content-Length', str(len(output)))]
|
2018-04-10 15:24:15 -05:00
|
|
|
|
2018-04-06 00:31:31 -05:00
|
|
|
start_response(status, response_headers)
|
|
|
|
|
|
|
|
return [output]
|
2018-04-17 14:50:46 -05:00
|
|
|
|
2018-04-17 15:14:52 -05:00
|
|
|
|
2018-04-17 14:57:52 -05:00
|
|
|
if __name__ == '__main__':
|
2018-04-26 11:52:09 -05:00
|
|
|
new_score = random.randint(1, 100)
|
|
|
|
print(fetch_user(1))
|
|
|
|
print(new_score)
|
|
|
|
update(1, new_score)
|
|
|
|
print(fetch_user(1))
|
2018-04-17 14:57:52 -05:00
|
|
|
|
2018-04-17 14:50:46 -05:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
|