mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-10 13:07:15 +00:00
feat(sync): add metadata
This commit is contained in:
parent
1fbd21183e
commit
de7cbc878a
@ -22,7 +22,7 @@ from watchdog.observers import Observer
|
|||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
from memos.read_metadata import read_metadata
|
from memos.utils import get_image_metadata
|
||||||
from memos.schemas import MetadataSource
|
from memos.schemas import MetadataSource
|
||||||
from memos.logging_config import LOGGING_CONFIG
|
from memos.logging_config import LOGGING_CONFIG
|
||||||
import logging.config
|
import logging.config
|
||||||
@ -220,7 +220,7 @@ async def loop_files(library_id, folder, folder_path, force, plugins):
|
|||||||
is_thumbnail = False
|
is_thumbnail = False
|
||||||
|
|
||||||
if file_type_group == "image":
|
if file_type_group == "image":
|
||||||
metadata = read_metadata(absolute_file_path)
|
metadata = get_image_metadata(absolute_file_path)
|
||||||
if metadata:
|
if metadata:
|
||||||
if (
|
if (
|
||||||
"active_window" in metadata
|
"active_window" in metadata
|
||||||
@ -800,22 +800,58 @@ def sync(
|
|||||||
params={"filepath": str(file_path)},
|
params={"filepath": str(file_path)},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
file_stat = file_path.stat()
|
||||||
|
file_type, file_type_group = get_file_type(file_path)
|
||||||
|
|
||||||
|
new_entity = {
|
||||||
|
"filename": file_path.name,
|
||||||
|
"filepath": str(file_path),
|
||||||
|
"size": file_stat.st_size,
|
||||||
|
"file_created_at": format_timestamp(file_stat.st_ctime),
|
||||||
|
"file_last_modified_at": format_timestamp(file_stat.st_mtime),
|
||||||
|
"file_type": file_type,
|
||||||
|
"file_type_group": file_type_group,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Handle metadata
|
||||||
|
is_thumbnail = False
|
||||||
|
if file_type_group == "image":
|
||||||
|
metadata = get_image_metadata(file_path)
|
||||||
|
if metadata:
|
||||||
|
if "active_window" in metadata and "active_app" not in metadata:
|
||||||
|
metadata["active_app"] = metadata["active_window"].split(" - ")[0]
|
||||||
|
new_entity["metadata_entries"] = [
|
||||||
|
{
|
||||||
|
"key": key,
|
||||||
|
"value": str(value),
|
||||||
|
"source": MetadataSource.SYSTEM_GENERATED.value,
|
||||||
|
"data_type": "number" if isinstance(value, (int, float)) else "text",
|
||||||
|
}
|
||||||
|
for key, value in metadata.items()
|
||||||
|
if key != IS_THUMBNAIL
|
||||||
|
]
|
||||||
|
if "active_app" in metadata:
|
||||||
|
new_entity.setdefault("tags", []).append(metadata["active_app"])
|
||||||
|
is_thumbnail = metadata.get(IS_THUMBNAIL, False)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
# File exists, update it
|
# File exists, update it
|
||||||
existing_entity = response.json()
|
existing_entity = response.json()
|
||||||
file_stat = file_path.stat()
|
new_entity["folder_id"] = existing_entity["folder_id"]
|
||||||
file_type, file_type_group = get_file_type(file_path)
|
|
||||||
|
|
||||||
new_entity = {
|
if is_thumbnail:
|
||||||
"filename": file_path.name,
|
new_entity["file_created_at"] = existing_entity["file_created_at"]
|
||||||
"filepath": str(file_path),
|
new_entity["file_last_modified_at"] = existing_entity["file_last_modified_at"]
|
||||||
"size": file_stat.st_size,
|
new_entity["file_type"] = existing_entity["file_type"]
|
||||||
"file_created_at": format_timestamp(file_stat.st_ctime),
|
new_entity["file_type_group"] = existing_entity["file_type_group"]
|
||||||
"file_last_modified_at": format_timestamp(file_stat.st_mtime),
|
new_entity["size"] = existing_entity["size"]
|
||||||
"file_type": file_type,
|
|
||||||
"file_type_group": file_type_group,
|
# Merge existing metadata with new metadata
|
||||||
"folder_id": existing_entity["folder_id"],
|
if new_entity.get("metadata_entries"):
|
||||||
}
|
new_metadata_keys = {entry["key"] for entry in new_entity["metadata_entries"]}
|
||||||
|
for existing_entry in existing_entity["metadata_entries"]:
|
||||||
|
if existing_entry["key"] not in new_metadata_keys:
|
||||||
|
new_entity["metadata_entries"].append(existing_entry)
|
||||||
|
|
||||||
if force or (
|
if force or (
|
||||||
existing_entity["file_last_modified_at"]
|
existing_entity["file_last_modified_at"]
|
||||||
@ -850,19 +886,7 @@ def sync(
|
|||||||
|
|
||||||
if folder:
|
if folder:
|
||||||
# Create new entity
|
# Create new entity
|
||||||
file_stat = file_path.stat()
|
new_entity["folder_id"] = folder["id"]
|
||||||
file_type, file_type_group = get_file_type(file_path)
|
|
||||||
|
|
||||||
new_entity = {
|
|
||||||
"filename": file_path.name,
|
|
||||||
"filepath": str(file_path),
|
|
||||||
"size": file_stat.st_size,
|
|
||||||
"file_created_at": format_timestamp(file_stat.st_ctime),
|
|
||||||
"file_last_modified_at": format_timestamp(file_stat.st_mtime),
|
|
||||||
"file_type": file_type,
|
|
||||||
"file_type_group": file_type_group,
|
|
||||||
"folder_id": folder["id"],
|
|
||||||
}
|
|
||||||
|
|
||||||
create_response = httpx.post(
|
create_response = httpx.post(
|
||||||
f"{BASE_URL}/libraries/{library_id}/entities",
|
f"{BASE_URL}/libraries/{library_id}/entities",
|
||||||
|
@ -134,9 +134,6 @@ def take_screenshot_macos(
|
|||||||
|
|
||||||
with Image.open(temp_filename) as img:
|
with Image.open(temp_filename) as img:
|
||||||
img = img.convert("RGB")
|
img = img.convert("RGB")
|
||||||
webp_filename = os.path.join(
|
|
||||||
base_dir, date, f"screenshot-{timestamp}-of-{screen_name}.webp"
|
|
||||||
)
|
|
||||||
current_hash = str(imagehash.phash(img))
|
current_hash = str(imagehash.phash(img))
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -163,8 +160,15 @@ def take_screenshot_macos(
|
|||||||
"sequence": screen_sequences[screen_name],
|
"sequence": screen_sequences[screen_name],
|
||||||
}
|
}
|
||||||
|
|
||||||
img.save(webp_filename, format="WebP", quality=85)
|
# Write metadata to the temporary PNG file
|
||||||
write_image_metadata(webp_filename, metadata)
|
write_image_metadata(temp_filename, metadata)
|
||||||
|
|
||||||
|
# Save as WebP with metadata included
|
||||||
|
webp_filename = os.path.join(
|
||||||
|
base_dir, date, f"screenshot-{timestamp}-of-{screen_name}.webp"
|
||||||
|
)
|
||||||
|
img.save(webp_filename, format="WebP", quality=85, exif=img.info.get("exif"))
|
||||||
|
|
||||||
save_screen_sequences(base_dir, screen_sequences, date)
|
save_screen_sequences(base_dir, screen_sequences, date)
|
||||||
|
|
||||||
os.remove(temp_filename)
|
os.remove(temp_filename)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user