diff options
author | clarkzjw <[email protected]> | 2023-03-01 19:14:59 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-03-01 20:21:47 -0800 |
commit | 375c0d2544b20124a8cf497819006e01cd217225 (patch) | |
tree | 8984bf464561722f33dd15f60a7872ff1aa71912 /bot | |
parent | f87fef5388ca419f79c4750810e302cedfc310f5 (diff) | |
download | photo-375c0d2544b20124a8cf497819006e01cd217225.tar.gz |
host photos.json on cloudflare r2
Diffstat (limited to 'bot')
-rw-r--r-- | bot/bot.py | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -11,6 +11,7 @@ from peewee import * | |||
11 | from uuid import uuid4 | 11 | from uuid import uuid4 |
12 | from datetime import datetime | 12 | from datetime import datetime |
13 | import json | 13 | import json |
14 | import httpx | ||
14 | 15 | ||
15 | db = SqliteDatabase("database/photos.db") | 16 | db = SqliteDatabase("database/photos.db") |
16 | db.connect(reuse_if_open=True) | 17 | db.connect(reuse_if_open=True) |
@@ -53,6 +54,7 @@ bucket_name = "pixel-jinwei-me" | |||
53 | cf_account_id = os.getenv("CF_ACCOUNT_ID") | 54 | cf_account_id = os.getenv("CF_ACCOUNT_ID") |
54 | aws_access_key_id = os.getenv("CF_R2_KEY_ID") | 55 | aws_access_key_id = os.getenv("CF_R2_KEY_ID") |
55 | aws_secret_access_key = os.getenv("CF_R2_ACCESS_KEY_SECRET") | 56 | aws_secret_access_key = os.getenv("CF_R2_ACCESS_KEY_SECRET") |
57 | cloudflare_pages_deploy_hook_url = os.getenv("CLOUDFLARE_PAGES_DEPLOY_HOOK_URL") | ||
56 | 58 | ||
57 | 59 | ||
58 | def write_json() -> bool: | 60 | def 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 | ||