Added old archive endpoints
This commit is contained in:
parent
2a852388bf
commit
16c7792b7f
3
app.py
3
app.py
|
@ -13,7 +13,8 @@ api = Api(app)
|
|||
|
||||
KF3DB(api)
|
||||
KFKDB(api)
|
||||
KFADB(api)
|
||||
KFADB(api, "Archive", "/home/pi/python/Katbots/JapariArchive/database.db")
|
||||
KFADB(api, "ArchiveOld", "/home/pi/python/Katbots/JapariArchive/databaseOld.db")
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='127.0.0.1', port=8080, debug=True)
|
|
@ -2,12 +2,12 @@ import json
|
|||
from flask import Flask
|
||||
|
||||
from .databaseController import DatabaseController
|
||||
from .endpoints.query import Query
|
||||
from .endpoints.post import GetPost
|
||||
from .endpoints.posts import GetPosts
|
||||
from .endpoints.set_action import SetAction
|
||||
from .endpoints.posts_count import GetPostsCount
|
||||
from .endpoints.account_stats import AccountStats
|
||||
from .endpoints.query import Query, QueryOld
|
||||
from .endpoints.post import GetPost, GetPostOld
|
||||
from .endpoints.posts import GetPosts, GetPostsOld
|
||||
from .endpoints.set_action import SetAction, SetActionOld
|
||||
from .endpoints.posts_count import GetPostsCount, GetPostsCountOld
|
||||
from .endpoints.account_stats import AccountStats, AccountStatsOld
|
||||
|
||||
def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating):
|
||||
if files == "" or files is None:
|
||||
|
@ -25,21 +25,30 @@ class Database:
|
|||
db : DatabaseController = None
|
||||
app : Flask = None
|
||||
|
||||
def __init__(self, api) -> None:
|
||||
def __init__(self, api, database_name, database_path) -> None:
|
||||
self.app = api.app
|
||||
if "Archive" in self.app.databases:
|
||||
del self.app.databases["Archive"]
|
||||
|
||||
self.reload_data()
|
||||
if database_name in self.app.databases:
|
||||
del self.app.databases[database_name]
|
||||
|
||||
self.app.databases["Archive"] = self
|
||||
self.reload_data(database_path)
|
||||
|
||||
api.add_resource(Query, "/Archive/Query")
|
||||
api.add_resource(GetPost, "/Archive/GetPost/<id>")
|
||||
api.add_resource(GetPosts, "/Archive/GetPosts")
|
||||
api.add_resource(GetPostsCount, "/Archive/GetPosts/Count")
|
||||
api.add_resource(AccountStats, "/Archive/AccountStats")
|
||||
api.add_resource(SetAction, "/Archive/SetAction")
|
||||
self.app.databases[database_name] = self
|
||||
|
||||
if database_name == "Archive":
|
||||
api.add_resource(Query, "/Archive/Query")
|
||||
api.add_resource(GetPost, "/Archive/GetPost/<id>")
|
||||
api.add_resource(GetPosts, "/Archive/GetPosts")
|
||||
api.add_resource(GetPostsCount, "/Archive/GetPosts/Count")
|
||||
api.add_resource(AccountStats, "/Archive/AccountStats")
|
||||
api.add_resource(SetAction, "/Archive/SetAction")
|
||||
elif database_name == "ArchiveOld":
|
||||
api.add_resource(QueryOld, "/ArchiveOld/Query")
|
||||
api.add_resource(GetPostOld, "/ArchiveOld/GetPost/<id>")
|
||||
api.add_resource(GetPostsOld, "/ArchiveOld/GetPosts")
|
||||
api.add_resource(GetPostsCountOld, "/ArchiveOld/GetPosts/Count")
|
||||
api.add_resource(AccountStatsOld, "/ArchiveOld/AccountStats")
|
||||
api.add_resource(SetActionOld, "/ArchiveOld/SetAction")
|
||||
|
||||
def get_accounts(self):
|
||||
query = f'''
|
||||
|
@ -151,5 +160,5 @@ ORDER BY x_handle'''
|
|||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
def reload_data(self):
|
||||
self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")
|
||||
def reload_data(self, database_path):
|
||||
self.db = DatabaseController(database_path)
|
|
@ -10,4 +10,10 @@ class AccountStats(Resource):
|
|||
def get(self):
|
||||
db : Database = app.databases["Archive"]
|
||||
result = db.get_account_stats()
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class AccountStatsOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
result = db.get_account_stats()
|
||||
return db.wrap_query_response(result)
|
|
@ -11,5 +11,13 @@ class GetPost(Resource):
|
|||
id = int(id)
|
||||
db : Database = app.databases["Archive"]
|
||||
|
||||
result = db.get_post(id)
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class GetPostOld(Resource):
|
||||
def get(self, id):
|
||||
id = int(id)
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.get_post(id)
|
||||
return db.wrap_query_response(result)
|
|
@ -51,5 +51,53 @@ class GetPosts(Resource):
|
|||
real_page = page-1
|
||||
offset = real_page * count
|
||||
|
||||
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order)
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class GetPostsOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
try:
|
||||
count = int(request.args["count"]) if "count" in request.args else 20
|
||||
page = int(request.args["page"]) if "page" in request.args else 1
|
||||
artist = request.args["artist"] if "artist" in request.args else None
|
||||
last_id = int(request.args["last_id"]) if "last_id" in request.args else -1
|
||||
except:
|
||||
response = app.response_class(status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
actions = []
|
||||
if "undecided" in request.args:
|
||||
actions.append(0)
|
||||
if "approved" in request.args:
|
||||
actions.append(1)
|
||||
if "denied" in request.args:
|
||||
actions.append(2)
|
||||
if "hidden" in request.args:
|
||||
actions.append(3)
|
||||
if actions == []:
|
||||
actions = [0,1,2,3]
|
||||
|
||||
ratings = []
|
||||
if "SFW" in request.args:
|
||||
ratings.append("SFW")
|
||||
if "NSFW" in request.args:
|
||||
ratings.append("NSFW")
|
||||
if "NSFL" in request.args:
|
||||
ratings.append("NSFL")
|
||||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
if "random" in request.args:
|
||||
order = "RAND"
|
||||
elif "ascending" in request.args:
|
||||
order = "ASC"
|
||||
else:
|
||||
order = "DESC"
|
||||
|
||||
real_page = page-1
|
||||
offset = real_page * count
|
||||
|
||||
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order)
|
||||
return db.wrap_query_response(result)
|
|
@ -38,5 +38,40 @@ class GetPostsCount(Resource):
|
|||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class GetPostsCountOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
try:
|
||||
artist = request.args["artist"] if "artist" in request.args else None
|
||||
except:
|
||||
response = app.response_class(status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
actions = []
|
||||
if "undecided" in request.args:
|
||||
actions.append(0)
|
||||
if "approved" in request.args:
|
||||
actions.append(1)
|
||||
if "denied" in request.args:
|
||||
actions.append(2)
|
||||
if "hidden" in request.args:
|
||||
actions.append(3)
|
||||
if actions == []:
|
||||
actions = [0,1,2,3]
|
||||
|
||||
ratings = []
|
||||
if "SFW" in request.args:
|
||||
ratings.append("SFW")
|
||||
if "NSFW" in request.args:
|
||||
ratings.append("NSFW")
|
||||
if "NSFL" in request.args:
|
||||
ratings.append("NSFL")
|
||||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings)
|
||||
return db.wrap_query_response(result, mode="text")
|
|
@ -14,4 +14,14 @@ class Query(Resource):
|
|||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class QueryOld(Resource):
|
||||
def post(self):
|
||||
query = request.data.decode("utf-8")
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
|
@ -31,5 +31,32 @@ class SetAction(Resource):
|
|||
if not bypass:
|
||||
query += " AND action_taken = 0"
|
||||
|
||||
result = db.db.run_command(query)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class SetActionOld(Resource):
|
||||
def post(self):
|
||||
data = json.loads(request.data)
|
||||
|
||||
try:
|
||||
bypass = ("allow_override" in data) and data["allow_override"]
|
||||
action = data["action_taken"]
|
||||
if "id_str" in data:
|
||||
id = int(data["id_str"])
|
||||
elif "id" in data:
|
||||
id = data["id"]
|
||||
else:
|
||||
raise Exception("no id (int) or id_str (str) specified")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
response = app.response_class(response=e, status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
query = f"UPDATE x_posts SET action_taken = {action} WHERE id = {id}"
|
||||
if not bypass:
|
||||
query += " AND action_taken = 0"
|
||||
|
||||
result = db.db.run_command(query)
|
||||
return db.wrap_query_response(result, mode="text")
|
Loading…
Reference in New Issue