From 375c0d2544b20124a8cf497819006e01cd217225 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 1 Mar 2023 19:14:59 -0800 Subject: host photos.json on cloudflare r2 --- bot/bot.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/bot.py b/bot/bot.py index f379e95..90b8141 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -11,6 +11,7 @@ from peewee import * from uuid import uuid4 from datetime import datetime import json +import httpx db = SqliteDatabase("database/photos.db") db.connect(reuse_if_open=True) @@ -53,6 +54,7 @@ bucket_name = "pixel-jinwei-me" cf_account_id = os.getenv("CF_ACCOUNT_ID") aws_access_key_id = os.getenv("CF_R2_KEY_ID") aws_secret_access_key = os.getenv("CF_R2_ACCESS_KEY_SECRET") +cloudflare_pages_deploy_hook_url = os.getenv("CLOUDFLARE_PAGES_DEPLOY_HOOK_URL") def write_json() -> bool: @@ -74,8 +76,7 @@ def write_json() -> bool: "uploadedAt": photo.uploadedAt.strftime("%Y-%m-%dT%H:%M:%S.000Z") }) results.sort(key=lambda x: x["uploadedAt"], reverse=True) - with open("database/photos.json", "w") as f: - f.write(json.dumps(results)) + upload_to_s3("photos.json", json.dumps(results).encode("utf-8")) return True @@ -155,8 +156,11 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: upload_to_s3(photo.fileId, output.read()) write_json() - await update.message.reply_markdown_v2(text="Done") - + response = httpx.post(cloudflare_pages_deploy_hook_url) + if response.status_code == 200: + await update.message.reply_markdown_v2(text="trigger deploy succeed") + else: + await update.message.reply_markdown_v2("trigger deploy failed, status: {}".format(response.status_code)) except Exception: await update.message.reply_markdown_v2(text="Error:\n```{}```".format(traceback.format_exc())) -- cgit v1.2.3