-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Export of metadata via XMP Sidecar
- Loading branch information
Showing
8 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Generate XMP sidecar file from photo asset record""" | ||
|
||
from __future__ import annotations | ||
|
||
import base64 | ||
import logging | ||
import plistlib | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
import dateutil.tz | ||
from exiftool import ExifToolHelper | ||
|
||
exif_tool = None | ||
def init_exiftool(logger: logging.Logger) -> None: | ||
"""Initialize ExifTool""" | ||
global exif_tool | ||
if not exif_tool: | ||
try: | ||
exif_tool = ExifToolHelper(logger=logging.getLogger(__name__)) | ||
except FileNotFoundError as err: | ||
logger.warning(err) | ||
logger.warning("XMP sidecar files will not be generated") | ||
else: | ||
raise Exception("ExifTool already initialized") | ||
|
||
def build_exiftool_arguments(asset_record: dict[str, Any]) -> list[str]: | ||
xmp_metadata: dict[str, str|int] = {} | ||
if 'captionEnc' in asset_record['fields']: | ||
xmp_metadata['Title'] = base64.b64decode(asset_record['fields']['captionEnc']['value']).decode('utf-8') | ||
if 'extendedDescEnc' in asset_record['fields']: | ||
xmp_metadata['Description'] = base64.b64decode(asset_record['fields']['extendedDescEnc']['value']).decode('utf-8') | ||
if 'orientation' in asset_record['fields']: | ||
xmp_metadata['Orientation'] = asset_record['fields']['orientation']['value'] | ||
if 'assetSubtypeV2' in asset_record['fields'] and int(asset_record['fields']['assetSubtypeV2']['value']) == 3: | ||
xmp_metadata["Make"] = "Screenshot" | ||
xmp_metadata["DigitalSourceType"] = "screenCapture" | ||
if 'keywordsEnc' in asset_record['fields']: | ||
keywords = plistlib.loads(base64.b64decode(asset_record['fields']['keywordsEnc']['value']), fmt=plistlib.FMT_BINARY) | ||
if(len(keywords) > 0): | ||
xmp_metadata["IPTC:keywords"] = ",".join(keywords) | ||
if 'locationEnc' in asset_record['fields']: | ||
locationDec = plistlib.loads(base64.b64decode(asset_record['fields']['locationEnc']['value']), fmt=plistlib.FMT_BINARY) | ||
if('alt' in locationDec): | ||
xmp_metadata["GPSAltitude"] = locationDec['alt'] | ||
if('lat' in locationDec): | ||
xmp_metadata["GPSLatitude"] = locationDec['lat'] | ||
if('lon' in locationDec): | ||
xmp_metadata["GPSLongitude"] = locationDec['lon'] | ||
if('speed' in locationDec): | ||
xmp_metadata["GPSSpeed"] = locationDec['speed'] | ||
if('timestamp' in locationDec and isinstance(locationDec['timestamp'], datetime)): | ||
xmp_metadata["exif:GPSDateTime"] = locationDec['timestamp'].strftime("%Y:%m:%d %H:%M:%S.%f%z") | ||
if 'assetDate' in asset_record['fields']: | ||
timeZoneOffset = 0 | ||
if timeZoneOffset in asset_record['fields']: | ||
timeZoneOffset = int(asset_record['fields']['timeZoneOffset']['value']) | ||
assetDate = datetime.fromtimestamp(int(asset_record['fields']['assetDate']['value'])/1000,tz=dateutil.tz.tzoffset(None, timeZoneOffset)) | ||
assetDateString = assetDate.strftime("%Y:%m:%d %H:%M:%S.%f%z") | ||
assetDateString = f"{assetDateString[:-2]}:{assetDateString[-2:]}" # Add a colon to timezone offset | ||
xmp_metadata["XMP-photoshop:DateCreated"] = assetDateString # Apple Photos uses this field when exporting an XMP sidecar | ||
xmp_metadata["CreateDate"] = assetDateString | ||
# Hidden or Deleted Photos should be marked as rejected (needs running as --album "Hidden" or --album "Recently Deleted") | ||
if (('isHidden' in asset_record['fields'] and asset_record['fields']['isHidden']['value'] == 1) or | ||
('isDeleted' in asset_record['fields'] and asset_record['fields']['isDeleted']['value'] == 1)): | ||
# -1 means rejected: https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata#image-rating | ||
xmp_metadata["Rating"] = -1 | ||
elif asset_record['fields']['isFavorite']['value'] == 1: #only mark photo as favorite if not hidden or deleted | ||
xmp_metadata["Rating"] = 5 | ||
|
||
args = ["-" + k + "=" + str(xmp_metadata[k]) for k in xmp_metadata] | ||
return args | ||
|
||
def generate_xmp_file(logger: logging.Logger, download_path: str, asset_record: dict[str, Any]) -> None: | ||
"""Generate XMP sidecar file from photo asset record""" | ||
if exif_tool: | ||
args = build_exiftool_arguments(asset_record) | ||
# json.dump(asset_record['fields'], open(download_path+".ar.json", "w"), indent=4) | ||
exif_tool.execute("-overwrite_original", download_path+".xmp", *args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from typing import Any, Dict | ||
from unittest import TestCase | ||
|
||
from icloudpd.xmp_sidecar import build_exiftool_arguments | ||
|
||
|
||
class BuildExifToolArguments(TestCase): | ||
def test_build_exiftool_arguments(self) -> None: | ||
assetRecordStub: Dict[str,Dict[str,Any]] = { | ||
'fields': { | ||
"captionEnc": { | ||
"value": "VGl0bGUgSGVyZQ==", | ||
"type": "ENCRYPTED_BYTES" | ||
}, | ||
"extendedDescEnc": { | ||
"value": "Q2FwdGlvbiBIZXJl", | ||
"type": "ENCRYPTED_BYTES" | ||
}, | ||
'orientation': { | ||
"value" : 6, | ||
"type" : "INT64" | ||
}, | ||
'assetSubtypeV2' : { | ||
"value" : 2, | ||
"type" : "INT64" | ||
}, | ||
"keywordsEnc": { | ||
"value": "YnBsaXN0MDChAVxzb21lIGtleXdvcmQICgAAAAAAAAEBAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAX", | ||
"type": "ENCRYPTED_BYTES" | ||
}, | ||
'locationEnc': { | ||
"value" : "YnBsaXN0MDDYAQIDBAUGBwgJCQoLCQwNCVZjb3Vyc2VVc3BlZWRTYWx0U2xvbld2ZXJ0QWNjU2xhdFl0aW1lc3RhbXBXaG9yekFjYyMAAAAAAAAAACNAdG9H6P0fpCNAWL2oZnRhiiNAMtKmTC+DezMAAAAAAAAAAAgZICYqLjY6RExVXmdwAAAAAAAAAQEAAAAAAAAADgAAAAAAAAAAAAAAAAAAAHk=", | ||
"type" : "ENCRYPTED_BYTES" | ||
}, | ||
'assetDate' : { | ||
"value" : 1532951050176, | ||
"type" : "TIMESTAMP" | ||
}, | ||
'isHidden': { | ||
"value" : 0, | ||
"type" : "INT64" | ||
}, | ||
'isDeleted': { | ||
"value" : 0, | ||
"type" : "INT64" | ||
}, | ||
'isFavorite': { | ||
"value" : 0, | ||
"type" : "INT64" | ||
}, | ||
}, | ||
} | ||
|
||
# Test full stub record | ||
args = build_exiftool_arguments(assetRecordStub) | ||
self.assertCountEqual(args , [ | ||
'-Title=Title Here', | ||
'-Description=Caption Here', | ||
'-IPTC:keywords=some keyword', | ||
'-GPSAltitude=326.9550561797753', | ||
'-GPSLatitude=18.82285', | ||
'-GPSLongitude=98.96340333333333', | ||
'-GPSSpeed=0.0', | ||
'-exif:GPSDateTime=2001:01:01 00:00:00.000000', | ||
'-XMP-photoshop:DateCreated=2018:07:30 11:44:10.176000+00:00', | ||
'-CreateDate=2018:07:30 11:44:10.176000+00:00', | ||
'-Orientation=6' | ||
]) | ||
|
||
# Test Screenshot Tagging | ||
assetRecordStub['fields']['assetSubtypeV2']['value'] = 3 | ||
args = build_exiftool_arguments(assetRecordStub) | ||
assert "-Make=Screenshot" in args | ||
assert "-DigitalSourceType=screenCapture" in args | ||
|
||
# Test Favorites | ||
assetRecordStub['fields']['isFavorite']['value'] = 1 | ||
args = build_exiftool_arguments(assetRecordStub) | ||
assert "-Rating=5" in args | ||
|
||
# Test Deleted | ||
assetRecordStub['fields']['isDeleted']['value'] = 1 | ||
args = build_exiftool_arguments(assetRecordStub) | ||
assert "-Rating=-1" in args | ||
|
||
# Test Hidden | ||
assetRecordStub['fields']['isDeleted']['value'] = 0 | ||
assetRecordStub['fields']['isHidden']['value'] = 1 | ||
args = build_exiftool_arguments(assetRecordStub) | ||
assert "-Rating=-1" in args |