aboutsummaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-03-01 19:14:59 -0800
committerclarkzjw <[email protected]>2023-03-01 20:21:47 -0800
commit375c0d2544b20124a8cf497819006e01cd217225 (patch)
tree8984bf464561722f33dd15f60a7872ff1aa71912 /bot
parentf87fef5388ca419f79c4750810e302cedfc310f5 (diff)
downloadphoto-375c0d2544b20124a8cf497819006e01cd217225.tar.gz
host photos.json on cloudflare r2
Diffstat (limited to 'bot')
-rw-r--r--bot/bot.py12
1 files changed, 8 insertions, 4 deletions
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 *
11from uuid import uuid4 11from uuid import uuid4
12from datetime import datetime 12from datetime import datetime
13import json 13import json
14import httpx
14 15
15db = SqliteDatabase("database/photos.db") 16db = SqliteDatabase("database/photos.db")
16db.connect(reuse_if_open=True) 17db.connect(reuse_if_open=True)
@@ -53,6 +54,7 @@ bucket_name = "pixel-jinwei-me"
53cf_account_id = os.getenv("CF_ACCOUNT_ID") 54cf_account_id = os.getenv("CF_ACCOUNT_ID")
54aws_access_key_id = os.getenv("CF_R2_KEY_ID") 55aws_access_key_id = os.getenv("CF_R2_KEY_ID")
55aws_secret_access_key = os.getenv("CF_R2_ACCESS_KEY_SECRET") 56aws_secret_access_key = os.getenv("CF_R2_ACCESS_KEY_SECRET")
57cloudflare_pages_deploy_hook_url = os.getenv("CLOUDFLARE_PAGES_DEPLOY_HOOK_URL")
56 58
57 59
58def write_json() -> bool: 60def write_json() -> bool:
@@ -74,8 +76,7 @@ def write_json() -> bool:
74 "uploadedAt": photo.uploadedAt.strftime("%Y-%m-%dT%H:%M:%S.000Z") 76 "uploadedAt": photo.uploadedAt.strftime("%Y-%m-%dT%H:%M:%S.000Z")
75 }) 77 })
76 results.sort(key=lambda x: x["uploadedAt"], reverse=True) 78 results.sort(key=lambda x: x["uploadedAt"], reverse=True)
77 with open("database/photos.json", "w") as f: 79 upload_to_s3("photos.json", json.dumps(results).encode("utf-8"))
78 f.write(json.dumps(results))
79 return True 80 return True
80 81
81 82
@@ -155,8 +156,11 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
155 upload_to_s3(photo.fileId, output.read()) 156 upload_to_s3(photo.fileId, output.read())
156 157
157 write_json() 158 write_json()
158 await update.message.reply_markdown_v2(text="Done") 159 response = httpx.post(cloudflare_pages_deploy_hook_url)
159 160 if response.status_code == 200:
161 await update.message.reply_markdown_v2(text="trigger deploy succeed")
162 else:
163 await update.message.reply_markdown_v2("trigger deploy failed, status: {}".format(response.status_code))
160 except Exception: 164 except Exception:
161 await update.message.reply_markdown_v2(text="Error:\n```{}```".format(traceback.format_exc())) 165 await update.message.reply_markdown_v2(text="Error:\n```{}```".format(traceback.format_exc()))
162 166
Powered by cgit v1.2.3 (git 2.41.0)