KemoFureApi/modules/Archive/database.py

153 lines
6.5 KiB
Python
Raw Normal View History

2025-01-12 17:27:10 +08:00
import json
from flask import Flask
2025-01-16 04:28:49 +08:00
2024-12-26 22:54:55 +08:00
from .databaseController import DatabaseController
2025-01-15 20:56:06 +08:00
from .endpoints.query import Query, QueryOld
2025-01-17 19:03:00 +08:00
from .endpoints.command import Command, CommandOld
from .endpoints.commands import Commands, CommandsOld
2025-01-15 20:56:06 +08:00
from .endpoints.post import GetPost, GetPostOld
from .endpoints.posts import GetPosts, GetPostsOld
2025-01-16 04:28:49 +08:00
from .endpoints.new_query import NewQuery, NewQueryOld
2025-01-15 20:56:06 +08:00
from .endpoints.set_action import SetAction, SetActionOld
from .endpoints.posts_count import GetPostsCount, GetPostsCountOld
from .endpoints.account_stats import AccountStats, AccountStatsOld
2025-01-02 19:05:25 +08:00
2024-12-26 22:54:55 +08:00
class Database:
db : DatabaseController = None
2025-01-12 17:27:10 +08:00
app : Flask = None
2024-12-26 22:54:55 +08:00
2025-01-15 20:56:06 +08:00
def __init__(self, api, database_name, database_path) -> None:
2025-01-12 17:27:10 +08:00
self.app = api.app
2024-12-26 22:54:55 +08:00
2025-01-15 20:56:06 +08:00
if database_name in self.app.databases:
del self.app.databases[database_name]
self.reload_data(database_path)
self.app.databases[database_name] = self
if database_name == "Archive":
api.add_resource(Query, "/Archive/Query")
2025-01-16 04:28:49 +08:00
api.add_resource(NewQuery, "/Archive/NewQuery")
2025-01-17 19:03:00 +08:00
api.add_resource(Command, "/Archive/Command")
api.add_resource(Commands, "/Archive/Commands")
2025-01-15 20:56:06 +08:00
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")
2025-01-16 04:28:49 +08:00
api.add_resource(NewQueryOld, "/ArchiveOld/NewQuery")
2025-01-17 19:03:00 +08:00
api.add_resource(CommandOld, "/Archive/Command")
api.add_resource(CommandsOld, "/Archive/Commands")
2025-01-15 20:56:06 +08:00
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")
2024-12-26 22:54:55 +08:00
2025-01-12 17:27:10 +08:00
def get_accounts(self):
query = f'''
SELECT x_handle, id FROM accounts
ORDER BY x_handle ASC'''
2025-01-16 04:28:49 +08:00
return self.db.run_query(query)
2025-01-12 17:27:10 +08:00
def get_account_stats(self):
query = '''SELECT x_handle,
SUM(IIF(x_posts.action_taken = 0, 1, 0)) as undecided,
SUM(IIF(x_posts.action_taken = 1, 1, 0)) as approved,
SUM(IIF(x_posts.action_taken = 2, 1, 0)) as deleted,
SUM(IIF(x_posts.action_taken = 3, 1, 0)) as hidden,
SUM(IIF(x_post_details.id is NULL, 1, 0)) as invalid
FROM accounts
LEFT JOIN x_posts on accounts.id = x_posts.account_id
2025-01-12 17:27:10 +08:00
LEFT JOIN x_post_details on x_post_details.id = x_posts.id
WHERE accounts.download_mode != 4
2025-01-12 17:27:10 +08:00
GROUP BY account_id
ORDER BY x_handle'''
2025-01-16 04:28:49 +08:00
return self.db.run_query(query)
2025-01-12 17:27:10 +08:00
2025-01-02 19:05:25 +08:00
def get_post(self, id):
2025-01-16 04:28:49 +08:00
query = f'''SELECT x_posts.id, cast(x_posts.id as TEXT) as id_str, x_posts.error_id, x_posts.action_taken, x_post_details.text, x_post_details.files, x_post_details.date, accounts.x_handle, accounts.rating from x_posts
2025-01-02 19:05:25 +08:00
LEFT JOIN x_post_details
ON x_posts.id = x_post_details.id
LEFT JOIN accounts
2025-01-16 04:28:49 +08:00
ON x_posts.account_id = accounts.id WHERE x_posts.id = {id}
LIMIT 1'''
2025-01-02 19:05:25 +08:00
result = self.db.run_query(query)
if len(result) == 0:
return None
else:
#return most recent post
2025-01-16 04:28:49 +08:00
return result[-1]
2025-01-02 19:05:25 +08:00
2025-01-12 00:45:49 +08:00
def build_where_query(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]):
2024-12-30 16:35:57 +08:00
where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0"
if last_id != -1:
where_query += f" AND x_posts.id < {last_id}"
if actions_taken != [0, 1, 2, 3]:
2024-12-30 16:35:57 +08:00
where_query += " AND (" + " OR ".join([f"x_posts.action_taken = {action}" for action in actions_taken]) + ")"
2025-01-06 01:49:14 +08:00
if include_ratings != ["SFW", "NSFW", "NSFL"]:
2025-01-03 05:12:35 +08:00
where_query += " AND (" + " OR ".join([f'accounts.rating = "{rating}"' for rating in include_ratings]) + ")"
2025-01-03 06:19:52 +08:00
if artist is not None and artist != "":
where_query += f' AND accounts.x_handle = "{artist}"'
2025-01-12 00:45:49 +08:00
return where_query
def get_posts_count(self, artist, actions_taken = [0, 1, 2, 3], last_id = -1, include_ratings = ["SFW", "NSFW"]):
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
query = f'''
2025-01-16 04:28:49 +08:00
SELECT count(*) as count FROM x_posts
2025-01-12 00:45:49 +08:00
LEFT JOIN x_post_details
ON x_posts.id = x_post_details.id
LEFT JOIN accounts
ON x_posts.account_id = accounts.id
{where_query}'''
result = self.db.run_query(query)
2025-01-16 04:28:49 +08:00
return result[0]["count"]
2025-01-12 00:45:49 +08:00
2025-01-12 01:32:03 +08:00
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"], order = "DESC"):
2025-01-12 00:45:49 +08:00
num_posts = max(1, min(num_posts, 100))
2025-01-12 01:32:03 +08:00
order_by = "RANDOM()" if order == "RAND" else "x_posts.id ASC" if order == "ASC" else "x_posts.id DESC"
2025-01-12 00:45:49 +08:00
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
2024-12-30 16:35:57 +08:00
query = f'''
2025-01-16 04:28:49 +08:00
SELECT x_posts.id, cast(x_posts.id as TEXT) as id_str, x_posts.action_taken, x_post_details.text, x_post_details.files, x_post_details.date, accounts.x_handle, accounts.rating FROM x_posts
2024-12-30 16:35:57 +08:00
LEFT JOIN x_post_details
ON x_posts.id = x_post_details.id
LEFT JOIN accounts
ON x_posts.account_id = accounts.id
{where_query}
2025-01-12 01:32:03 +08:00
ORDER BY {order_by}
2024-12-30 16:35:57 +08:00
LIMIT {num_posts} OFFSET {offset}'''
2025-01-16 04:28:49 +08:00
return self.db.run_query(query)
2024-12-30 16:35:57 +08:00
2025-01-12 17:27:10 +08:00
def wrap_query_response(self, result, mode = "json"):
if result is None:
response = self.app.response_class(status=400)
else:
if mode == "json":
response = self.app.response_class(
response=json.dumps(result, ensure_ascii=False, indent=1),
status=200,
mimetype='application/json'
)
elif mode == "text":
response = self.app.response_class(
response=str(result),
status=200,
mimetype='text/plain'
)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
2025-01-15 20:56:06 +08:00
def reload_data(self, database_path):
self.db = DatabaseController(database_path)