You've already forked KemoFureApi
							
							additional archive endpoints
This commit is contained in:
		| @@ -2,7 +2,9 @@ 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_post import Archive_GetPost | ||||||
| from .endpoints.get_posts import Archive_GetPosts | from .endpoints.get_posts import Archive_GetPosts | ||||||
|  | from .endpoints.set_action import Archive_SetAction | ||||||
|  |  | ||||||
| class Database: | class Database: | ||||||
|     db : DatabaseController = None |     db : DatabaseController = None | ||||||
| @@ -17,10 +19,26 @@ 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_GetPost, "/Archive/GetPost/<id>") | ||||||
|         api.add_resource(Archive_GetPosts, "/Archive/GetPosts") |         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 {"id": result[0], "id_str": str(result[0]), "error_id": result[1], "action_taken": result[2], "text": result[3], "files": result[4].split(','), "date": result[5], "handle": result[6], "rating": result[7]} | ||||||
|  |  | ||||||
|     def get_posts(self, num_posts, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0): |     def get_posts(self, num_posts, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0): | ||||||
|         num_posts = max(1, min(num_posts, 30)) |         num_posts = max(1, min(num_posts, 100)) | ||||||
|  |  | ||||||
|         where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0" |         where_query = "WHERE x_post_details.id NOT NULL AND x_posts.error_id = 0" | ||||||
|         if last_id != -1: |         if last_id != -1: | ||||||
| @@ -29,7 +47,7 @@ class Database: | |||||||
|             where_query += " AND (" + " OR ".join([f"x_posts.action_taken = {action}" for action in actions_taken]) + ")" |             where_query += " AND (" + " OR ".join([f"x_posts.action_taken = {action}" for action in actions_taken]) + ")" | ||||||
|  |  | ||||||
|         query = f''' |         query = f''' | ||||||
|         SELECT x_posts.id, x_post_details.text, x_post_details.files, accounts.x_handle, x_post_details.date FROM x_posts  |         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 |         LEFT JOIN x_post_details | ||||||
|             ON x_posts.id = x_post_details.id |             ON x_posts.id = x_post_details.id | ||||||
|         LEFT JOIN accounts |         LEFT JOIN accounts | ||||||
| @@ -42,7 +60,7 @@ class Database: | |||||||
|         posts = [] |         posts = [] | ||||||
|         if result is not None: |         if result is not None: | ||||||
|             for result in self.db.run_query(query): |             for result in self.db.run_query(query): | ||||||
|                 posts.append({"id": result[0], "id_str": str(result[0]), "text": result[1], "files": result[2].split(','), "handle": result[3], "date": result[4]}) |                 posts.append({"id": result[0], "id_str": str(result[0]), "action_taken": result[1], "text": result[2], "files": result[3].split(','), "date": result[4], "handle": result[5], "rating": result[6]}) | ||||||
|         return posts |         return posts | ||||||
|  |  | ||||||
|     def reload_data(self): |     def reload_data(self): | ||||||
|   | |||||||
| @@ -18,5 +18,15 @@ class DatabaseController: | |||||||
|             print(e) |             print(e) | ||||||
|             return None |             return None | ||||||
|          |          | ||||||
|  |     def run_command(self, commnd): | ||||||
|  |         try: | ||||||
|  |             self.cursor.execute(commnd) | ||||||
|  |             result = self.cursor.rowcount | ||||||
|  |             self.conn.commit() | ||||||
|  |             return result > 0 | ||||||
|  |         except Exception as e: | ||||||
|  |             print(e) | ||||||
|  |             return False | ||||||
|  |  | ||||||
|     def close(self): |     def close(self): | ||||||
|         self.conn.close() |         self.conn.close() | ||||||
							
								
								
									
										27
									
								
								modules/Archive/endpoints/get_post.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								modules/Archive/endpoints/get_post.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | 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_GetPost(Resource): | ||||||
|  |     def get(self, id): | ||||||
|  |         id = int(id) | ||||||
|  |         db : Database = app.databases["Archive"] | ||||||
|  |  | ||||||
|  |         result = db.get_post(id) | ||||||
|  |  | ||||||
|  |         if result is None: | ||||||
|  |             response = app.response_class(status=404) | ||||||
|  |         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    | ||||||
| @@ -11,7 +11,7 @@ class Archive_GetPosts(Resource): | |||||||
|     def get(self): |     def get(self): | ||||||
|         db : Database = app.databases["Archive"] |         db : Database = app.databases["Archive"] | ||||||
|         try: |         try: | ||||||
|             count = int(request.args["count"]) if "count" in request.args else 10 |             count = int(request.args["count"]) if "count" in request.args else 20 | ||||||
|             page = int(request.args["page"]) if "page" in request.args else 1 |             page = int(request.args["page"]) if "page" in request.args else 1 | ||||||
|             last_id = int(request.args["last_id"]) if "last_id" in request.args else -1 |             last_id = int(request.args["last_id"]) if "last_id" in request.args else -1 | ||||||
|         except: |         except: | ||||||
|   | |||||||
							
								
								
									
										36
									
								
								modules/Archive/endpoints/set_action.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								modules/Archive/endpoints/set_action.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | |||||||
|  | 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_SetAction(Resource): | ||||||
|  |     def post(self): | ||||||
|  |         data = request.json | ||||||
|  |  | ||||||
|  |         try: | ||||||
|  |             bypass = "allow_override" in data and data["allow_override"] | ||||||
|  |             action = data["action_taken"] | ||||||
|  |             id = data["id"] | ||||||
|  |         except Exception as e: | ||||||
|  |             print(e) | ||||||
|  |             response = app.response_class(response=e, status=400) | ||||||
|  |  | ||||||
|  |         db : Database = app.databases["Archive"] | ||||||
|  |         query = f"UPDATE x_posts SET action_taken = {action} WHERE id = {id}" | ||||||
|  |         if not bypass: | ||||||
|  |             query += " AND action_taken = 0" | ||||||
|  |  | ||||||
|  |         result = db.db.run_command(query) | ||||||
|  |  | ||||||
|  |         response = app.response_class( | ||||||
|  |             response=result, | ||||||
|  |             status=200, | ||||||
|  |             mimetype='application/json' | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         response.headers.add("Access-Control-Allow-Origin", "*") | ||||||
|  |         return response    | ||||||
		Reference in New Issue
	
	Block a user