Skip to content

Commit

Permalink
Added date_original
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Aug 18, 2024
1 parent 1ffee1a commit 331e1e3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
7 changes: 7 additions & 0 deletions API_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,13 @@ Returns the original filename of the photo when it was imported to Photos. **No

Returns the create date of the photo as a datetime.datetime object

#### `date_original`

Returns the original creation date of the photo as a datetime.datetime object.
If user changed the photo's date in Photos, this will return the original date Photos assigned
as the creation date at the time of import.
Photos 5+ only; on Photos < 5.0, this will return the same value as `date`.

#### `date_added`

Returns the date the photo was added to the Photos library as a timezone aware datetime.datetime object, or None if the data added cannot be determined
Expand Down
19 changes: 16 additions & 3 deletions osxphotos/photoinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,24 @@ def original_filename(self) -> str:

@property
def date(self) -> datetime.datetime:
"""image creation date as timezone aware datetime object"""
"""Asset creation date as timezone aware datetime object"""
return self._info["imageDate"]

@property
def date_original(self) -> datetime.datetime:
"""Original creation date of asset as timezone aware datetime object.
If user has changed the asset's creation date in Photos, use this to access the original creation date
set when the asset was imported. Photos 5+; on Photos version < 5, returns the same value as `date`.
"""
if self._db._db_version <= _PHOTOS_4_VERSION:
return self.date
if self.exif_info and self.exif_info.date:
return self.exif_info.date
return self.date

@property
def date_modified(self) -> datetime.datetime | None:
"""image modification date as timezone aware datetime object
"""Asset modification date as timezone aware datetime object
or None if no modification date set"""

# Photos <= 4 provides no way to get date of adjustment and will update
Expand Down Expand Up @@ -1590,7 +1602,7 @@ def likes(self) -> list[LikeInfo]:
except:
return []

@property
@cached_property
def exif_info(self) -> ExifInfo | None:
"""Returns an ExifInfo object with the EXIF data for photo
Note: the returned EXIF data is the data Photos stores in the database on import;
Expand Down Expand Up @@ -2129,6 +2141,7 @@ def asdict(self, shallow: bool = True) -> dict[str, Any]:
dict_data["shared_library"] = self.shared_library
dict_data["rating"] = self.rating
dict_data["screen_recording"] = self.screen_recording
dict_data["date_original"] = self.date_original

return dict_data

Expand Down
8 changes: 8 additions & 0 deletions tests/test_mojave_10_14_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@

UUID_FINGERPRINT = {"6bxcNnzRQKGnK4uPrCJ9UQ": "ASs96bJvsunOg9Vxo5hK7VU3HegE"}

UUID_DATE_ORIGINAL = "URxYNAiXR9CTkXbUvWweAA"


@pytest.fixture(scope="module")
def photosdb():
Expand Down Expand Up @@ -701,3 +703,9 @@ def test_tables(photosdb: osxphotos.PhotosDB):
photo = photosdb.get_photo(UUID_DICT["favorite"])
tables = photo.tables()
assert tables is None


def test_date_original(photosdb: osxphotos.PhotosDB):
"""Test PhotoInfo.date_original, which on Photos <5 == PhotoInfo.date"""
photo = photosdb.get_photo(UUID_DATE_ORIGINAL)
assert photo.date_original == photo.date
18 changes: 18 additions & 0 deletions tests/test_sequoia_15_0_0_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@
}


UUID_DATE_ORIGINAL = [
(
"8846E3E6-8AC8-4857-8448-E3D025784410",
"1970-01-01 00:00:00+00:00",
"2020-05-12 18:47:13-04:00",
),
]

RawInfo = namedtuple(
"RawInfo",
[
Expand Down Expand Up @@ -1272,3 +1280,13 @@ def test_tables(photosdb: osxphotos.PhotosDB):
assert tables.ZADDITIONALASSETATTRIBUTES.ZTITLE[0] == photo.title
assert len(tables.ZASSET.rows()) == 1
assert tables.ZASSET.rows_dict()[0]["ZUUID"] == photo.uuid


@pytest.mark.parametrize("uuid,date,date_original", UUID_DATE_ORIGINAL)
def test_date_original(
photosdb: osxphotos.PhotosDB, uuid: str, date: str, date_original: str
):
"""Test PhotoInfo.date_original"""
photo = photosdb.get_photo(uuid)
assert photo.date == datetime.datetime.fromisoformat(date)
assert photo.date_original == datetime.datetime.fromisoformat(date_original)
18 changes: 18 additions & 0 deletions tests/test_sequoia_15_1_0_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@
}


UUID_DATE_ORIGINAL = [
(
"8846E3E6-8AC8-4857-8448-E3D025784410",
"1970-01-01 00:00:00+00:00",
"2020-05-12 18:47:13-04:00",
),
]

RawInfo = namedtuple(
"RawInfo",
[
Expand Down Expand Up @@ -1273,3 +1281,13 @@ def test_tables(photosdb: osxphotos.PhotosDB):
assert tables.ZADDITIONALASSETATTRIBUTES.ZTITLE[0] == photo.title
assert len(tables.ZASSET.rows()) == 1
assert tables.ZASSET.rows_dict()[0]["ZUUID"] == photo.uuid


@pytest.mark.parametrize("uuid,date,date_original", UUID_DATE_ORIGINAL)
def test_date_original(
photosdb: osxphotos.PhotosDB, uuid: str, date: str, date_original: str
):
"""Test PhotoInfo.date_original"""
photo = photosdb.get_photo(uuid)
assert photo.date == datetime.datetime.fromisoformat(date)
assert photo.date_original == datetime.datetime.fromisoformat(date_original)
17 changes: 17 additions & 0 deletions tests/test_ventura_13_0_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@
"4D521201-92AC-43E5-8F7C-59BC41C37A96": "public.jpeg",
}

UUID_DATE_ORIGINAL = [
(
"8846E3E6-8AC8-4857-8448-E3D025784410",
"1970-01-01 00:00:00+00:00",
"2020-05-12 18:47:13-04:00",
),
]

RawInfo = namedtuple(
"RawInfo",
Expand Down Expand Up @@ -1326,3 +1333,13 @@ def test_photosdb_library_path():
assert photosdb.library_path == PHOTOS_LIBRARY_PATH
assert photosdb.db_path == dbpath
assert len(photosdb) == PHOTOS_DB_LEN


@pytest.mark.parametrize("uuid,date,date_original", UUID_DATE_ORIGINAL)
def test_date_original(
photosdb: osxphotos.PhotosDB, uuid: str, date: str, date_original: str
):
"""Test PhotoInfo.date_original"""
photo = photosdb.get_photo(uuid)
assert photo.date == datetime.datetime.fromisoformat(date)
assert photo.date_original == datetime.datetime.fromisoformat(date_original)

0 comments on commit 331e1e3

Please sign in to comment.