some post 1107

This commit is contained in:
pictures2333 2024-11-07 02:19:53 +08:00
commit 7e3a130db1
7 changed files with 106 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__
.env

15
app.py Normal file
View File

@ -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)

0
blueprints/admin.py Normal file
View File

73
blueprints/article.py Normal file
View File

@ -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...

0
blueprints/log.py Normal file
View File

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
flask
python-dotenv
supabase

13
supaclient.py Normal file
View File

@ -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)