You've already forked KemoFureApi
removed ArchiveOld, added more data to posts, added DMM proxy endpoint
This commit is contained in:
@@ -3,50 +3,39 @@ from flask import Flask
|
||||
|
||||
|
||||
from .databaseController import DatabaseController
|
||||
from .endpoints.query import Query, QueryOld
|
||||
from .endpoints.command import Command, CommandOld
|
||||
from .endpoints.commands import Commands, CommandsOld
|
||||
from .endpoints.post import GetPost, GetPostOld
|
||||
from .endpoints.posts import GetPosts, GetPostsOld
|
||||
from .endpoints.new_query import NewQuery, NewQueryOld
|
||||
from .endpoints.set_action import SetAction, SetActionOld
|
||||
from .endpoints.posts_count import GetPostsCount, GetPostsCountOld
|
||||
from .endpoints.account_stats import AccountStats, AccountStatsOld
|
||||
from .endpoints.query import Query
|
||||
from .endpoints.command import Command
|
||||
from .endpoints.commands import Commands
|
||||
from .endpoints.post import GetPost
|
||||
from .endpoints.posts import GetPosts
|
||||
from .endpoints.new_query import NewQuery
|
||||
from .endpoints.set_action import SetAction
|
||||
from .endpoints.posts_count import GetPostsCount
|
||||
from .endpoints.account_stats import AccountStats
|
||||
|
||||
class Database:
|
||||
db : DatabaseController = None
|
||||
app : Flask = None
|
||||
|
||||
def __init__(self, api, database_name, database_path) -> None:
|
||||
def __init__(self, api) -> None:
|
||||
self.app = api.app
|
||||
|
||||
if database_name in self.app.databases:
|
||||
del self.app.databases[database_name]
|
||||
if "Archive" in self.app.databases:
|
||||
del self.app.databases["Archive"]
|
||||
|
||||
self.reload_data(database_path)
|
||||
self.reload_data("/home/pi/python/Katbots/JapariArchive/database.db")
|
||||
|
||||
self.app.databases[database_name] = self
|
||||
self.app.databases["Archive"] = self
|
||||
|
||||
if database_name == "Archive":
|
||||
api.add_resource(Query, "/Archive/Query")
|
||||
api.add_resource(NewQuery, "/Archive/NewQuery")
|
||||
api.add_resource(Command, "/Archive/Command")
|
||||
api.add_resource(Commands, "/Archive/Commands")
|
||||
api.add_resource(GetPost, "/Archive/GetPost/<id>")
|
||||
api.add_resource(GetPosts, "/Archive/GetPosts")
|
||||
api.add_resource(GetPostsCount, "/Archive/GetPosts/Count")
|
||||
api.add_resource(AccountStats, "/Archive/AccountStats")
|
||||
api.add_resource(SetAction, "/Archive/SetAction")
|
||||
elif database_name == "ArchiveOld":
|
||||
api.add_resource(QueryOld, "/ArchiveOld/Query")
|
||||
api.add_resource(NewQueryOld, "/ArchiveOld/NewQuery")
|
||||
api.add_resource(CommandOld, "/Archive/Command")
|
||||
api.add_resource(CommandsOld, "/Archive/Commands")
|
||||
api.add_resource(GetPostOld, "/ArchiveOld/GetPost/<id>")
|
||||
api.add_resource(GetPostsOld, "/ArchiveOld/GetPosts")
|
||||
api.add_resource(GetPostsCountOld, "/ArchiveOld/GetPosts/Count")
|
||||
api.add_resource(AccountStatsOld, "/ArchiveOld/AccountStats")
|
||||
api.add_resource(SetActionOld, "/ArchiveOld/SetAction")
|
||||
api.add_resource(Query, "/Archive/Query")
|
||||
api.add_resource(NewQuery, "/Archive/NewQuery")
|
||||
api.add_resource(Command, "/Archive/Command")
|
||||
api.add_resource(Commands, "/Archive/Commands")
|
||||
api.add_resource(GetPost, "/Archive/GetPost/<id>")
|
||||
api.add_resource(GetPosts, "/Archive/GetPosts")
|
||||
api.add_resource(GetPostsCount, "/Archive/GetPosts/Count")
|
||||
api.add_resource(AccountStats, "/Archive/AccountStats")
|
||||
api.add_resource(SetAction, "/Archive/SetAction")
|
||||
|
||||
def get_accounts(self):
|
||||
query = f'''
|
||||
@@ -71,7 +60,7 @@ ORDER BY x_handle'''
|
||||
return self.db.run_query(query)
|
||||
|
||||
def get_post(self, id):
|
||||
query = f'''SELECT x_posts.id, cast(x_posts.id as TEXT) as id_str, 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, cast(x_posts.id as TEXT) as id_str, x_posts.error_id, x_posts.action_taken, x_posts.is_saved, 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
|
||||
@@ -118,7 +107,7 @@ ORDER BY x_handle'''
|
||||
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
|
||||
|
||||
query = f'''
|
||||
SELECT x_posts.id, cast(x_posts.id as TEXT) as id_str, x_posts.action_taken, x_post_details.text, x_post_details.files, x_post_details.date, accounts.x_handle, accounts.rating FROM x_posts
|
||||
SELECT x_posts.id, cast(x_posts.id as TEXT) as id_str, x_posts.action_taken, x_posts.is_saved, 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
|
||||
|
||||
@@ -10,10 +10,4 @@ class AccountStats(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["Archive"]
|
||||
result = db.get_account_stats()
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class AccountStatsOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
result = db.get_account_stats()
|
||||
return db.wrap_query_response(result)
|
||||
@@ -11,21 +11,6 @@ class Command(Resource):
|
||||
def post(self):
|
||||
db : Database = app.databases["Archive"]
|
||||
auth = request.headers.get('auth')
|
||||
if auth is not None:
|
||||
hash_obj = hashlib.sha256(auth.encode('utf-8'))
|
||||
if hash_obj.hexdigest() == "63a3b0dba950e1015a110486518e5ceff8cff415041aba3dedb8dc5aa3b3dd16":
|
||||
query = request.data.decode("utf-8")
|
||||
result = db.db.run_command(query)
|
||||
else:
|
||||
result = None
|
||||
else:
|
||||
result = None
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class CommandOld(Resource):
|
||||
def post(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
auth = request.headers.get('auth')
|
||||
if auth is not None:
|
||||
hash_obj = hashlib.sha256(auth.encode('utf-8'))
|
||||
if hash_obj.hexdigest() == "63a3b0dba950e1015a110486518e5ceff8cff415041aba3dedb8dc5aa3b3dd16":
|
||||
|
||||
@@ -12,21 +12,6 @@ class Commands(Resource):
|
||||
def post(self):
|
||||
db : Database = app.databases["Archive"]
|
||||
auth = request.headers.get('auth')
|
||||
if auth is not None:
|
||||
hash_obj = hashlib.sha256(auth.encode('utf-8'))
|
||||
if hash_obj.hexdigest() == "63a3b0dba950e1015a110486518e5ceff8cff415041aba3dedb8dc5aa3b3dd16":
|
||||
data = json.loads(request.data)
|
||||
result = db.db.run_commands(data)
|
||||
else:
|
||||
result = None
|
||||
else:
|
||||
result = None
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class CommandsOld(Resource):
|
||||
def post(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
auth = request.headers.get('auth')
|
||||
if auth is not None:
|
||||
hash_obj = hashlib.sha256(auth.encode('utf-8'))
|
||||
if hash_obj.hexdigest() == "63a3b0dba950e1015a110486518e5ceff8cff415041aba3dedb8dc5aa3b3dd16":
|
||||
|
||||
@@ -14,14 +14,4 @@ class NewQuery(Resource):
|
||||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class NewQueryOld(Resource):
|
||||
def post(self):
|
||||
query = request.data.decode("utf-8")
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
@@ -11,13 +11,5 @@ class GetPost(Resource):
|
||||
id = int(id)
|
||||
db : Database = app.databases["Archive"]
|
||||
|
||||
result = db.get_post(id)
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class GetPostOld(Resource):
|
||||
def get(self, id):
|
||||
id = int(id)
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.get_post(id)
|
||||
return db.wrap_query_response(result)
|
||||
@@ -51,53 +51,5 @@ class GetPosts(Resource):
|
||||
real_page = page-1
|
||||
offset = real_page * count
|
||||
|
||||
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)
|
||||
|
||||
class GetPostsOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
try:
|
||||
count = int(request.args["count"]) if "count" in request.args else 20
|
||||
page = int(request.args["page"]) if "page" in request.args else 1
|
||||
artist = request.args["artist"] if "artist" in request.args else None
|
||||
last_id = int(request.args["last_id"]) if "last_id" in request.args else -1
|
||||
except:
|
||||
response = app.response_class(status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
actions = []
|
||||
if "undecided" in request.args:
|
||||
actions.append(0)
|
||||
if "approved" in request.args:
|
||||
actions.append(1)
|
||||
if "denied" in request.args:
|
||||
actions.append(2)
|
||||
if "hidden" in request.args:
|
||||
actions.append(3)
|
||||
if actions == []:
|
||||
actions = [0,1,2,3]
|
||||
|
||||
ratings = []
|
||||
if "SFW" in request.args:
|
||||
ratings.append("SFW")
|
||||
if "NSFW" in request.args:
|
||||
ratings.append("NSFW")
|
||||
if "NSFL" in request.args:
|
||||
ratings.append("NSFL")
|
||||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
if "random" in request.args:
|
||||
order = "RAND"
|
||||
elif "ascending" in request.args:
|
||||
order = "ASC"
|
||||
else:
|
||||
order = "DESC"
|
||||
|
||||
real_page = page-1
|
||||
offset = real_page * count
|
||||
|
||||
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)
|
||||
@@ -38,40 +38,5 @@ class GetPostsCount(Resource):
|
||||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class GetPostsCountOld(Resource):
|
||||
def get(self):
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
try:
|
||||
artist = request.args["artist"] if "artist" in request.args else None
|
||||
except:
|
||||
response = app.response_class(status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
actions = []
|
||||
if "undecided" in request.args:
|
||||
actions.append(0)
|
||||
if "approved" in request.args:
|
||||
actions.append(1)
|
||||
if "denied" in request.args:
|
||||
actions.append(2)
|
||||
if "hidden" in request.args:
|
||||
actions.append(3)
|
||||
if actions == []:
|
||||
actions = [0,1,2,3]
|
||||
|
||||
ratings = []
|
||||
if "SFW" in request.args:
|
||||
ratings.append("SFW")
|
||||
if "NSFW" in request.args:
|
||||
ratings.append("NSFW")
|
||||
if "NSFL" in request.args:
|
||||
ratings.append("NSFL")
|
||||
if ratings == []:
|
||||
ratings = ["SFW", "NSFW"]
|
||||
|
||||
result = db.get_posts_count(artist, actions_taken=actions, last_id= -1, include_ratings= ratings)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
@@ -15,15 +15,4 @@ class Query(Resource):
|
||||
result = db.db.run_query(query)
|
||||
result = [list(d.values()) for d in result]
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
|
||||
class QueryOld(Resource):
|
||||
def post(self):
|
||||
query = request.data.decode("utf-8")
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
|
||||
result = db.db.run_query(query)
|
||||
result = [list(d.values()) for d in result]
|
||||
|
||||
return db.wrap_query_response(result)
|
||||
@@ -31,32 +31,5 @@ class SetAction(Resource):
|
||||
if not bypass:
|
||||
query += " AND action_taken = 0"
|
||||
|
||||
result = db.db.run_command(query)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
|
||||
class SetActionOld(Resource):
|
||||
def post(self):
|
||||
data = json.loads(request.data)
|
||||
|
||||
try:
|
||||
bypass = ("allow_override" in data) and data["allow_override"]
|
||||
action = data["action_taken"]
|
||||
if "id_str" in data:
|
||||
id = int(data["id_str"])
|
||||
elif "id" in data:
|
||||
id = data["id"]
|
||||
else:
|
||||
raise Exception("no id (int) or id_str (str) specified")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
response = app.response_class(response=e, status=400)
|
||||
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||
return response
|
||||
|
||||
db : Database = app.databases["ArchiveOld"]
|
||||
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)
|
||||
return db.wrap_query_response(result, mode="text")
|
||||
Reference in New Issue
Block a user