-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (99 loc) · 4.07 KB
/
main.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
import DataBase
import MyAnimeList
import datetime
from flask import Flask, request, g
import base64
import psycopg2
import json
import secrets
app = Flask(__name__)
#****************** TEMPORARY JUST FOR DEVELOPMENT
#cors = CORS(app) //for development only
#from flask_cors import CORS, cross_origin
#*******************
# add logging for the amount of logins and dashboard visits!!!!!!!!!!
def get_db():
if "db" not in g:
g.db = DataBase.DataBase()
return g.db
def generate_30_day_date():
date_format_string = "%Y-%m-%d"
return (datetime.datetime.now() + datetime.timedelta(30)).strftime(date_format_string)
def get_user_agent():
return request.headers.get('User-Agent')
@app.route("/api/authenticateUser", methods=["POST"])
def authenticateUser():
data_object = {"status": False, "session-key": None}
#expected data type application/json
requestData = request.get_json()
try:
username = requestData["username"]
password = requestData["password"]
type = requestData["type"]
except KeyError:
return json.dumps(data_object)
if not username or not password or not type:
return json.dumps(data_object)
if type == "signup":
if (get_db().check_if_username_exists(username)):
return json.dumps(data_object)
else:
#assume its safe
get_db().createUser(username, password)
if (get_db().verify_user(username, password)):
data_object["status"] = True;
session_key = secrets.token_urlsafe(128)
#assume its safe because we already verified user
get_db().write_session_to_user(username, session_key, generate_30_day_date(), get_user_agent())
data_object["session-key"] = session_key
return json.dumps(data_object)
else:
return json.dumps(data_object)
return json.dumps(data_object)
@app.route("/api/getWatchList", methods=["GET"])
def getWatchList():
dataObject = {"sessionKeyValid": False, "WatchList": None}
session_key = request.headers.get("session-key")
if not session_key or not get_db().is_session_valid(session_key):
return dataObject
for _ in range(2): #loop can only run twice
mal_auth_details = get_db().get_mal_auth_details(session_key)
if not mal_auth_details["status"]:
return dataObject
else:
dataObject["sessionKeyValid"] = get_db().is_session_valid(session_key)
dataObject["WatchList"] = MyAnimeList.get_watch_list(mal_auth_details["accesstoken"])
if (not dataObject["WatchList"]):
MyAnimeList.refresh_access_token(session_key, mal_auth_details["refreshtoken"], get_db())
else:
return dataObject
return dataObject
@app.route("/api/authenticateMal", methods=["POST"])
def authenticateMal():
data_object = {"status": False}
data = request.get_json()
if not "sessionKey" in data or not "authorizationCode" in data or not data["sessionKey"] or not data["authorizationCode"] or not get_db().is_session_valid(data["sessionKey"]):
return json.dumps(data_object)
data_object["status"] = MyAnimeList.authenticate_user(data["sessionKey"], data["authorizationCode"], data["codeChallenge"], get_db())
return json.dumps(data_object)
@app.route("/api/adjustEpisode", methods=["POST"])
def adjustEpisode():
dataObject = {"status": False}
requestData = json.loads(request.data)
try:
numWatched = requestData["numWatched"]
id = requestData["id"]
except KeyError:
return json.dumps(dataObject)
session_key = request.headers.get("session-key")
if not session_key or not get_db().is_session_valid(session_key):
return dataObject
mal_auth_details = get_db().get_mal_auth_details(session_key)
if not mal_auth_details["status"]:
return dataObject
if (not MyAnimeList.update_episode(mal_auth_details["accesstoken"], id, numWatched)):
return json.dumps(dataObject)
dataObject["status"] = True
return json.dumps(dataObject)
if __name__ == "__main__":
app.run(debug=True, port=8000, host="0.0.0.0")