From 7e3a130db1fc0d4beae24c7b1cc42d62cabe2249 Mon Sep 17 00:00:00 2001 From: pictures2333 Date: Thu, 7 Nov 2024 02:19:53 +0800 Subject: [PATCH] some post 1107 --- .gitignore | 2 ++ app.py | 15 +++++++++ blueprints/admin.py | 0 blueprints/article.py | 73 +++++++++++++++++++++++++++++++++++++++++++ blueprints/log.py | 0 requirements.txt | 3 ++ supaclient.py | 13 ++++++++ 7 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 blueprints/admin.py create mode 100644 blueprints/article.py create mode 100644 blueprints/log.py create mode 100644 requirements.txt create mode 100644 supaclient.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d17870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +.env \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..cf51fa0 --- /dev/null +++ b/app.py @@ -0,0 +1,15 @@ +from flask import Flask, session, request, redirect +import os +from blueprints.article import article +from supaclient import supaClient + +app = Flask(__name__) +app.config["SECRET_KEY"] = os.urandom(64) +app.shared_resource = supaClient() + +# blueprints +app.register_blueprint(article, url_prefix = '/api/article') + +# run +if __name__ == "__main__": + app.run(host = "0.0.0.0", port = 8000, debug = False) \ No newline at end of file diff --git a/blueprints/admin.py b/blueprints/admin.py new file mode 100644 index 0000000..e69de29 diff --git a/blueprints/article.py b/blueprints/article.py new file mode 100644 index 0000000..c3f91b2 --- /dev/null +++ b/blueprints/article.py @@ -0,0 +1,73 @@ +# /api/article +# 負責公開範圍內的資料上傳與存取 + +from flask import Blueprint, current_app, request +from supabase import Client +from hashlib import sha256 +import time, math + +article = Blueprint('article', __name__) + +# listing - public +# /api/article/list?page=&count= +@article.route("/list", methods = ["GET"]) +def list(): + page = int(request.args.get("page")) + count = int(request.args.get("count")) + + client: Client = current_app.shared_resource.client + response = client.table("niming_posts").select("id, content, igid, created_at").order("id", desc = True).range(start=page*count, end=(page+1)*count-1).execute() + + return response.data + +# fetching article - public +# /api/article/get?id= +@article.route("/get", methods = ["GET"]) +def get(): + id = str(int(request.args.get("id"))) + + client: Client = current_app.shared_resource.client + response = client.table("niming_posts").select("id, content, igid, created_at").eq("id", id).execute() + + return response.data + +# new post - public +@article.route("/post", methods = ["POST"]) +def post(): + # variables + ctx = request.form.get("ctx") + if ctx is None: return "No content" + else: ctx = str(ctx) + # future - images + # future - videos + + # IG API Part + + # hash + t = str(math.floor(time.time()*10000000)) + hash = sha256( ( ctx + t ).encode() ).hexdigest() + + # Supabase + client: Client = current_app.shared_resource.client + response = (client.table("niming_posts").insert({ + "content": ctx, + "hash": hash + }).execute()) + + # log + + return response.data + +# 發文者專用 - 發文者有義務記住並保護好自己發的文章的sha256 +# fetching article - article owner +@article.route("/owner/get", methods = ["GET"]) +def owner_list(): + hash = str(request.args.get("hash")) + + client: Client = current_app.shared_resource.client + response = client.table("niming_posts").select("id, content, igid, created_at, hash").eq("hash", hash).execute() + + return response.data + +# delete article - article owner +# Coming Soon... \ No newline at end of file diff --git a/blueprints/log.py b/blueprints/log.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2bf9f7d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +python-dotenv +supabase \ No newline at end of file diff --git a/supaclient.py b/supaclient.py new file mode 100644 index 0000000..dd183d9 --- /dev/null +++ b/supaclient.py @@ -0,0 +1,13 @@ +from supabase import Client, create_client +from dotenv import load_dotenv +import os + +load_dotenv() + +URL = os.getenv("SUPABASE_IP") +# service key +KEY = os.getenv("SUPABASE_KEY") + +class supaClient: + def __init__(self): + self.client = create_client(URL, KEY) \ No newline at end of file