diff options
author | clarkzjw <[email protected]> | 2023-03-10 20:29:16 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-03-10 20:29:16 -0800 |
commit | 8332a935ab85c451a7481b05b9709bcd466dbe01 (patch) | |
tree | 2ce45347f1836882a098958cb9699b9b6f6111e9 /bot | |
parent | 15ed2a825c760b3ae946d8b7a57a063f8bf393c9 (diff) | |
download | photo-8332a935ab85c451a7481b05b9709bcd466dbe01.tar.gz |
add caption and exif
Diffstat (limited to 'bot')
-rw-r--r-- | bot/.dockerignore | 1 | ||||
-rw-r--r-- | bot/bot.py | 34 | ||||
-rw-r--r-- | bot/docker-compose.yaml | 8 | ||||
-rw-r--r-- | bot/migrations/0001-add_exif.py | 22 |
4 files changed, 55 insertions, 10 deletions
diff --git a/bot/.dockerignore b/bot/.dockerignore new file mode 100644 index 0000000..a2a8dea --- /dev/null +++ b/bot/.dockerignore | |||
@@ -0,0 +1 @@ | |||
.env/ | |||
@@ -2,7 +2,7 @@ import io | |||
2 | import os | 2 | import os |
3 | import logging | 3 | import logging |
4 | import traceback | 4 | import traceback |
5 | from PIL import Image | 5 | from PIL import Image, ExifTags |
6 | import boto3 | 6 | import boto3 |
7 | from telegram import Update | 7 | from telegram import Update |
8 | from telegram.constants import ParseMode | 8 | from telegram.constants import ParseMode |
@@ -11,9 +11,19 @@ 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 sys | ||
14 | import httpx | 15 | import httpx |
15 | 16 | ||
16 | db = SqliteDatabase("database/photos.db") | 17 | |
18 | if 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")) | ||
21 | elif os.getenv("DB_DRIVER") == "sqlite3": | ||
22 | db = SqliteDatabase("database/photos.db") | ||
23 | else: | ||
24 | print("No DB_DRIVER specified") | ||
25 | sys.exit(1) | ||
26 | |||
17 | db.connect(reuse_if_open=True) | 27 | db.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 @@ | |||
1 | version: '3' | 1 | version: '3' |
2 | services: | 2 | services: |
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 | |||
13 | volumes: | ||
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 @@ | |||
1 | import os | ||
2 | import sys | ||
3 | from playhouse.migrate import * | ||
4 | |||
5 | |||
6 | if 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) | ||
10 | elif os.getenv("DB_DRIVER") == "sqlite3": | ||
11 | db = SqliteDatabase("database/photos.db") | ||
12 | migrator = SqliteMigrator(db) | ||
13 | else: | ||
14 | print("No DB_DRIVER specified") | ||
15 | sys.exit(1) | ||
16 | |||
17 | model = CharField(max_length=256, default="") | ||
18 | |||
19 | # Run the migration, specifying the database table, field name and field. | ||
20 | migrate( | ||
21 | migrator.add_column('photo', 'model', model), | ||
22 | ) | ||