KemoFureApi/modules/Archive/database.py

103 lines
4.5 KiB
Python
Raw Normal View History

2024-12-26 22:54:55 +08:00
from .databaseController import DatabaseController
from .endpoints.query import Archive_Query
2025-01-02 19:05:25 +08:00
from .endpoints.get_post import Archive_GetPost
2024-12-30 16:35:57 +08:00
from .endpoints.get_posts import Archive_GetPosts
2025-01-02 19:05:25 +08:00
from .endpoints.set_action import Archive_SetAction
2025-01-12 00:45:49 +08:00
from .endpoints.get_posts_count import Archive_GetPostsCount
2025-01-02 19:05:25 +08:00
def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating):
if files == "" or files is None:
files = []
else:
files = files.split(',')
dict = {"id": id, "id_str": str(id), "error_id": error_id, "action_taken": action_taken, "text": text, "files": files, "date": date, "handle": x_handle, "rating": x_rating}
if error_id == -1:
del(dict["error_id"])
if action_taken == -1:
del(dict["action_taken"])
return dict
2024-12-26 22:54:55 +08:00
class Database:
db : DatabaseController = None
def __init__(self, api) -> None:
app = api.app
if "Archive" in app.databases:
del app.databases["Archive"]
self.reload_data()
app.databases["Archive"] = self
api.add_resource(Archive_Query, "/Archive/Query")
2025-01-02 19:05:25 +08:00
api.add_resource(Archive_GetPost, "/Archive/GetPost/<id>")
2024-12-30 16:35:57 +08:00
api.add_resource(Archive_GetPosts, "/Archive/GetPosts")
2025-01-12 00:45:49 +08:00
api.add_resource(Archive_GetPostsCount, "/Archive/GetPosts/Count")
2025-01-02 19:05:25 +08:00
api.add_resource(Archive_SetAction, "/Archive/SetAction")
2024-12-26 22:54:55 +08:00
2025-01-02 19:05:25 +08:00
def get_post(self, id):
query = f'''SELECT x_posts.id, 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
LEFT JOIN x_post_details
ON x_posts.id = x_post_details.id
LEFT JOIN accounts
ON x_posts.account_id = accounts.id WHERE x_posts.id = {id}'''
result = self.db.run_query(query)
if len(result) == 0:
return None
else:
#return most recent post
result = result[-1]
return get_post_dictionary(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7])
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'''
SELECT count(*) FROM x_posts
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)
return result[0][0]
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-02 19:05:25 +08:00
SELECT x_posts.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
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-12 01:32:03 +08:00
results = self.db.run_query(query)
2024-12-30 16:35:57 +08:00
posts = []
2025-01-12 01:32:03 +08:00
if results is not None:
for result in results:
2025-01-02 19:05:25 +08:00
posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6]))
2024-12-30 16:35:57 +08:00
return posts
2024-12-26 22:54:55 +08:00
def reload_data(self):
self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")