autoimport/autoimport/order/order.py

81 lines
2.3 KiB
Python

import os
import logging
from .date_and_time import get_datetime
def order(db, path_specifier):
logger = logging.getLogger(__name__)
cursor = db.cursor()
result = cursor.execute(
'''SELECT rowid,
name,
DateTime,
DateTimeDigitized,
DateTimeOriginal,
Model,
Make,
Software
FROM FILES'''
)
for (rowid
, name
, DateTime
, DateTimeDigitized
, DateTimeOriginal
, Model
, Make
, Software) in cursor.fetchall():
DateTime = get_datetime(DateTime)
DateTimeDigitized = get_datetime(DateTimeDigitized)
DateTimeOriginal = get_datetime(DateTimeOriginal)
data = {
"<name>": name,
"<DateTime.day>": str(DateTime.day).zfill(2),
"<DateTime.month>": str(DateTime.month).zfill(2),
"<DateTime.year>": DateTime.year,
"<DateTime.hour>": str(DateTime.hour).zfill(2),
"<DateTime.minute>": str(DateTime.minute).zfill(2),
"<DateTime.second>": str(DateTime.second).zfill(2),
"<DateTimeDigitized.day>": str(DateTimeDigitized.day).zfill(2),
"<DateTimeDigitized.month>": str(DateTimeDigitized.month).zfill(2),
"<DateTimeDigitized.year>": DateTimeDigitized.year,
"<DateTimeDigitized.hour>": str(DateTimeDigitized.hour).zfill(2),
"<DateTimeDigitized.minute>": str(DateTimeDigitized.minute).zfill(2),
"<DateTimeDigitized.second>": str(DateTimeDigitized.second).zfill(2),
"<DateTimeOriginal.day>": str(DateTimeOriginal.day).zfill(2),
"<DateTimeOriginal.month>": str(DateTimeOriginal.month).zfill(2),
"<DateTimeOriginal.year>": DateTimeOriginal.year,
"<DateTimeOriginal.hour>": str(DateTimeOriginal.hour).zfill(2),
"<DateTimeOriginal.minute>": str(DateTimeOriginal.minute).zfill(2),
"<DateTimeOriginal.second>": str(DateTimeOriginal.second).zfill(2),
"<Model>": Model,
"<Make>": Make,
"<Software>": Software
}
this_path = [str(data[p]) if p in data else p for p in path_specifier]
logger.debug(this_path)
this_path = os.path.join(*this_path)
path_id = get_path_id(db, this_path)
cursor.execute("INSERT INTO ASSOCIATIONS(file_id, directory_id) VALUES(?, ?)", (rowid, path_id))
def get_path_id(db, path):
cursor = db.cursor()
cursor.execute("SELECT rowid FROM DIRECTORIES WHERE name=?", (path,))
result = cursor.fetchone()
if(result):
return result[0]
cursor.execute("INSERT INTO DIRECTORIES(name) VALUES(?)", (path,))
return cursor.lastrowid