Small changes
This commit is contained in:
parent
1e772e7034
commit
7653766c73
10
main.py
10
main.py
|
@ -1,6 +1,7 @@
|
|||
from fastapi import FastAPI, HTTPException
|
||||
from db import MySQLDB
|
||||
from PlayerSubscription import PlayerSubscription
|
||||
from utils import get_unix_timestamp_after_days
|
||||
|
||||
app = FastAPI()
|
||||
db = MySQLDB()
|
||||
|
@ -16,11 +17,18 @@ async def subscribe(player_data: PlayerSubscription):
|
|||
raise HTTPException(status_code=404, detail="Player not found")
|
||||
uuid = result[0][0]
|
||||
|
||||
if player_data.expiry is not None:
|
||||
if player_data.expiry < 0:
|
||||
raise HTTPException(status_code=400, detail="Expiry must be a positive integer")
|
||||
expiry_timestamp = get_unix_timestamp_after_days(player_data.expiry)
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail="Expiry must be a positive integer")
|
||||
|
||||
subscription_query = """
|
||||
INSERT INTO luckperms_user_permissions (uuid, permission, value, server, world, expiry, contexts)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
await db.execute(subscription_query, uuid, 'group.subscribe', 1, 'global', 'global', player_data.expiry, "{}")
|
||||
await db.execute(subscription_query, uuid, 'group.subscribe', 1, 'global', 'global', expiry_timestamp, "{}")
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
|
10
utils.py
10
utils.py
|
@ -1,11 +1,12 @@
|
|||
import yaml
|
||||
import datetime
|
||||
|
||||
def read_config(config_file):
|
||||
with open(config_file, 'r') as file:
|
||||
config = yaml.safe_load(file)
|
||||
return config
|
||||
|
||||
def get_parameters():
|
||||
def get_parameters() -> dict:
|
||||
parameters = {}
|
||||
config = read_config('config.yml')
|
||||
for key, value in config.items():
|
||||
|
@ -15,3 +16,10 @@ def get_parameters():
|
|||
else:
|
||||
parameters[key] = value
|
||||
return parameters
|
||||
|
||||
def get_unix_timestamp_after_days(days: int) -> int:
|
||||
return int((datetime.datetime.now() + datetime.timedelta(days=days)).timestamp()) if days != 0 else 0
|
||||
|
||||
def get_uts(timestamp: int) -> int:
|
||||
return datetime.datetime.utcfromtimestamp(timestamp)
|
||||
|
||||
|
|
Loading…
Reference in New Issue