almost working
BiRabittoh birabittoh@tilde.team
Fri, 04 Aug 2023 16:50:59 +0200
6 files changed,
62 insertions(+),
34 deletions(-)
A
.vscode/launch.json
@@ -0,0 +1,27 @@
+{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Flask", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "artbound_python", + "FLASK_DEBUG": "1" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload", + "--port", + "1111" + ], + "jinja": true, + "justMyCode": true + } + ] +}
M
artbound_python/api.py
→
artbound_python/api.py
@@ -58,7 +58,6 @@ fp.write(fh_content)
return file_name load_dotenv() -SPREADSHEET_ID = os.getenv("SPREADSHEET_ID") #'1OHI2PA62LNrEbViq87AyUlX1ni3Gr9gttxPLvkOzZ6g' -RANGE = os.getenv("RANGE")#'Risposte del modulo 1!A2:E' - +SPREADSHEET_ID = os.getenv("SPREADSHEET_ID") +RANGE = os.getenv("RANGE") creds = get_google_credentials()
M
artbound_python/cache.py
→
artbound_python/cache.py
@@ -9,7 +9,7 @@ CACHE_DIRECTORY = "cache"
CACHE_PATH = os.path.join("artbound_python", "static", "res", CACHE_DIRECTORY) db = [] -last_updated = None +last_updated = "" with suppress(FileExistsError): os.makedirs(CACHE_PATH)@@ -31,34 +31,33 @@ def clear_cache():
shutil.rmtree(CACHE_PATH) def handle_fanart(fanart): - fanart_id = fanart["id"] - fanart["content"] = download_fanart(fanart_id) + fanart["content"] = download_fanart(fanart["id"]) return fanart def handle_row(row): fanart_date = datetime.strptime(row[0], "%d/%m/%Y %H.%M.%S") - fanart_id = row[3].strip("https://drive.google.com/open?id=") + fanart_id = row[3][33:] return { 'id': fanart_id, 'date': fanart_date.strftime("%Y-%m"), 'name': row[1], 'enabled': 1 } - -def update_database(): - global db - global last_updated - db = [ handle_row(x) for x in get_rows() ] - if len(db) == 0: - print("No fanarts!") - exit(1) - last_updated = datetime.now() - -def get_fanarts(month): - return [ handle_fanart(x) for x in db if x["date"] == month ] - -update_database() - -if __name__ == "__main__": - print(handle_fanart({ 'id': '1_DUo-dW40So3T24a91SyGrEcAGKfP0l_'})) - #clear_cache() + +class DB(): + def update_database(self): + self.db = [ handle_row(x) for x in get_rows() ] + if len(self.db) == 0: + print("No fanarts!") + exit(1) + self.last_updated = datetime.now() + + def __init__(self): + self.db = [] + self.update_database() + + def get_fanarts(self, month): + return [ handle_fanart(x) for x in self.db if x["date"] == month ] + + def get_last_updated(self): + return self.last_updated.strftime("%d/%m/%Y %H:%M")
M
artbound_python/static/script.js
→
artbound_python/static/script.js
@@ -101,7 +101,7 @@ }
async function updateFanartList() { main_container_div.hidden = false; - content_div.innerHTML = "" + //content_div.innerHTML = "" let i = 0; for (fanart of fanarts) {@@ -212,7 +212,6 @@ return response.json();
} function getArtworks() { - document.getElementById("") postData("/", { month: month_input.value }, "application/json").then((data) => { console.log(data); fanarts = data;
M
artbound_python/templates/index.html
→
artbound_python/templates/index.html
@@ -26,12 +26,12 @@ <line x1="3" y1="22" x2="21" y2="22"></line>
</svg> ArtBound Panel 2.0 </h1> - <p class="lead" onclick="debugFn()">Perché semplice è meglio.</p> + <a href="/update" class="lead">{{ last_updated }}</a> <div id="month_div"> <label for="month_input" style="margin-right: 10px;">Scegli il mese: </label><input id="month_input" type="month" value="2022-08"> <a href="#" class="btn my-2 btn-go" onclick="handleAuthClick()" id="authorize_button" hidden>Vai</a> - <a href="#" class="btn btn-secondary my-2" onclick="getArtworks()">getArtworks</a> + <a href="#" class="btn btn-secondary my-2" onclick="getArtworks()">Ottieni</a> </div> <div id="controls" hidden> <a href="#" class="btn btn-secondary my-2" onclick="selectAllNone(1)" id="selectall_button">✅</a>
M
artbound_python/views.py
→
artbound_python/views.py
@@ -1,19 +1,23 @@
import json from flask import request, redirect, render_template from artbound_python import app -from artbound_python.cache import get_fanarts +from artbound_python.cache import last_updated, DB - -debug_fanarts = [{"id":"1dE8L7w2DuOfQSJwf5oRjAeJ-VZfy5o-6","date":["03","08","2022"],"name":"Saro","content":"/static/res/wm.png","enabled":1,"index":1,"div":{}},{"id":"1ZO2poqaylmh7_FkEjRthozVXFpcP1Edx","date":["18","08","2022"],"name":"suchetto","content":"https://drive.google.com/uc?id=1ZO2poqaylmh7_FkEjRthozVXFpcP1Edx","enabled":1,"index":2,"div":{}},{"id":"1jkpZzqnQUdXv7QiW1khuwnSsdnjudBt-","date":["18","08","2022"],"name":"suca","content":"https://drive.google.com/uc?id=1clZbi1QzJQo_c7PGWx-nmLgfPhXqHdZR","enabled":1,"index":3,"div":{}}] +database = DB() @app.route('/', methods=['GET', 'POST']) def route_index(): if request.method == 'GET': - return render_template("index.html") + return render_template("index.html", last_updated=database.get_last_updated()) if (request.headers.get('Content-Type') != 'application/json'): return 'Content-Type not supported. Please use "application/json".' month = request.json["month"] - fanarts = get_fanarts(month) + fanarts = database.get_fanarts(month) return json.dumps(fanarts) + +@app.route('/update') +def route_update(): + database.update_database() + return redirect("/")