added get_posts endpoint
This commit is contained in:
parent
76b6c6cb79
commit
c89288c105
|
@ -1,6 +1,8 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from .databaseController import DatabaseController
|
from .databaseController import DatabaseController
|
||||||
from .endpoints.query import Archive_Query
|
from .endpoints.query import Archive_Query
|
||||||
|
from .endpoints.get_posts import Archive_GetPosts
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
db : DatabaseController = None
|
db : DatabaseController = None
|
||||||
|
@ -15,6 +17,35 @@ class Database:
|
||||||
app.databases["Archive"] = self
|
app.databases["Archive"] = self
|
||||||
|
|
||||||
api.add_resource(Archive_Query, "/Archive/Query")
|
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)
|
||||||
|
|
||||||
|
result = self.db.run_query(query)
|
||||||
|
posts = []
|
||||||
|
if result is not None:
|
||||||
|
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):
|
def reload_data(self):
|
||||||
self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")
|
self.db = DatabaseController("/home/pi/python/Katbots/JapariArchive/database.db")
|
|
@ -14,7 +14,8 @@ class DatabaseController:
|
||||||
self.cursor.execute(query)
|
self.cursor.execute(query)
|
||||||
results = self.cursor.fetchall()
|
results = self.cursor.fetchall()
|
||||||
return results
|
return results
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue