aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'bot/bot.py')
-rw-r--r--bot/bot.py34
1 files changed, 30 insertions, 4 deletions
diff --git a/bot/bot.py b/bot/bot.py
index 90b8141..1370c54 100644
--- a/bot/bot.py
+++ b/bot/bot.py
@@ -2,7 +2,7 @@ import io
2import os 2import os
3import logging 3import logging
4import traceback 4import traceback
5from PIL import Image 5from PIL import Image, ExifTags
6import boto3 6import boto3
7from telegram import Update 7from telegram import Update
8from telegram.constants import ParseMode 8from telegram.constants import ParseMode
@@ -11,9 +11,19 @@ from peewee import *
11from uuid import uuid4 11from uuid import uuid4
12from datetime import datetime 12from datetime import datetime
13import json 13import json
14import sys
14import httpx 15import httpx
15 16
16db = SqliteDatabase("database/photos.db") 17
18if os.getenv("DB_DRIVER") == "psql":
19 db = PostgresqlDatabase(os.getenv("DB_NAME"), user=os.getenv("DB_USER"), password=os.getenv("DB_PASS"),
20 host=os.getenv("DB_HOST"), port=os.getenv("DB_PORT"))
21elif os.getenv("DB_DRIVER") == "sqlite3":
22 db = SqliteDatabase("database/photos.db")
23else:
24 print("No DB_DRIVER specified")
25 sys.exit(1)
26
17db.connect(reuse_if_open=True) 27db.connect(reuse_if_open=True)
18 28
19 29
@@ -32,6 +42,7 @@ class Photo(BaseModel):
32 path = CharField(max_length=256) 42 path = CharField(max_length=256)
33 caption = CharField(max_length=256) 43 caption = CharField(max_length=256)
34 alt = CharField(max_length=256) 44 alt = CharField(max_length=256)
45 model = CharField(max_length=256, default="")
35 createdAt = DateTimeField() 46 createdAt = DateTimeField()
36 uploadedAt = DateTimeField() 47 uploadedAt = DateTimeField()
37 48
@@ -72,6 +83,7 @@ def write_json() -> bool:
72 "path": photo.path, 83 "path": photo.path,
73 "caption": photo.caption, 84 "caption": photo.caption,
74 "alt": photo.alt, 85 "alt": photo.alt,
86 "model": photo.model,
75 "createdAt": photo.createdAt.strftime("%Y-%m-%dT%H:%M:%S.000Z"), 87 "createdAt": photo.createdAt.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
76 "uploadedAt": photo.uploadedAt.strftime("%Y-%m-%dT%H:%M:%S.000Z") 88 "uploadedAt": photo.uploadedAt.strftime("%Y-%m-%dT%H:%M:%S.000Z")
77 }) 89 })
@@ -126,6 +138,7 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
126 else: 138 else:
127 return 139 return
128 140
141 caption = update.effective_message.caption
129 await context.bot.send_message(chat_id, "Processing `{}`".format(filename), parse_mode=ParseMode.MARKDOWN_V2) 142 await context.bot.send_message(chat_id, "Processing `{}`".format(filename), parse_mode=ParseMode.MARKDOWN_V2)
130 143
131 img = io.BytesIO() 144 img = io.BytesIO()
@@ -133,6 +146,18 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
133 146
134 try: 147 try:
135 im = Image.open(img) 148 im = Image.open(img)
149 exif = im.getexif()
150
151 camera_model = ""
152 shot_datetime = ""
153 if exif:
154 for key, val in exif.items():
155 if key in ExifTags.TAGS:
156 if ExifTags.TAGS[key] == "Model":
157 camera_model = val
158 elif ExifTags.TAGS[key] == "DateTime":
159 shot_datetime = val
160
136 output = io.BytesIO() 161 output = io.BytesIO()
137 im.save(output, format="webp", lossless=False, quality=80) 162 im.save(output, format="webp", lossless=False, quality=80)
138 163
@@ -147,9 +172,10 @@ async def process(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
147 ratio=im.width / im.height, 172 ratio=im.width / im.height,
148 orientation="landscape" if im.width > im.height else "portrait", 173 orientation="landscape" if im.width > im.height else "portrait",
149 path="https://pixelstatic.jinwei.me/{}".format(key_name), 174 path="https://pixelstatic.jinwei.me/{}".format(key_name),
150 caption="", 175 caption=caption,
151 alt="", 176 alt="",
152 createdAt=datetime.now(), 177 model=camera_model,
178 createdAt=shot_datetime if len(shot_datetime) > 0 else datetime.now(),
153 uploadedAt=datetime.now()) 179 uploadedAt=datetime.now())
154 180
155 output.seek(0) 181 output.seek(0)
Powered by cgit v1.2.3 (git 2.41.0)