You've already forked KemoFureApi
							
							added account post count endpoint
This commit is contained in:
		| @@ -1,9 +1,11 @@ | |||||||
|  | import json | ||||||
|  | from flask import Flask | ||||||
| from .databaseController import DatabaseController | from .databaseController import DatabaseController | ||||||
| from .endpoints.query import Archive_Query | from .endpoints.query import Query | ||||||
| from .endpoints.get_post import Archive_GetPost | from .endpoints.get_post import GetPost | ||||||
| from .endpoints.get_posts import Archive_GetPosts | from .endpoints.get_posts import GetPosts | ||||||
| from .endpoints.set_action import Archive_SetAction | from .endpoints.set_action import SetAction | ||||||
| from .endpoints.get_posts_count import Archive_GetPostsCount | from .endpoints.get_posts_count import GetPostsCount | ||||||
|  |  | ||||||
| def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating): | def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, x_rating): | ||||||
|     if files == "" or files is None: |     if files == "" or files is None: | ||||||
| @@ -19,22 +21,49 @@ def get_post_dictionary(id, error_id, action_taken, text, files, date, x_handle, | |||||||
|  |  | ||||||
| class Database: | class Database: | ||||||
|     db : DatabaseController = None |     db : DatabaseController = None | ||||||
|  |     app : Flask = None | ||||||
|  |  | ||||||
|     def __init__(self, api) -> None: |     def __init__(self, api) -> None: | ||||||
|         app = api.app |         self.app = api.app | ||||||
|         if "Archive" in app.databases: |         if "Archive" in self.app.databases: | ||||||
|             del app.databases["Archive"] |             del self.app.databases["Archive"] | ||||||
|  |  | ||||||
|         self.reload_data() |         self.reload_data() | ||||||
|  |  | ||||||
|         app.databases["Archive"] = self |         self.app.databases["Archive"] = self | ||||||
|  |  | ||||||
|         api.add_resource(Archive_Query, "/Archive/Query") |         api.add_resource(Query, "/Archive/Query") | ||||||
|         api.add_resource(Archive_GetPost, "/Archive/GetPost/<id>") |         api.add_resource(GetPost, "/Archive/GetPost/<id>") | ||||||
|         api.add_resource(Archive_GetPosts, "/Archive/GetPosts") |         api.add_resource(GetPosts, "/Archive/GetPosts") | ||||||
|         api.add_resource(Archive_GetPostsCount, "/Archive/GetPosts/Count") |         api.add_resource(GetPostsCount, "/Archive/GetPosts/Count") | ||||||
|         api.add_resource(Archive_SetAction, "/Archive/SetAction") |         api.add_resource(SetAction, "/Archive/SetAction") | ||||||
|    |    | ||||||
|  |     def get_accounts(self): | ||||||
|  |         query = f''' | ||||||
|  |         SELECT x_handle, id FROM accounts  | ||||||
|  |         ORDER BY x_handle ASC''' | ||||||
|  |  | ||||||
|  |         results = self.db.run_query(query) | ||||||
|  |         accounts = [] | ||||||
|  |         if results is not None: | ||||||
|  |             accounts = [{"x_handle": result[0], "id": result[1]} for result in results] | ||||||
|  |         return accounts | ||||||
|  |  | ||||||
|  |     def get_account_stats(self): | ||||||
|  |         query = '''SELECT x_handle, | ||||||
|  | 	SUM(IIF(x_posts.action_taken = 0, 1, 0)) as undecided, | ||||||
|  | 	SUM(IIF(x_posts.action_taken = 1, 1, 0)) as approved, | ||||||
|  | 	SUM(IIF(x_posts.action_taken = 2, 1, 0)) as deleted, | ||||||
|  | 	SUM(IIF(x_posts.action_taken = 3, 1, 0)) as hidden, | ||||||
|  | 	SUM(IIF(x_post_details.id is NULL, 1, 0)) as invalid | ||||||
|  | FROM x_posts  | ||||||
|  | 	LEFT JOIN accounts on accounts.id = x_posts.account_id  | ||||||
|  | 	LEFT JOIN x_post_details on x_post_details.id = x_posts.id | ||||||
|  | GROUP BY account_id  | ||||||
|  | ORDER BY x_handle''' | ||||||
|  |         results = self.db.run_query(query) | ||||||
|  |         return [{"x_handle": result[0], "undecided": result[1], "approved": result[2], "deleted": result[3], "hidden": result[4], "invalid": result[5]} for result in results] | ||||||
|  |  | ||||||
|     def get_post(self, id): |     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 |         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 |         LEFT JOIN x_post_details | ||||||
| @@ -99,5 +128,25 @@ class Database: | |||||||
|                 posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6])) |                 posts.append(get_post_dictionary(result[0], -1, result[1], result[2], result[3], result[4], result[5], result[6])) | ||||||
|         return posts |         return posts | ||||||
|  |  | ||||||
|  |     def wrap_query_response(self, result, mode = "json"): | ||||||
|  |         if result is None: | ||||||
|  |             response = self.app.response_class(status=400) | ||||||
|  |         else: | ||||||
|  |             if mode == "json": | ||||||
|  |                 response = self.app.response_class( | ||||||
|  |                     response=json.dumps(result, ensure_ascii=False, indent=1), | ||||||
|  |                     status=200, | ||||||
|  |                     mimetype='application/json' | ||||||
|  |             ) | ||||||
|  |             elif mode == "text": | ||||||
|  |                 response = self.app.response_class( | ||||||
|  |                     response=str(result), | ||||||
|  |                     status=200, | ||||||
|  |                     mimetype='text/plain' | ||||||
|  |             ) | ||||||
|  |                  | ||||||
|  |         response.headers.add("Access-Control-Allow-Origin", "*") | ||||||
|  |         return response | ||||||
|  |  | ||||||
|     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") | ||||||
							
								
								
									
										13
									
								
								modules/Archive/endpoints/get_accounts_post_count.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								modules/Archive/endpoints/get_accounts_post_count.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | from __future__ import annotations | ||||||
|  | from flask_restful import Resource | ||||||
|  | from flask import current_app as app | ||||||
|  | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
|  | if TYPE_CHECKING: | ||||||
|  |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
|  | class AccountsPostCount(Resource): | ||||||
|  |     def get(self): | ||||||
|  |         db : Database = app.databases["Archive"] | ||||||
|  |         result = db.get_account_stats() | ||||||
|  |         return db.wrap_query_response(result) | ||||||
| @@ -1,27 +1,15 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
| import json |  | ||||||
| from flask_restful import Resource | from flask_restful import Resource | ||||||
| from flask import current_app as app, jsonify, request | from flask import current_app as app | ||||||
| from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     from modules.Archive.database import Database |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
| class Archive_GetPost(Resource): | class GetPost(Resource): | ||||||
|     def get(self, id): |     def get(self, id): | ||||||
|         id = int(id) |         id = int(id) | ||||||
|         db : Database = app.databases["Archive"] |         db : Database = app.databases["Archive"] | ||||||
|  |  | ||||||
|         result = db.get_post(id) |         result = db.get_post(id) | ||||||
|  |         return db.wrap_query_response(result)  | ||||||
|         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    |  | ||||||
| @@ -1,13 +1,12 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
| import json |  | ||||||
| from flask_restful import Resource | from flask_restful import Resource | ||||||
| from flask import current_app as app, jsonify, request | from flask import current_app as app, request | ||||||
| from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     from modules.Archive.database import Database |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
| class Archive_GetPosts(Resource): | class GetPosts(Resource): | ||||||
|     def get(self): |     def get(self): | ||||||
|         db : Database = app.databases["Archive"] |         db : Database = app.databases["Archive"] | ||||||
|         try: |         try: | ||||||
| @@ -53,15 +52,4 @@ class Archive_GetPosts(Resource): | |||||||
|         offset = real_page * count |         offset = real_page * count | ||||||
|  |  | ||||||
|         result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order) |         result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order) | ||||||
|  |         return db.wrap_query_response(result) | ||||||
|         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    |  | ||||||
| @@ -1,13 +1,12 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
| import json |  | ||||||
| from flask_restful import Resource | from flask_restful import Resource | ||||||
| from flask import current_app as app, jsonify, request | from flask import current_app as app, request | ||||||
| from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     from modules.Archive.database import Database |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
| class Archive_GetPostsCount(Resource): | class GetPostsCount(Resource): | ||||||
|     def get(self): |     def get(self): | ||||||
|         db : Database = app.databases["Archive"] |         db : Database = app.databases["Archive"] | ||||||
|         try: |         try: | ||||||
| @@ -40,15 +39,4 @@ class Archive_GetPostsCount(Resource): | |||||||
|             ratings = ["SFW", "NSFW"] |             ratings = ["SFW", "NSFW"] | ||||||
|  |  | ||||||
|         result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings) |         result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings) | ||||||
|  |         return db.wrap_query_response(result, mode="text")  | ||||||
|         if result is None: |  | ||||||
|             response = app.response_class(status=400) |  | ||||||
|         else: |  | ||||||
|             response = app.response_class( |  | ||||||
|                 response=str(result), |  | ||||||
|                 status=200, |  | ||||||
|                 mimetype='text/plain' |  | ||||||
|             ) |  | ||||||
|  |  | ||||||
|         response.headers.add("Access-Control-Allow-Origin", "*") |  | ||||||
|         return response    |  | ||||||
| @@ -1,13 +1,12 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
| import json |  | ||||||
| from flask_restful import Resource | from flask_restful import Resource | ||||||
| from flask import current_app as app, jsonify, request | from flask import current_app as app, request | ||||||
| from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     from modules.Archive.database import Database |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
| class Archive_Query(Resource): | class Query(Resource): | ||||||
|     def post(self): |     def post(self): | ||||||
|         query = request.data.decode("utf-8") |         query = request.data.decode("utf-8") | ||||||
|          |          | ||||||
| @@ -15,14 +14,4 @@ class Archive_Query(Resource): | |||||||
|  |  | ||||||
|         result = db.db.run_query(query) |         result = db.db.run_query(query) | ||||||
|  |  | ||||||
|         if result is None: |         return db.wrap_query_response(result) | ||||||
|             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    |  | ||||||
| @@ -1,13 +1,13 @@ | |||||||
| from __future__ import annotations | from __future__ import annotations | ||||||
| import json | import json | ||||||
| from flask_restful import Resource | from flask_restful import Resource | ||||||
| from flask import current_app as app, jsonify, request | from flask import current_app as app, request | ||||||
| from typing import TYPE_CHECKING | from typing import TYPE_CHECKING | ||||||
|  |  | ||||||
| if TYPE_CHECKING: | if TYPE_CHECKING: | ||||||
|     from modules.Archive.database import Database |     from modules.Archive.database import Database | ||||||
|  |  | ||||||
| class Archive_SetAction(Resource): | class SetAction(Resource): | ||||||
|     def post(self): |     def post(self): | ||||||
|         data = json.loads(request.data) |         data = json.loads(request.data) | ||||||
|  |  | ||||||
| @@ -32,12 +32,4 @@ class Archive_SetAction(Resource): | |||||||
|             query += " AND action_taken = 0" |             query += " AND action_taken = 0" | ||||||
|  |  | ||||||
|         result = db.db.run_command(query) |         result = db.db.run_command(query) | ||||||
|  |         return db.wrap_query_response(result, mode="text") | ||||||
|         response = app.response_class( |  | ||||||
|             response=str(result), |  | ||||||
|             status=200, |  | ||||||
|             mimetype='text/plain' |  | ||||||
|         ) |  | ||||||
|  |  | ||||||
|         response.headers.add("Access-Control-Allow-Origin", "*") |  | ||||||
|         return response    |  | ||||||
		Reference in New Issue
	
	Block a user