Skip to content

Commit

Permalink
starting on removing exiftool dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
chkuendig committed Nov 24, 2024
1 parent b01db33 commit 3662351
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/icloudpd/xmp_sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

import json
import xml.etree.ElementTree as ET
import base64
import logging
import plistlib
Expand Down Expand Up @@ -71,9 +73,68 @@ def build_exiftool_arguments(asset_record: dict[str, Any]) -> list[str]:
args = ["-" + k + "=" + str(xmp_metadata[k]) for k in xmp_metadata]
return args

def generate_xml(logger) -> ET.Element:

# Create the root element
xmpmeta = ET.Element('x:xmpmeta', {
'xmlns:x': 'adobe:ns:meta/',
'x:xmptk': 'XMP Core 6.0.0'
})

# Create the RDF element
rdf = ET.SubElement(xmpmeta, 'rdf:RDF', {
'xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
})

# Create the Description element
description = ET.SubElement(rdf, 'rdf:Description', {
'rdf:about': '',
'xmlns:exif': 'http://ns.adobe.com/exif/1.0/',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:photoshop': 'http://ns.adobe.com/photoshop/1.0/'
})

# Add EXIF elements
ET.SubElement(description, 'exif:GPSHPositioningError').text = '3.5355339055557087'
ET.SubElement(description, 'exif:GPSImgDirection').text = '65.517410228509249'
ET.SubElement(description, 'exif:GPSAltitudeRef').text = '0'
ET.SubElement(description, 'exif:GPSAltitude').text = '594.27303754266211'
ET.SubElement(description, 'exif:GPSLatitudeRef').text = 'N'
ET.SubElement(description, 'exif:GPSLatitude').text = '47.386561666666665'
ET.SubElement(description, 'exif:GPSLongitudeRef').text = 'E'
ET.SubElement(description, 'exif:GPSLongitude').text = '8.5784166666666675'
ET.SubElement(description, 'exif:GPSImgDirectionRef').text = 'T'
ET.SubElement(description, 'exif:GPSSpeed').text = '0.37999999523160005'
ET.SubElement(description, 'exif:GPSSpeedRef').text = 'K'
ET.SubElement(description, 'exif:GPSTimeStamp').text = '2024-10-28T10:03:22Z'

# Add DC elements
ET.SubElement(description, 'dc:title').text = 'Title Here'
ET.SubElement(description, 'dc:description').text = 'Caption Here'

# Add DC subject elements
subject = ET.SubElement(description, 'dc:subject')
seq = ET.SubElement(subject, 'rdf:Seq')
ET.SubElement(seq, 'rdf:li').text = 'some keyword'

# Add Photoshop elements
ET.SubElement(description, 'photoshop:DateCreated').text = '2024-10-28T11:03:25+01:00'

return xmpmeta

def generate_xmp_file(logger: logging.Logger, download_path: str, asset_record: dict[str, Any]) -> None:
"""Generate XMP sidecar file from photo asset record"""
# todo: check if xmptk ( "XMPToolkit" https://exiftool.org/TagNames/XMP.html#x) is correct
# Convert the tree to a string
xmpmeta = generate_xml(logger)
xml_str = ET.tostring(xmpmeta, encoding='unicode')

# Add XML declaration
xml_str = '<?xml version="1.0"?>\n' + xml_str

# Print the XML string
print(xml_str)
if exif_tool:
args = build_exiftool_arguments(asset_record)
# json.dump(asset_record['fields'], open(download_path+".ar.json", "w"), indent=4)
json.dump(asset_record['fields'], open(download_path+".ar.json", "w"), indent=4)
exif_tool.execute("-overwrite_original", download_path+".xmp", *args)

0 comments on commit 3662351

Please sign in to comment.