From 16c7792b7f8d5187df917c70be4c2469c36959c8 Mon Sep 17 00:00:00 2001 From: katboi01 Date: Wed, 15 Jan 2025 13:56:06 +0100 Subject: [PATCH] Added old archive endpoints --- app.py | 3 +- modules/Archive/database.py | 47 ++++++++++++--------- modules/Archive/endpoints/account_stats.py | 6 +++ modules/Archive/endpoints/post.py | 8 ++++ modules/Archive/endpoints/posts.py | 48 ++++++++++++++++++++++ modules/Archive/endpoints/posts_count.py | 35 ++++++++++++++++ modules/Archive/endpoints/query.py | 10 +++++ modules/Archive/endpoints/set_action.py | 27 ++++++++++++ 8 files changed, 164 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index 3369474..bafbf99 100644 --- a/app.py +++ b/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) \ No newline at end of file diff --git a/modules/Archive/database.py b/modules/Archive/database.py index 95e7511..7e0f9d0 100644 --- a/modules/Archive/database.py +++ b/modules/Archive/database.py @@ -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/") - 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/") + 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/") + 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") \ No newline at end of file + def reload_data(self, database_path): + self.db = DatabaseController(database_path) \ No newline at end of file diff --git a/modules/Archive/endpoints/account_stats.py b/modules/Archive/endpoints/account_stats.py index b6aa1a4..d8697ba 100644 --- a/modules/Archive/endpoints/account_stats.py +++ b/modules/Archive/endpoints/account_stats.py @@ -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) \ No newline at end of file diff --git a/modules/Archive/endpoints/post.py b/modules/Archive/endpoints/post.py index d83d432..fc609ff 100644 --- a/modules/Archive/endpoints/post.py +++ b/modules/Archive/endpoints/post.py @@ -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) \ No newline at end of file diff --git a/modules/Archive/endpoints/posts.py b/modules/Archive/endpoints/posts.py index 05d235c..21885a8 100644 --- a/modules/Archive/endpoints/posts.py +++ b/modules/Archive/endpoints/posts.py @@ -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) \ No newline at end of file diff --git a/modules/Archive/endpoints/posts_count.py b/modules/Archive/endpoints/posts_count.py index 68712f4..01bf0ed 100644 --- a/modules/Archive/endpoints/posts_count.py +++ b/modules/Archive/endpoints/posts_count.py @@ -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") \ No newline at end of file diff --git a/modules/Archive/endpoints/query.py b/modules/Archive/endpoints/query.py index 61a96cd..2e8f3a9 100644 --- a/modules/Archive/endpoints/query.py +++ b/modules/Archive/endpoints/query.py @@ -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) \ No newline at end of file diff --git a/modules/Archive/endpoints/set_action.py b/modules/Archive/endpoints/set_action.py index eb6da8c..cb8207d 100644 --- a/modules/Archive/endpoints/set_action.py +++ b/modules/Archive/endpoints/set_action.py @@ -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") \ No newline at end of file