diff options
author | Kyomotoi <[email protected]> | 2020-11-07 13:54:57 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2020-11-07 13:54:57 +0800 |
commit | 7cae371b51a14c626ce184987eea2392e15430b9 (patch) | |
tree | a35aa21a64dad59a8dc91270d78f781dbed8953d /api.py | |
parent | 11e4632aaf2be56c776dbc4e9f0ad5065bb60b5f (diff) | |
download | ATRI-7cae371b51a14c626ce184987eea2392e15430b9.tar.gz ATRI-7cae371b51a14c626ce184987eea2392e15430b9.tar.bz2 ATRI-7cae371b51a14c626ce184987eea2392e15430b9.zip |
[Update]
Diffstat (limited to 'api.py')
-rw-r--r-- | api.py | 199 |
1 files changed, 109 insertions, 90 deletions
@@ -13,41 +13,47 @@ __author__ = 'kyomotoi' import json import time import sqlite3 +import uvicorn from enum import Enum from pathlib import Path from fastapi import FastAPI from starlette.requests import Request from starlette.templating import Jinja2Templates +from utils.utils_yml import load_yaml from utils.utils_request import aio_get_bytes - # 急着去上学写乱了下次一定改别骂了别骂了我爪巴爪巴爪巴 # orz orz orz orz orz - app = FastAPI() tmp_API = Jinja2Templates(directory='ATRI/data/data_HTML/api') tmp_HELP = Jinja2Templates(directory='ATRI/data/data_HTML/user') async def get_ip_info(ip: str) -> None: - URL = await aio_get_bytes(f"http://ip-api.com/json/{ip}?lang=zh-CN&fields=status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,isp,org,as,asname,reverse,mobile,proxy,hosting,query") + URL = await aio_get_bytes( + f"http://ip-api.com/json/{ip}?lang=zh-CN&fields=status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,isp,org,as,asname,reverse,mobile,proxy,hosting,query" + ) info = json.loads(URL) return info + def check_visitors(query: str): try: - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'banip' / 'banip.json', 'r') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'banip' / 'banip.json', 'r') as f: data = json.load(f) except: data = {} - + if query in data: return False else: return True + async def load_setu(name: str): file_type = 'normal' s_type = 'normal' @@ -65,62 +71,61 @@ async def load_setu(name: str): s_type = "r18" else: - return { - "code": 404, - "data": "", - "msg": "Can't find aims type!" - } + return {"code": 404, "data": "", "msg": "Can't find aims type!"} con = sqlite3.connect(file_type) cur = con.cursor() info = cur.execute(f'SELECT * FROM {s_type} ORDER BY RANDOM() limit 1;') for i in info: - pid = i[0] - title = i[1] - tags = i[2] - account = i[3] - name = i[4] - u_id = i[5] - user_link = i[6] - img = i[7] - print('success!') - con.commit() - con.close() - return { - "code": 200, - "pid": pid, - "title": title, - "tags": tags, - "account": account, - "name": name, - "u_id": u_id, - "user_link": user_link, - "img": img - } - - -file_setu_normal = Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / 'normal.db' -file_setu_nearR18 = Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / 'nearR18.db' + pid = i[0] + title = i[1] + tags = i[2] + account = i[3] + name = i[4] + u_id = i[5] + user_link = i[6] + img = i[7] + print('success!') + con.commit() + con.close() + return { + "code": 200, + "pid": pid, + "title": title, + "tags": tags, + "account": account, + "name": name, + "u_id": u_id, + "user_link": user_link, + "img": img + } + + +file_setu_normal = Path( + '.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / 'normal.db' +file_setu_nearR18 = Path( + '.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / 'nearR18.db' file_setu_R18 = Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / 'r18.db' -con = sqlite3.connect(file_setu_normal) # setu-normal +con = sqlite3.connect(file_setu_normal) # setu-normal cur = con.cursor() cur.execute("select * from normal") data_normal = len(cur.fetchall()) con.close() -con = sqlite3.connect(file_setu_nearR18) # setu-nearR18 +con = sqlite3.connect(file_setu_nearR18) # setu-nearR18 cur = con.cursor() cur.execute("select * from nearR18") data_nearR18 = len(cur.fetchall()) con.close() -con = sqlite3.connect(file_setu_R18) # setu-r18 +con = sqlite3.connect(file_setu_R18) # setu-r18 cur = con.cursor() cur.execute("select * from r18") data_r18 = len(cur.fetchall()) con.close() + class SetuModelName(str, Enum): noraml = "normal" nearR18 = "nearR18" @@ -138,54 +143,63 @@ async def index(request: Request): info = await get_ip_info(ip) try: - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_all.json', 'r') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_all.json', 'r') as f: data_all = json.load(f) except: data_all = {} - + try: - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_index.json', 'r') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_index.json', 'r') as f: data_index = json.load(f) except: data_index = {} - + info = await get_ip_info(ip) data_all[f"{time.time()}"] = [query, info, "index"] data_index[f"{time.time()}"] = [query, info] - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_all.json', 'w') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_all.json', 'w') as f: f.write(json.dumps(data_all)) f.close() - - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_index.json', 'w') as f: + + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_index.json', 'w') as f: f.write(json.dumps(data_index)) f.close() - return tmp_API.TemplateResponse('index.html', - {'request': request, - 'data_normal': data_normal, - 'data_nearR18': data_nearR18, - 'data_r18': data_r18, - 'times_all': 4, - 'date_now': localtime, - 'times_now': 6, - 'date_yesterday': 7, - 'times_yesterday': 8, - 'date_before_yesterday': 9, - 'times_before_yesterday': 10, - 'info_ip': query, - 'info_continent': info["continent"], - 'info_country': info["country"], - 'info_regionName': info["regionName"], - 'info_city': info["city"], - 'info_lat': info["lat"], - 'info_lon': info["lon"], - 'info_timezone': info["timezone"], - 'info_isp': info["isp"], - 'info_as': info["as"], - 'info_asname': info["asname"] - } - ) + return tmp_API.TemplateResponse( + 'index.html', { + 'request': request, + 'data_normal': data_normal, + 'data_nearR18': data_nearR18, + 'data_r18': data_r18, + 'times_all': 4, + 'date_now': localtime, + 'times_now': 6, + 'date_yesterday': 7, + 'times_yesterday': 8, + 'date_before_yesterday': 9, + 'times_before_yesterday': 10, + 'info_ip': query, + 'info_continent': info["continent"], + 'info_country': info["country"], + 'info_regionName': info["regionName"], + 'info_city': info["city"], + 'info_lat': info["lat"], + 'info_lon': info["lon"], + 'info_timezone': info["timezone"], + 'info_isp': info["isp"], + 'info_as': info["as"], + 'info_asname': info["asname"] + }) + @app.get("/api/setu/{s_type}") async def get_setu(s_type: str, request: Request): @@ -194,49 +208,54 @@ async def get_setu(s_type: str, request: Request): query = str(ip) + ":" + str(port) try: - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_all.json', 'r') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_all.json', 'r') as f: data_all = json.load(f) except: data_all = {} - + try: - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_setu.json', 'r') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_setu.json', 'r') as f: data_setu = json.load(f) except: data_setu = {} - + info = await get_ip_info(ip) data_all[f"{time.time()}"] = [query, info, "setu"] data_setu[f"{time.time()}"] = [query, info] - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_all.json', 'w') as f: + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_all.json', 'w') as f: f.write(json.dumps(data_all)) f.close() - - with open(Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / 'times' / 'api_setu.json', 'w') as f: + + with open( + Path('.') / 'ATRI' / 'data' / 'data_HTML' / 'api' / 'data' / + 'times' / 'api_setu.json', 'w') as f: f.write(json.dumps(data_setu)) f.close() if check_visitors(query): if s_type == SetuModelName.noraml: return await load_setu(s_type) - + elif s_type == SetuModelName.nearR18: return await load_setu(s_type) - + elif s_type == SetuModelName.R18: return await load_setu(s_type) else: - return { - "code": 403, - "data": "", - "msg": "请停止非法行为!如再继续,达到次数将永久Ban IP!" - } + return {"code": 403, "data": "", "msg": "请停止非法行为!如再继续,达到次数将永久Ban IP!"} + if __name__ == '__main__': - file_info = Path('.') / 'key.json' - with open(file_info, 'r') as f: - file_info = json.load(f) - import uvicorn - uvicorn.run(app, host=file_info["html"]["ip"], port=file_info["html"]["port"])
\ No newline at end of file + CONFIG_PATH = Path('.') / 'config.yml' + config = load_yaml(CONFIG_PATH)['api'] + uvicorn.run(app, + host=config['host'], + port=config['port']) |