mirror of
https://github.com/FAUSheppy/open-web-leaderboard.git
synced 2025-12-07 23:51:36 +01:00
more api fixes
This commit is contained in:
45
api.py
45
api.py
@@ -7,6 +7,8 @@ import os
|
|||||||
import time
|
import time
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
import requests
|
import requests
|
||||||
|
import datetime as dt
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
REGION = "euw1"
|
REGION = "euw1"
|
||||||
|
|
||||||
@@ -29,16 +31,41 @@ def divisionToNumber(division):
|
|||||||
"IV" : 0 }
|
"IV" : 0 }
|
||||||
return divisionmap[division]
|
return divisionmap[division]
|
||||||
|
|
||||||
cache = dict()
|
DATABASE = "rating_cache.sqlite"
|
||||||
counter =
|
def checkPlayerKnown(playerName):
|
||||||
|
conn = sqlite3.connect(DATABASE)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
backlog = dt.datetime.now() - dt.timedelta(days=7)
|
||||||
|
query = '''SELECT * from players where playerName = ? LIMIT 1;'''
|
||||||
|
cursor.execute(query, (playerName,))
|
||||||
|
try:
|
||||||
|
playerName, rating, lastUpdated = cursor.fetchone()
|
||||||
|
except TypeError:
|
||||||
|
print("sqlite cache player not found")
|
||||||
|
return None
|
||||||
|
conn.close()
|
||||||
|
return (playerName, rating, lastUpdated)
|
||||||
|
|
||||||
|
def addToDB(playerName, rating):
|
||||||
|
|
||||||
|
conn = sqlite3.connect(DATABASE)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("INSERT INTO players VALUES(?,?,?);",(
|
||||||
|
playerName,
|
||||||
|
rating,
|
||||||
|
dt.datetime.now().timestamp()))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
def getCache():
|
|
||||||
return cache
|
|
||||||
|
|
||||||
def getPlayerRatingFromApi(playerName, WATCHER):
|
def getPlayerRatingFromApi(playerName, WATCHER):
|
||||||
|
|
||||||
if playerName in cache:
|
if not playerName:
|
||||||
return cache[playerName]
|
return 0
|
||||||
|
|
||||||
|
tupel = checkPlayerKnown(playerName)
|
||||||
|
if tupel:
|
||||||
|
return tupel[1]
|
||||||
|
|
||||||
while(True):
|
while(True):
|
||||||
try:
|
try:
|
||||||
@@ -46,7 +73,7 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
|||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
# not found #
|
# not found #
|
||||||
if e.response.status_code == 404:
|
if e.response.status_code == 404:
|
||||||
cache[playerName] = 0
|
addToDB(playerName, 0)
|
||||||
return 0
|
return 0
|
||||||
# rate limit
|
# rate limit
|
||||||
elif e.response.status_code == 429:
|
elif e.response.status_code == 429:
|
||||||
@@ -56,7 +83,7 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
|||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
if not pTmp:
|
if not pTmp:
|
||||||
cache[playerName] = 0
|
addToDB(playerName, 0)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
computed = 0
|
computed = 0
|
||||||
@@ -77,6 +104,6 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
|||||||
computed = tierToNumber(queue["tier"]) + divisionToNumber(queue["rank"]) + \
|
computed = tierToNumber(queue["tier"]) + divisionToNumber(queue["rank"]) + \
|
||||||
int(queue["leaguePoints"])
|
int(queue["leaguePoints"])
|
||||||
print(computed)
|
print(computed)
|
||||||
cache.update( { playerName : computed })
|
addToDB(playerName, computed)
|
||||||
|
|
||||||
return computed
|
return computed
|
||||||
|
|||||||
24
server.py
24
server.py
@@ -152,7 +152,7 @@ class Player:
|
|||||||
submission = dict()
|
submission = dict()
|
||||||
@app.route("/role-submission", methods=['GET', 'POST'])
|
@app.route("/role-submission", methods=['GET', 'POST'])
|
||||||
def roleSubmissionTool():
|
def roleSubmissionTool():
|
||||||
positions=["Top", "Jungle", "Mid", "Support", "Bottom"]
|
positions=["Top", "Jungle", "Mid", "Bottom", "Support" ]
|
||||||
|
|
||||||
ident = flask.request.args.get("id")
|
ident = flask.request.args.get("id")
|
||||||
if flask.request.method == 'POST':
|
if flask.request.method == 'POST':
|
||||||
@@ -192,11 +192,12 @@ def balanceToolData():
|
|||||||
return flask.Response(json.dumps(retDict), 200, mimetype='application/json')
|
return flask.Response(json.dumps(retDict), 200, mimetype='application/json')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
@app.route("/balance-tool", methods=['GET', 'POST'])
|
@app.route("/balance-tool", methods=['GET', 'POST'])
|
||||||
def balanceTool():
|
def balanceTool():
|
||||||
positions=["Top", "Jungle", "Mid", "Support", "Bottom"]
|
positions=["Top", "Jungle", "Mid", "Bottom", "Support"]
|
||||||
|
|
||||||
db = DatabaseConnection(app.config["DB_PATH"])
|
#db = DatabaseConnection(app.config["DB_PATH"])
|
||||||
|
|
||||||
if flask.request.method == 'POST':
|
if flask.request.method == 'POST':
|
||||||
|
|
||||||
@@ -310,6 +311,14 @@ def playerApi():
|
|||||||
else:
|
else:
|
||||||
return ("Nope", 404)
|
return ("Nope", 404)
|
||||||
|
|
||||||
|
@app.route("/player-api-cache")
|
||||||
|
def playerApiCache():
|
||||||
|
result = api.checkPlayerKnown(flask.request.args.get("id"))
|
||||||
|
if result:
|
||||||
|
return ("OK", 200)
|
||||||
|
else:
|
||||||
|
return ("Nope", 404)
|
||||||
|
|
||||||
@app.route("/player")
|
@app.route("/player")
|
||||||
def player():
|
def player():
|
||||||
'''Show Info about Player'''
|
'''Show Info about Player'''
|
||||||
@@ -366,7 +375,6 @@ def player():
|
|||||||
Y_MIN=yMin, Y_MAX=yMax)
|
Y_MIN=yMin, Y_MAX=yMax)
|
||||||
|
|
||||||
@app.route('/leaderboard')
|
@app.route('/leaderboard')
|
||||||
@app.route('/')
|
|
||||||
@cache.cached(timeout=10, query_string=True)
|
@cache.cached(timeout=10, query_string=True)
|
||||||
def leaderboard():
|
def leaderboard():
|
||||||
'''Show main leaderboard page with range dependant on parameters'''
|
'''Show main leaderboard page with range dependant on parameters'''
|
||||||
@@ -452,12 +460,6 @@ def send_js(path):
|
|||||||
|
|
||||||
@app.before_first_request
|
@app.before_first_request
|
||||||
def init():
|
def init():
|
||||||
SERVERS_FILE = "servers.json"
|
|
||||||
if os.path.isfile(SERVERS_FILE):
|
|
||||||
import valve.source.a2s
|
|
||||||
from valve.source import NoResponseError
|
|
||||||
with open(SERVERS_FILE) as f:
|
|
||||||
SERVERS = json.load(f)
|
|
||||||
|
|
||||||
global WATCHER
|
global WATCHER
|
||||||
with open("key.txt","r") as f:
|
with open("key.txt","r") as f:
|
||||||
@@ -472,7 +474,7 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument('--port', default="5002", \
|
parser.add_argument('--port', default="5002", \
|
||||||
help='Port on which flask (this server) will take requests on')
|
help='Port on which flask (this server) will take requests on')
|
||||||
|
|
||||||
parser.add_argument('--skillbird-db', required=True, help='skillbird database (overrides web connection if set)')
|
parser.add_argument('--skillbird-db', required=False, help='skillbird database (overrides web connection if set)')
|
||||||
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|||||||
5
sqlite.init
Normal file
5
sqlite.init
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE players(
|
||||||
|
playerName TEXT PRIMARY KEY,
|
||||||
|
rating INTEGER,
|
||||||
|
lastupdated TEXT
|
||||||
|
);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
positions = [ "Top", "Jungle", "Mid", "Support" , "Bottom" ]
|
positions = [ "Top", "Jungle", "Mid", "Bottom", "Support" ]
|
||||||
acceptedParser = [ "top", "jungle", "mid", "sup" , "bot", "adc", "support", "bottom", "*" ]
|
acceptedParser = [ "top", "jungle", "mid", "sup" , "bot", "adc", "support", "bottom", "*" ]
|
||||||
sides = [ "left", "right"]
|
sides = [ "left", "right"]
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@ var checkPlayerFunc = function checkPlayer() {
|
|||||||
if(this.value == ""){
|
if(this.value == ""){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
url = "/player?id=" + this.value
|
url = "/player-api-cache?id=" + this.value
|
||||||
fetch(url).then(r => {
|
fetch(url).then(r => {
|
||||||
if(r.status == 200){
|
if(r.status == 200){
|
||||||
this.style.background = "#74bb74"
|
this.style.background = "#74bb74"
|
||||||
|
|||||||
Reference in New Issue
Block a user