synology-pictures/synology.py
Jeena a26b128fba Remove /volume1 from path
In DSM 7 the path mounted for SSH is /photo and does not have the
real path in it anymore.
2022-10-22 13:12:23 +09:00

74 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env python3
import psycopg2
import tempfile
import shutil
import os
import helper
from darktable import Darktable
def connect_db(db_host, db_user, db_passwd):
return psycopg2.connect(
host=db_host,
database="synofoto",
user=db_user,
password=db_passwd)
def close_db(conn):
conn.close()
def fetch_paths_for_names(conn, names, limit=20):
sql_query = """
SELECT folder.name, unit.filename, user_info.name AS user
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)
ORDER BY random()
LIMIT %s;
"""
cur = conn.cursor()
cur.execute(sql_query, ('%(' + names + ')%', limit))
pictures = cur.fetchall()
cur.close()
return pictures
def fetch_files(remotehost, pictures):
dirpath = tempfile.mkdtemp()
for picture in pictures:
# /var/services/homes/kaylee/Photos/MobileBackup/iPhone/2018/09/IMG_20180908_094627.HEIC
# /volume1/photo/jeena/William/Pictures/Photos/Moments/Mobile/SM-N986B/DCIM/Camera/20210924_122532.jpg
path = picture[0]
name = picture[1]
user = picture[2]
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])
escaped_remotefile = helper.escape_file_path(remotefile)
cmd = 'scp -P 23 "' + remotehost + ':' + escaped_remotefile + '" "' + localfile + '"'
if os.system(cmd) == 0:
# Get .xmp file if available
cmd = 'scp -P 23 "' + remotehost + ':' + escaped_remotefile + '.xmp" "' + localfile + '.xmp"'
if os.system(cmd) == 0:
# Use darktable
d = Darktable(localfile)
d.export()
os.remove(localfile)
os.remove(localfile + ".xmp")
return dirpath
if __name__ == "__main__":
host_ip = os.getenv('DB_HOST')
conn = connect_db(host_ip, os.getenv('DB_USER'), os.getenv('DB_PASSWD'))
names = "richard|yingfen"
pictures = fetch_paths_for_names(conn, names, 5)
close_db(conn)
dirpath = fetch_files("jeena@" + host_ip, pictures)
shutil.rmtree(dirpath)