From 89e5519bc13c2388b0b496a787e41c113768e9d7 Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Sat, 16 Jan 2021 18:09:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=95=E5=85=A5FB=E7=99=BB=E5=85=A5?= =?UTF-8?q?=E6=A9=9F=E5=88=B6&=E5=BE=8C=E7=AB=AFapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ data.db | Bin 0 -> 16384 bytes index.html | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 api.py create mode 100644 data.db diff --git a/api.py b/api.py new file mode 100644 index 0000000..f977f7f --- /dev/null +++ b/api.py @@ -0,0 +1,55 @@ +from flask import Flask, render_template, request +import requests +import json +import sqlite3 + +app = Flask(__name__) + +def facebookAuth(token): + url = "https://graph.facebook.com/v9.0/me?access_token={}" + + response = requests.get(url.format(token)) + data = json.loads(response.text) + + # 若 access code 通過 facebook 驗證 + if response.status_code == 200: + return True, data['id'], data['name'] + else: + return False, None, None + +@app.route('/courseTable', methods=["POST"]) +def get(): + # 若 access code 通過 facebook 驗證 + status, uid, name = facebookAuth(request.values['token']) + + if status: + # 由資料庫提取資料 + with sqlite3.connect('data.db') as conn: + sql = "SELECT `json` FROM `courseTables` WHERE `uid`=?" + + data = conn.execute(sql, [uid]).fetchone() + # 已經存在使用者,直接回傳 課表json + if data: + ansJSON = data[0] + return json.dumps({ + "status": "ok", + "data": ansJSON + }) + + # 使用者第一次使用,回傳並寫入空json + else: + sql = "INSERT INTO `courseTables` VALUES(NULL, ?, ?, ?)" + conn.execute(sql, [uid, '[]', name]) + conn.commit() + return json.dumps({ + "status": "ok", + "data": "[]" + }) + + # access code 可能是偽造的 + else: + return '{"status": "error access code"}', 403 + +if __name__ == '__main__': + app.debug = True + app.run(host='0.0.0.0') diff --git a/data.db b/data.db new file mode 100644 index 0000000000000000000000000000000000000000..3ddcad1e02960526f2aaee915da8bf138d6242e8 GIT binary patch literal 16384 zcmeI&&n^Q&90%~3trAUYx#)$MY|>~I*|w|HNy(;ZP}E2?BC;6qcS~n`tAjX79K3}=+@GqW@ExlJ<5OLKdkU^|uSk>jy6=_ZttDaHsP zK~>sR(Nv)gxvGAI|E*aB$!PvIth+?(suR7hs~&!AHU#YufB*y_009U<00Izz00bb= zTtIhev3al7_E3kxyU!Jz^#?j4(I`DT^_g36xIw)^QaZ(fRmiUzheys6VW6Ym+SdEOUeP6X!AG1RM z0uX=z1Rwwb2tWV=5P$##An^YL!fMM@el6^&QKedXfMD literal 0 HcmV?d00001 diff --git a/index.html b/index.html index d241348..97e9a71 100644 --- a/index.html +++ b/index.html @@ -17,6 +17,51 @@ + + + +
+ + + + + + +