KemoFureApi/modules/Archive/database.py

81 lines
3.4 KiB
Python

import json
from .databaseController import DatabaseController
from .endpoints.query import Archive_Query
from .endpoints.get_post import Archive_GetPost
from .endpoints.get_posts import Archive_GetPosts
from .endpoints.set_action import Archive_SetAction
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
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")
api.add_resource(Archive_GetPost, "/Archive/GetPost/<id>")
api.add_resource(Archive_GetPosts, "/Archive/GetPosts")
api.add_resource(Archive_SetAction, "/Archive/SetAction")
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])
def get_posts(self, num_posts, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"]):
num_posts = max(1, min(num_posts, 100))
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]:
where_query += " AND (" + " OR ".join([f"x_posts.action_taken = {action}" for action in actions_taken]) + ")"
if include_ratings != ["SFW", "NSFW"]:
where_query += " AND (" + " OR ".join([f'account.rating = "{rating}"' for rating in include_ratings]) + ")"
query = f'''
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
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}
ORDER BY x_posts.id DESC
LIMIT {num_posts} OFFSET {offset}'''
result = self.db.run_query(query)
posts = []
if result is not None:
for result in self.db.run_query(query):
posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6]))
return posts
def reload_data(self):
self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")