aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-03-10 20:29:16 -0800
committerclarkzjw <[email protected]>2023-03-10 20:29:16 -0800
commit8332a935ab85c451a7481b05b9709bcd466dbe01 (patch)
tree2ce45347f1836882a098958cb9699b9b6f6111e9
parent15ed2a825c760b3ae946d8b7a57a063f8bf393c9 (diff)
downloadphoto-8332a935ab85c451a7481b05b9709bcd466dbe01.tar.gz
add caption and exif
-rw-r--r--.gitignore2
-rw-r--r--bot/.dockerignore1
-rw-r--r--bot/bot.py34
-rw-r--r--bot/docker-compose.yaml8
-rw-r--r--bot/migrations/0001-add_exif.py22
5 files changed, 57 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index fbc84df..ab3415e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
1.env
2pg.yml
1.idea/ 3.idea/
2bot/database/ 4bot/database/
3env 5env
diff --git a/bot/.dockerignore b/bot/.dockerignore
new file mode 100644
index 0000000..a2a8dea
--- /dev/null
+++ b/bot/.dockerignore
@@ -0,0 +1 @@
.env/
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)
diff --git a/bot/docker-compose.yaml b/bot/docker-compose.yaml
index eba14ea..4d22739 100644
--- a/bot/docker-compose.yaml
+++ b/bot/docker-compose.yaml
@@ -1,14 +1,10 @@
1version: '3' 1version: '3'
2services: 2services:
3 photobot: 3 bot:
4 build: 4 build:
5 context: . 5 context: .
6 dockerfile: Dockerfile 6 dockerfile: Dockerfile
7 restart: always 7 restart: always
8 network_mode: "host"
8 env_file: 9 env_file:
9 - .env 10 - .env
10 volumes:
11 - sqlite3:/usr/src/app/database/
12
13volumes:
14 sqlite3:
diff --git a/bot/migrations/0001-add_exif.py b/bot/migrations/0001-add_exif.py
new file mode 100644
index 0000000..0eff797
--- /dev/null
+++ b/bot/migrations/0001-add_exif.py
@@ -0,0 +1,22 @@
1import os
2import sys
3from playhouse.migrate import *
4
5
6if os.getenv("DB_DRIVER") == "psql":
7 db = PostgresqlDatabase(os.getenv("DB_NAME"), user=os.getenv("DB_USER"), password=os.getenv("DB_PASS"),
8 host=os.getenv("DB_HOST"), port=os.getenv("DB_PORT"))
9 migrator = PostgresqlMigrator(db)
10elif os.getenv("DB_DRIVER") == "sqlite3":
11 db = SqliteDatabase("database/photos.db")
12 migrator = SqliteMigrator(db)
13else:
14 print("No DB_DRIVER specified")
15 sys.exit(1)
16
17model = CharField(max_length=256, default="")
18
19# Run the migration, specifying the database table, field name and field.
20migrate(
21 migrator.add_column('photo', 'model', model),
22)
Powered by cgit v1.2.3 (git 2.41.0)