From 9f093ce1ee8c4155af4b67f4c7fc58b6df726ea3 Mon Sep 17 00:00:00 2001 From: katboi01 Date: Mon, 30 Dec 2024 09:35:57 +0100 Subject: [PATCH] added get_posts endpoint --- modules/Archive/database.py | 29 ++++++++++++++++++++ modules/Archive/endpoints/get_posts.py | 38 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 modules/Archive/endpoints/get_posts.py diff --git a/modules/Archive/database.py b/modules/Archive/database.py index 03b8a78..6aa9535 100644 --- a/modules/Archive/database.py +++ b/modules/Archive/database.py @@ -1,6 +1,8 @@ import json + from .databaseController import DatabaseController from .endpoints.query import Archive_Query +from .endpoints.get_posts import Archive_GetPosts class Database: db : DatabaseController = None @@ -15,6 +17,33 @@ class Database: app.databases["Archive"] = self api.add_resource(Archive_Query, "/Archive/Query") + api.add_resource(Archive_GetPosts, "/Archive/GetPosts") + def get_posts(self, num_posts, actions_taken = [0, 1, 2], last_id = -1): + num_posts = min(num_posts, 30) + + where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0" + if last_id != -1: + where_query += " AND x_posts.id < last_id" + if actions_taken != [0, 1, 2]: + where_query += "AND (" + " OR ".join([f"x_posts.action_taken = {action}" for action in actions_taken]) + ")" + + query = f''' + SELECT x_posts.id, x_post_details.files, accounts.x_handle 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}''' + + print(query) + + posts = [] + for result in self.db.run_query(query): + posts.append({"id": result[0], "files": result[1].split(','), "handle": result[2]}) + return posts + def reload_data(self): self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db") \ No newline at end of file diff --git a/modules/Archive/endpoints/get_posts.py b/modules/Archive/endpoints/get_posts.py new file mode 100644 index 0000000..b5dc48e --- /dev/null +++ b/modules/Archive/endpoints/get_posts.py @@ -0,0 +1,38 @@ +from __future__ import annotations +import json +from flask_restful import Resource +from flask import current_app as app, jsonify, request +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from modules.Archive.database import Database + +class Archive_GetPosts(Resource): + def get(self): + db : Database = app.databases["Archive"] + count = request.args["count"] if "count" in request.args else 10 + last_id = request.args["last_id"] if "last_id" in request.args else -1 + + param = [] + if "undecided" in request.args: + param.append(0) + if "approved" in request.args: + param.append(1) + if "denied" in request.args: + param.append(2) + if param == []: + param = [0,1,2] + + result = db.get_posts(count, param, last_id) + + if result is None: + response = app.response_class(status=400) + else: + response = app.response_class( + response=json.dumps(result, ensure_ascii=False, indent=1), + status=200, + mimetype='application/json' + ) + + response.headers.add("Access-Control-Allow-Origin", "*") + return response \ No newline at end of file