2024-07-18 15:34:40 +00:00
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
from pydantic import BaseModel
|
2024-12-01 17:37:10 +00:00
|
|
|
from typing import Optional
|
2024-11-01 10:52:57 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from statistics import median
|
2024-11-30 05:45:14 +00:00
|
|
|
from uuid import uuid4
|
2024-12-01 14:34:58 +00:00
|
|
|
from random import randint
|
2024-12-04 09:07:45 +00:00
|
|
|
from time import sleep
|
2024-07-18 15:34:40 +00:00
|
|
|
|
2024-08-08 14:15:12 +00:00
|
|
|
# Fix 3.3 + 0.15 = 3.4499999999999997
|
|
|
|
from decimal import Decimal as d
|
|
|
|
def fix_add(one, two):
|
|
|
|
return float(d(str(one)) + d(str(two)))
|
|
|
|
def fix_sub(one, two):
|
|
|
|
return float(d(str(one)) - d(str(two)))
|
|
|
|
|
2024-07-18 15:34:40 +00:00
|
|
|
from db import *
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
def token_check(token):
|
|
|
|
db = read()
|
|
|
|
if token in db['tokens']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2024-12-04 09:07:45 +00:00
|
|
|
############## STATS ##################
|
|
|
|
|
|
|
|
STAT_RUN = False
|
2024-11-01 10:52:57 +00:00
|
|
|
def stat_run(cdm_change):
|
2024-11-29 09:10:01 +00:00
|
|
|
# Общий баланс, среднее значение, медиана
|
|
|
|
# Низшее значение, высшее значение
|
|
|
|
# Изменение баланса и time2cdm
|
|
|
|
# Прирост за счёт алмазов, за счёт time2cdm
|
2024-12-01 14:34:58 +00:00
|
|
|
|
2024-12-04 09:07:45 +00:00
|
|
|
# Защита от конфликтов
|
|
|
|
global STAT_RUN
|
|
|
|
while STAT_RUN:
|
|
|
|
sleep(1)
|
|
|
|
STAT_RUN = True
|
2024-11-29 09:10:01 +00:00
|
|
|
# Получаем все балансы
|
|
|
|
db = read()
|
|
|
|
bals = []
|
|
|
|
for id in db['id']:
|
|
|
|
bals.append(db['id'][id]['bal'])
|
|
|
|
|
2024-11-01 10:52:57 +00:00
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
2024-11-29 09:10:01 +00:00
|
|
|
stat = read('stat.json')
|
2024-11-01 10:52:57 +00:00
|
|
|
if date in stat:
|
|
|
|
stat[date]['gbal'] += cdm_change
|
|
|
|
else:
|
2024-11-29 09:10:01 +00:00
|
|
|
stat[date] = {'gbal': sum(bals), 'time2cdm': 0}
|
|
|
|
|
|
|
|
|
|
|
|
stats = {'gbal': 0, 'average': 0, 'median': 0, 'time2cdm': 0,
|
|
|
|
'gbal_delta': '0', 'time2cdm_delta': '',
|
|
|
|
'min': 0, 'max': 0,
|
|
|
|
'up_diamond': 0}
|
|
|
|
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
|
|
|
|
if yesterday in stat:
|
|
|
|
gbal_y = stat[yesterday]['gbal']
|
|
|
|
time2cdm_y = stat[yesterday]['time2cdm']
|
|
|
|
else:
|
|
|
|
gbal_y, time2cdm_y = 0, 0
|
|
|
|
time2cdm = stat[date]['time2cdm']
|
|
|
|
gbal = stat[date]['gbal']
|
|
|
|
|
|
|
|
# Заполняем данные
|
|
|
|
stats['gbal'] = round(gbal, 3)
|
|
|
|
stats['average'] = round(sum([x for x in bals if x != 0])/len([x for x in bals if x != 0]), 3)
|
|
|
|
stats['median'] = round(median([x for x in bals if x != 0]), 3)
|
|
|
|
stats['time2cdm'] = round(time2cdm, 3)
|
|
|
|
stats['time2cdm_delta'] = round(time2cdm-time2cdm_y, 3)
|
|
|
|
stats['gbal_delta'] = round(gbal-gbal_y, 3)
|
|
|
|
stats['min'] = round(min([x for x in bals if x != 0]), 3)
|
|
|
|
stats['max'] = round(max(bals), 3)
|
2024-12-02 08:30:36 +00:00
|
|
|
stats['up_diamond'] = round(gbal - (gbal_y + time2cdm))
|
2024-11-29 09:10:01 +00:00
|
|
|
|
|
|
|
stat[date] = stats
|
|
|
|
write(stat, 'stat.json')
|
2024-12-04 09:07:45 +00:00
|
|
|
STAT_RUN = False
|
|
|
|
|
|
|
|
#######################
|
2024-11-01 10:52:57 +00:00
|
|
|
|
2024-07-18 15:34:40 +00:00
|
|
|
class User_in_db(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str = None
|
|
|
|
tg: str = None
|
|
|
|
ds: str = None
|
|
|
|
mine: str = None
|
|
|
|
nick: str = None
|
|
|
|
@app.post('/api/user_in_db/')
|
|
|
|
def user_in_db(it: User_in_db):
|
|
|
|
token, id, tg, ds, mine, nick = it.token, it.id, it.tg, it.ds, it.mine, it.nick
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
try:
|
|
|
|
if id and id in db['id']:
|
|
|
|
return id
|
|
|
|
elif tg and tg in db['tg']:
|
|
|
|
return db['tg'][tg]
|
|
|
|
elif ds and ds in db['ds']:
|
|
|
|
return db['ds'][ds]
|
|
|
|
elif mine and mine in db['mine']:
|
|
|
|
return db['mine'][mine]
|
|
|
|
elif nick and nick in db['nick']:
|
|
|
|
return db['nick'][nick]
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
|
|
def gen_id():
|
|
|
|
db = read()
|
|
|
|
for i in range(1,100000):
|
|
|
|
check = str(i)
|
|
|
|
if check not in db['id']:
|
|
|
|
return str(i)
|
|
|
|
return 'Full?'
|
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class Add_user(BaseModel):
|
2024-07-18 15:34:40 +00:00
|
|
|
token: str
|
|
|
|
id: str = None
|
|
|
|
tg: str = None
|
|
|
|
ds: str = None
|
|
|
|
mine: str = None
|
|
|
|
nick: str
|
|
|
|
passwd: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/add_user/')
|
|
|
|
def add_user(it: Add_user):
|
2024-07-18 15:34:40 +00:00
|
|
|
token, id, tg, ds, mine, nick, passwd = it.token, it.id, it.tg, it.ds, it.mine, it.nick, it.passwd
|
|
|
|
id = gen_id()
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
2024-11-29 09:10:01 +00:00
|
|
|
db['id'][id] = {'tg': tg, 'ds': ds, 'mine': mine, 'nick': nick, 'passwd': passwd
|
|
|
|
, 'bal': 0.0, 'time2cdm': [0, datetime.today().strftime('%Y-%m-%d')]}
|
2024-07-18 15:34:40 +00:00
|
|
|
db['nick'][nick] = id
|
|
|
|
if tg:
|
|
|
|
db['tg'][tg] = id
|
|
|
|
if ds:
|
|
|
|
db['ds'][ds] = id
|
|
|
|
if mine:
|
|
|
|
db['mine'][mine] = id
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class Del_user(BaseModel):
|
2024-07-18 15:34:40 +00:00
|
|
|
token: str
|
|
|
|
id: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/del_user/')
|
|
|
|
def del_user(it: Del_user):
|
2024-07-18 15:34:40 +00:00
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
tg, ds, mine, nick = db['id'][id]['tg'], db['id'][id]['ds'], db['id'][id]['mine'], db['id'][id]['nick']
|
|
|
|
del db['nick'][nick]
|
|
|
|
if tg:
|
|
|
|
del db['tg'][tg]
|
|
|
|
if ds:
|
|
|
|
del db['ds'][ds]
|
|
|
|
if mine:
|
|
|
|
del db['mine'][mine]
|
|
|
|
del db['id'][id]
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class Add_coins(BaseModel):
|
2024-07-18 15:34:40 +00:00
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
amount: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/add_coins/')
|
|
|
|
def add_coins(it: Add_coins):
|
2024-11-08 16:37:42 +00:00
|
|
|
token, id, amount = it.token, it.id, abs(float(it.amount))
|
2024-07-18 15:34:40 +00:00
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
2024-08-08 14:15:12 +00:00
|
|
|
db['id'][id]['bal'] = fix_add(db['id'][id]['bal'], amount)
|
2024-07-18 15:34:40 +00:00
|
|
|
write(db)
|
2024-11-01 10:52:57 +00:00
|
|
|
stat_run(amount)
|
2024-07-18 15:34:40 +00:00
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class Del_coins(BaseModel):
|
2024-07-18 15:34:40 +00:00
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
amount: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/del_coins/')
|
|
|
|
def del_coins(it: Del_coins):
|
2024-11-08 16:53:16 +00:00
|
|
|
token, id, amount = it.token, it.id, abs(float(it.amount))
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
if db['id'][id]['bal'] >= amount:
|
|
|
|
db['id'][id]['bal'] = fix_sub(db['id'][id]['bal'], amount)
|
|
|
|
write(db)
|
|
|
|
stat_run(amount*-1)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
2024-07-18 15:34:40 +00:00
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class Transfer_coins(BaseModel):
|
2024-07-18 15:34:40 +00:00
|
|
|
token: str
|
|
|
|
src_id: str
|
|
|
|
dst_id: str
|
|
|
|
amount: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/transfer_coins/')
|
|
|
|
def transfer_coins(it: Transfer_coins):
|
2024-07-18 15:34:40 +00:00
|
|
|
token, src_id, dst_id, amount = it.token, it.src_id, it.dst_id, float(it.amount)
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
amount = abs(amount) # Защита от отриц. чисел
|
|
|
|
src_bal = db['id'][src_id]['bal']
|
2024-08-08 14:15:12 +00:00
|
|
|
# Больше баланса и количество цифр после запятой <= 3
|
|
|
|
if src_bal >= amount and len(str(amount).split('.')[1]) <= 3: # and amount > 0.0001:
|
|
|
|
db['id'][src_id]['bal'] = fix_sub(db['id'][src_id]['bal'], amount)
|
|
|
|
db['id'][dst_id]['bal'] = fix_add(db['id'][dst_id]['bal'], amount)
|
2024-07-18 15:34:40 +00:00
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'No_money'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Update_tg(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
tg: str
|
|
|
|
@app.post('/api/update_tg/')
|
|
|
|
def update_tg(it: Update_tg):
|
|
|
|
token, id, tg = it.token, it.id, it.tg
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
cur_tg = db['id'][id]['tg']
|
|
|
|
try:
|
|
|
|
del db['tg'][cur_tg]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if tg == 'None':
|
|
|
|
db['id'][id]['tg'] = None
|
|
|
|
else:
|
|
|
|
db['id'][id]['tg'] = tg
|
|
|
|
db['tg'][tg] = id
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Update_ds(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
ds: str
|
|
|
|
@app.post('/api/update_ds/')
|
|
|
|
def update_ds(it: Update_ds):
|
|
|
|
token, id, ds = it.token, it.id, it.ds
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
cur_ds = db['id'][id]['ds']
|
|
|
|
try:
|
|
|
|
del db['ds'][cur_ds]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if ds == 'None':
|
|
|
|
db['id'][id]['ds'] = None
|
|
|
|
else:
|
|
|
|
db['id'][id]['ds'] = ds
|
|
|
|
db['ds'][ds] = id
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Update_mine(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
mine: str
|
|
|
|
@app.post('/api/update_mine/')
|
|
|
|
def update_mine(it: Update_mine):
|
|
|
|
token, id, mine = it.token, it.id, it.mine
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
2024-10-26 11:33:26 +00:00
|
|
|
try:
|
|
|
|
cur_mine = db['id'][id]['mine']
|
|
|
|
del db['mine'][cur_mine]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if mine == 'None':
|
|
|
|
db['id'][id]['mine'] = None
|
|
|
|
else:
|
|
|
|
db['id'][id]['mine'] = mine
|
|
|
|
db['mine'][mine] = id
|
2024-07-18 15:34:40 +00:00
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Update_nick(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
nick: str
|
|
|
|
@app.post('/api/update_nick/')
|
|
|
|
def update_nick(it: Update_nick):
|
|
|
|
token, id, nick = it.token, it.id, it.nick
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
cur_nick = db['id'][id]['nick']
|
|
|
|
del db['nick'][cur_nick]
|
|
|
|
db['id'][id]['nick'] = nick
|
|
|
|
db['nick'][nick] = id
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Update_passwd(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
passwd: str
|
|
|
|
@app.post('/api/update_passwd/')
|
2024-11-01 10:52:57 +00:00
|
|
|
def update_passwd(it: Update_passwd):
|
2024-07-18 15:34:40 +00:00
|
|
|
token, id, passwd = it.token, it.id, it.passwd
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
db['id'][id]['passwd'] = passwd
|
|
|
|
write(db)
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-11-01 10:52:57 +00:00
|
|
|
class Add_time(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
time: str
|
|
|
|
@app.post('/api/add_time/')
|
|
|
|
def add_time(it: Add_time):
|
|
|
|
token, id, time = it.token, it.id, int(it.time)
|
|
|
|
if token_check(token):
|
2024-11-29 09:10:01 +00:00
|
|
|
course = read('conf.json')['time2cdm']
|
|
|
|
amount = time*course
|
|
|
|
# Пополнение баланса
|
2024-11-01 10:52:57 +00:00
|
|
|
db = read()
|
2024-11-29 09:10:01 +00:00
|
|
|
db['id'][id]['bal'] += amount
|
|
|
|
# Статистика
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
2024-12-06 09:27:24 +00:00
|
|
|
if date not in stat:
|
|
|
|
stat_run(0)
|
2024-11-29 09:10:01 +00:00
|
|
|
# Для пользователя
|
|
|
|
t2c_date = db['id'][id]['time2cdm'][1]
|
|
|
|
if t2c_date != date:
|
|
|
|
db['id'][id]['time2cdm'][0] = amount
|
|
|
|
db['id'][id]['time2cdm'][1] = date
|
|
|
|
else:
|
|
|
|
db['id'][id]['time2cdm'][0] += amount
|
2024-11-01 10:52:57 +00:00
|
|
|
write(db)
|
2024-11-29 09:10:01 +00:00
|
|
|
# Глобально
|
|
|
|
stat = read('stat.json')
|
|
|
|
stat[date]['time2cdm'] += amount
|
|
|
|
write(stat, 'stat.json')
|
2024-12-04 09:07:45 +00:00
|
|
|
|
2024-12-05 09:59:18 +00:00
|
|
|
stat_run(amount)
|
2024-11-01 10:52:57 +00:00
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-07-18 15:34:40 +00:00
|
|
|
|
|
|
|
class Check_bal(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/check_bal/')
|
|
|
|
def check_bal(it: Check_bal):
|
2024-11-29 09:10:01 +00:00
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
stat_run(0)
|
|
|
|
db = read()
|
|
|
|
# Если дата time2cdm прошла - обнулить баланс
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
t2c_date = db['id'][id]['time2cdm'][1]
|
|
|
|
if t2c_date != date:
|
|
|
|
db['id'][id]['time2cdm'][0] = 0
|
|
|
|
db['id'][id]['time2cdm'][1] = date
|
|
|
|
write(db)
|
|
|
|
return db['id'][id]['bal']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
2024-07-18 15:34:40 +00:00
|
|
|
|
|
|
|
class Get_nick(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/get_nick/')
|
|
|
|
def get_nick(it: Get_nick):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
return db['id'][id]['nick']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Get_tg(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/get_tg/')
|
|
|
|
def get_tg(it: Get_tg):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
return db['id'][id]['tg']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Get_ds(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/get_ds/')
|
|
|
|
def get_ds(it: Get_ds):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
return db['id'][id]['ds']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Get_mine(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/get_mine/')
|
|
|
|
def get_mine(it: Get_mine):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
return db['id'][id]['mine']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Get_passwd(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/get_passwd/')
|
|
|
|
def get_passwd(it: Get_passwd):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
|
|
|
return db['id'][id]['passwd']
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-11-29 09:10:01 +00:00
|
|
|
class Get_time2cdm(BaseModel):
|
2024-11-01 10:52:57 +00:00
|
|
|
token: str
|
|
|
|
id: str
|
2024-11-29 09:10:01 +00:00
|
|
|
@app.post('/api/get_time2cdm/')
|
|
|
|
def get_time(it: Get_time2cdm):
|
2024-11-01 10:52:57 +00:00
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
db = read()
|
2024-11-29 09:10:01 +00:00
|
|
|
return db['id'][id]['time2cdm'][0]
|
2024-11-01 10:52:57 +00:00
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Get_stat(BaseModel):
|
|
|
|
token: str
|
2024-12-04 09:07:45 +00:00
|
|
|
date: Optional[str] = None
|
2024-11-01 10:52:57 +00:00
|
|
|
@app.post('/api/get_stat/')
|
|
|
|
def get_stat(it: Get_stat):
|
2024-12-01 14:34:58 +00:00
|
|
|
token, date = it.token, it.date
|
2024-12-04 09:07:45 +00:00
|
|
|
if not date:
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d')
|
2024-11-01 10:52:57 +00:00
|
|
|
if token_check(token):
|
|
|
|
stat_run(0)
|
2024-11-29 09:10:01 +00:00
|
|
|
db = read('stat.json')
|
2024-12-01 14:34:58 +00:00
|
|
|
if date not in db:
|
|
|
|
return 'Not found'
|
2024-11-29 09:10:01 +00:00
|
|
|
stats = db[date]
|
2024-11-01 10:52:57 +00:00
|
|
|
return stats
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
############# USER API ################
|
|
|
|
|
|
|
|
class Gen_token(BaseModel):
|
2024-11-30 05:45:14 +00:00
|
|
|
token: str
|
|
|
|
id: str
|
2024-12-01 14:34:58 +00:00
|
|
|
@app.post('/api/gen_token/')
|
|
|
|
def gen_token(it: Gen_token):
|
2024-11-30 05:45:14 +00:00
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
user_token = str(uuid4())
|
|
|
|
user_api = read('user_api.json')
|
|
|
|
user_api['tokens'][id] = user_token
|
|
|
|
write(user_api, 'user_api.json')
|
|
|
|
return user_token
|
|
|
|
else:
|
|
|
|
return 'Error'
|
2024-07-18 15:34:40 +00:00
|
|
|
|
2024-12-01 14:34:58 +00:00
|
|
|
class List_fp(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
@app.post('/api/list_fp/')
|
|
|
|
def list_fp(it: List_fp):
|
|
|
|
token, id = it.token, it.id
|
|
|
|
if token_check(token):
|
|
|
|
user_api = read('user_api.json')
|
|
|
|
if id not in user_api['fp']:
|
|
|
|
user_api['fp'][id] = []
|
|
|
|
write(user_api, 'user_api.json')
|
|
|
|
return user_api['fp'][id]
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
def gen_fp_id(user_api):
|
|
|
|
ok = False
|
|
|
|
while not ok:
|
|
|
|
ok = True
|
|
|
|
fp_id = str(randint(5236, 645862))
|
|
|
|
if fp_id in user_api['fp']:
|
|
|
|
ok = False
|
|
|
|
return fp_id
|
|
|
|
|
|
|
|
class Gen_fp(BaseModel):
|
|
|
|
token: str
|
|
|
|
id: str
|
|
|
|
amount: str
|
|
|
|
@app.post('/api/gen_fp/')
|
|
|
|
def gen_fp(it: Gen_fp):
|
|
|
|
token, id, amount = it.token, it.id, it.amount
|
|
|
|
if token_check(token):
|
2024-12-05 09:59:18 +00:00
|
|
|
try:
|
|
|
|
if float(amount) <= 0.0001:
|
|
|
|
return 'Error'
|
|
|
|
amount = str(float(amount)) # Защиты от 1000 нулей в начале
|
|
|
|
except:
|
|
|
|
return 'Error'
|
2024-12-01 14:34:58 +00:00
|
|
|
user_api = read('user_api.json')
|
|
|
|
if id not in user_api['fp']:
|
|
|
|
user_api['fp'][id] = []
|
|
|
|
write(user_api, 'user_api.json')
|
|
|
|
if len(user_api['fp'][id]) >= 5:
|
|
|
|
return 'Limit'
|
|
|
|
fp_id = gen_fp_id(user_api)
|
|
|
|
user_api['fp'][fp_id] = [id, amount]
|
|
|
|
user_api['fp'][id].append(fp_id)
|
|
|
|
write(user_api, 'user_api.json')
|
|
|
|
return fp_id
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
class Del_fp(BaseModel):
|
|
|
|
token: str
|
|
|
|
fp_id: str
|
|
|
|
@app.post('/api/del_fp/')
|
|
|
|
def del_fp(it: Del_fp):
|
|
|
|
token, fp_id = it.token, it.fp_id
|
|
|
|
if token_check(token):
|
|
|
|
user_api = read('user_api.json')
|
|
|
|
id = user_api['fp'][fp_id][0]
|
|
|
|
del user_api['fp'][fp_id]
|
|
|
|
user_api['fp'][id].remove(fp_id)
|
|
|
|
write(user_api, 'user_api.json')
|
|
|
|
return 'OK'
|
|
|
|
else:
|
|
|
|
return 'Error'
|
|
|
|
|
2024-07-18 15:34:40 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import uvicorn
|
2024-08-08 14:15:12 +00:00
|
|
|
uvicorn.run(app, host='0.0.0.0', port=7001)
|