Add matrix 'Today x years ago'

This commit is contained in:
Jeena 2023-01-25 10:36:43 +09:00
parent 073cb1d320
commit 52f935a084
8 changed files with 1011 additions and 255 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
__pycache__/
client_secret.json
token.pickle
credentials.json
store

View file

@ -11,12 +11,11 @@ opencv-python = "*"
numpy = "*"
exif = "*"
geopy = "*"
python-matrixbot = "*"
pyheif = "*"
Pillow = "*"
piexif = "*"
exifread = "*"
matrix-commander = "*"
[dev-packages]

1153
Pipfile.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,11 +9,21 @@ Installation
------------
```
sudo apt-get install ufraw-batch imagemagick libheif-examples
sudo apt-get install ufraw-batch imagemagick libheif-examples libolm-dev
pipenv install
pipenv shell
```
Open postgresql port on Synology
================================
Taken from https://www.youtube.com/watch?v=MqJuKu38BsA
```
echo "host all all 192.168.1.1/24 trust" >> /etc/postgresql/pg_hba.conf
sed -i /listen_addresses = '127.0.0.1'/listen_addresses = '*'/ /etc/pastgresql/postgresql.conf
```
License
-------

14
edit.py
View file

@ -7,6 +7,7 @@ import exif
from geopy.geocoders import Nominatim
from datetime import datetime
import pathlib
import os
def wait_with_check_closing(win_name):
"""
@ -60,6 +61,19 @@ class Image:
if name != "":
return name
return None
def get_takentime(self):
e = self.get_exif()
date = e.get('datetime_original')
if date == None:
date = e.get('datetime')
if date == None:
date = e.get('datetime_digitized')
if date == None:
date = e.get('gps_datestamp')
if date == None:
date = os.path.basename(self.path)
return date
def crop(self):
oh, ow, z = self.image.shape

52
matrix.py Executable file
View file

@ -0,0 +1,52 @@
#!/usr/bin/env python3
from datetime import datetime
import sys
import os
import synology
import edit
import shutil
import matrix_commander
from matrix_commander import main
def years_ago(path):
img = edit.Image(path)
takenyear = int(img.get_takentime()[:4])
diff_year = datetime.today().year - takenyear
ret = "Today, "
ret += str(diff_year)
ret += " year"
if diff_year > 1:
ret += "s"
ret += " ago."
return ret
def post_photo(path):
argv = ["matrix-commander"]
argv.extend(["--message", "" + years_ago(path) + ""])
argv.extend(["--image", path])
argv.extend(["--print-event-id"])
return matrix_commander.main(argv)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: matrix.py richard|kaylee")
exit(1)
sy_host = os.getenv('SY_DB_HOST')
sy_user = os.getenv('SY_USER')
conn = synology.connect_db(sy_host, os.getenv('SY_DB_USER'), os.getenv('SY_DB_PASSWD'))
names = sys.argv[1]
month = datetime.today().month
day = datetime.today().day
pictures = synology.fetch_path_for_names_day_and_month(conn, names, month, day, 1)
synology.close_db(conn)
dirpath = synology.fetch_files(sy_user + "@" + sy_host, pictures)
ret = 0
with os.scandir(dirpath) as dirs:
for i, entry in enumerate(dirs):
ret = post_photo(os.path.join(dirpath, entry.name))
shutil.rmtree(dirpath)
exit(ret)

4
run-matrix.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/bash
PATH=$HOME/.local/bin/:$PATH
pipenv run python matrix.py "richard|yingfen|kaylee"

View file

@ -35,6 +35,29 @@ LIMIT %s;
cur.close()
return pictures
def fetch_path_for_names_day_and_month(conn, names, month, day, limit=1):
sql_query = """
SELECT folder.name, unit.filename, user_info.name AS user, EXTRACT(YEAR FROM to_timestamp(unit.takentime)::date) as year
FROM unit
LEFT OUTER JOIN face ON face.ref_id_unit = unit.id
LEFT OUTER JOIN folder ON folder.id = unit.id_folder
LEFT OUTER JOIN user_info ON user_info.id = unit.id_user
WHERE
face.id_person in (SELECT id FROM person WHERE lower(name) SIMILAR TO %s)
AND
EXTRACT(MONTH FROM to_timestamp(unit.takentime)::date) = %s
AND
EXTRACT(DAY FROM to_timestamp(unit.takentime)::date) = %s
ORDER BY random()
LIMIT %s;
"""
cur = conn.cursor()
cur.execute(sql_query, ('%(' + names + ')%', month, day, limit))
pictures = cur.fetchall()
cur.close()
return pictures
def fetch_files(remotehost, pictures):
dirpath = tempfile.mkdtemp()
@ -44,12 +67,13 @@ def fetch_files(remotehost, pictures):
path = picture[0]
name = picture[1]
user = picture[2]
year = picture[3] + "-" if len(picture) > 3 else ""
if user == '/volume1/photo':
lib_path = "/photo"
else:
lib_path = "/homes/" + user + "/Photos"
remotefile = '\ '.join('/'.join([lib_path, path, name]).split())
localfile = '/'.join([dirpath, name])
localfile = '/'.join([dirpath, str(year) + name])
escaped_remotefile = helper.escape_file_path(remotefile)
cmd = 'scp -P 23 "' + remotehost + ':' + escaped_remotefile + '" "' + localfile + '"'
if os.system(cmd) == 0: