Skip to content

Commit

Permalink
Updates to Photo
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Sep 15, 2020
1 parent e0a60e4 commit e5ea6d5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 32 deletions.
23 changes: 18 additions & 5 deletions photoscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ def export(self, export_path, original=False, overwrite=False, timeout=120):
for photo in self.photos():
exported_photos.extend(
photo.export(
export_path=export_path, original=original, overwrite=overwrite, timeout=timeout
export_path=export_path,
original=original,
overwrite=overwrite,
timeout=timeout,
)
)
return exported_photos
Expand Down Expand Up @@ -666,10 +669,10 @@ def __init__(self, uuid):
id_ = f"{uuid}{UUID_SUFFIX_FOLDER}"
else:
uuid = uuid.split("/")[0]

valid_folder = run_script("_folder_exists", id_)
if valid_folder:
self.id = id_
self.id = id_
self._uuid = uuid
else:
raise ValueError(f"Invalid folder id: {uuid}")
Expand Down Expand Up @@ -811,7 +814,7 @@ def __init__(self, uuid):
uuid = uuid.split("/")[0]
valid = run_script("_photo_exists", uuid)
if valid:
self.id = id_
self.id = id_
self._uuid = uuid
else:
raise ValueError(f"Invalid photo id: {uuid}")
Expand Down Expand Up @@ -895,7 +898,8 @@ def width(self):
@property
def altitude(self):
""" GPS altitude of photo in meters """
return run_script("_photo_altitude", self.id)
altitude = run_script("_photo_altitude", self.id)
return altitude if altitude != kMissingValue else None

@property
def location(self):
Expand Down Expand Up @@ -936,6 +940,15 @@ def date(self):
""" date of photo as timezone-naive datetime.datetime object """
return run_script("_photo_date", self.id)

@date.setter
def date(self, date):
""" Set date of photo as timezone-naive datetime.datetime object
Args:
date: timezone-naive datetime.datetime object
"""
return run_script("_photo_set_date", self.id, date)

@property
def filename(self):
""" filename of photo """
Expand Down
2 changes: 1 addition & 1 deletion photoscript/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" version info """

__version__ = "0.0.19"
__version__ = "0.0.20"
28 changes: 25 additions & 3 deletions photoscript/photoscript.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,21 @@ on _photo_date(_id)
end tell
end _photo_date

on _photo_set_date(id_, date_)
(* set date of photo *)
_photoslibrary_waitforphotos(WAIT_FOR_PHOTOS)
set count_ to 0
repeat while count_ < MAX_RETRY
tell application "Photos"
set date of media item id (id_) to date_
if date of media item id (id_) = date_ then
return date_
end if
end tell
set count_ to count_ + 1
end repeat
end _photo_set_date

on _photo_height(id_)
(* height of photo in pixels *)
_photoslibrary_waitforphotos(WAIT_FOR_PHOTOS)
Expand Down Expand Up @@ -1047,9 +1062,16 @@ end _photo_location
on _photo_set_location(id_, location_)
(* set GPS location of photo *)
_photoslibrary_waitforphotos(WAIT_FOR_PHOTOS)
tell application "Photos"
set location of media item id (id_) to location_
end tell
set count_ to 0
repeat while count_ < MAX_RETRY
tell application "Photos"
set location of media item id (id_) to location_
if location of media item id (id_) = location_ then
return location_
end if
end tell
set count_ to count_ + 1
end repeat
end _photo_set_location

on _photo_export(theUUID, thePath, original, edited, theTimeOut)
Expand Down
13 changes: 13 additions & 0 deletions tests/export_photo_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
""" Use osxphotos to export details to build PHOTOS_DICT for tests """

import osxphotos

photosdb = osxphotos.PhotosDB()
for p in photosdb.photos():
print(
f'{{"uuid": "{p.uuid}/L0/001", "uuid_osxphotos": "{p.uuid}", '
f'"filename": "{p.original_filename}", "favorite": {p.favorite}, '
f'"keywords": {p.keywords}, "height": {p.height}, "width": {p.width}, '
f'"location": {p.location}, "date": "{p.date}" }},'
)

70 changes: 47 additions & 23 deletions tests/photoscript_config_catalina.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,63 @@

PHOTOS_DICT = [
{
"uuid": "F8EFA39F-7D26-4DC2-82FE-CC9357F19F00/L0/001",
"uuid_osxphotos": "F8EFA39F-7D26-4DC2-82FE-CC9357F19F00",
"keywords": ["travel"],
"filename": "IMG_2774.JPG",
"title": None,
"description": None,
"uuid": "B6DB996D-8A0A-4983-AFBD-D206B7D38A23/L0/001",
"uuid_osxphotos": "B6DB996D-8A0A-4983-AFBD-D206B7D38A23",
"filename": "IMG_2510.JPG",
"favorite": False,
"keywords": [],
"height": 3024,
"width": 4032,
"location": (33.82677, -118.32748332999999),
"date": "2020-07-18 12:00:20",
"altitude": 26.1,
},
{
"uuid": "1CD1B172-C94B-4093-A303-EE24FE7EEF60/L0/001",
"uuid_osxphotos": "1CD1B172-C94B-4093-A303-EE24FE7EEF60",
"filename": "IMG_2242.JPG",
"favorite": True,
"keywords": [],
"height": 3024,
"width": 3024,
"location": (33.782425, -118.34803833000001),
"date": "2020-07-05 10:07:04",
"altitude": 130.6,
},
{
"uuid": "EECD91FE-D716-48F2-A62C-A4D558ACD52E/L0/001",
"uuid_osxphotos": "EECD91FE-D716-48F2-A62C-A4D558ACD52E",
"keywords": ["travel"],
"filename": "IMG_2768.JPG",
"title": None,
"description": None,
"favorite": True,
"favorite": False,
"keywords": ["travel"],
"height": 3024,
"width": 3024,
"location": (33.501921669999994, -117.66425333000002),
"date": "2020-08-12 14:12:15",
"altitude": 32.9,
},
{
"uuid": "B6DB996D-8A0A-4983-AFBD-D206B7D38A23/L0/001",
"uuid_osxphotos": "B6DB996D-8A0A-4983-AFBD-D206B7D38A23",
"uuid": "3A71DE26-EDEF-41D3-86C1-E8328DFC9FA0/L0/001",
"uuid_osxphotos": "3A71DE26-EDEF-41D3-86C1-E8328DFC9FA0",
"filename": "IMG_0096.jpeg",
"favorite": False,
"keywords": [],
"filename": "IMG_2510.JPG",
"title": None,
"description": None,
"favorite": True,
"height": 903,
"width": 701,
"location": (None, None),
"date": "2020-09-07 07:27:40",
"altitude": None,
},
{
"uuid": "1CD1B172-C94B-4093-A303-EE24FE7EEF60/L0/001",
"uuid_osxphotos": "1CD1B172-C94B-4093-A303-EE24FE7EEF60",
"keywords": [],
"filename": "IMG_2242.JPG",
"title": "Living Wall",
"description": "Wall of plants at Botanical Garden",
"favorite": False,
"uuid": "F8EFA39F-7D26-4DC2-82FE-CC9357F19F00/L0/001",
"uuid_osxphotos": "F8EFA39F-7D26-4DC2-82FE-CC9357F19F00",
"filename": "IMG_2774.JPG",
"favorite": True,
"keywords": ["travel"],
"height": 4032,
"width": 3024,
"location": (33.50126666999999, -117.66378833000002),
"date": "2020-08-12 15:43:32",
"altitude": 29.4,
},
]
59 changes: 59 additions & 0 deletions tests/test_4_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,62 @@ def test_photo_keywords(photoslib):
assert sorted(photo_obj.keywords) == sorted(["Foo", "Bar"])
photo_obj.keywords = []
assert photo_obj.keywords == []


def test_photo_favorite(photoslib):
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
assert photo_obj.favorite == photo["favorite"]
photo_obj.favorite = not photo["favorite"]
assert photo_obj.favorite != photo["favorite"]


def test_photo_height_width(photoslib):
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
assert photo_obj.height == photo["height"]
assert photo_obj.width == photo["width"]


def test_photo_altitude(photoslib):
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
assert photo_obj.altitude == photo["altitude"]


def test_photo_location(photoslib):
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
lat, lon = photo_obj.location
assert (lat, lon) == photo["location"]
photo_obj.location = (34.0, -118.0)
assert photo_obj.location == (34.0, -118.0)
photo_obj.location = (None, None)
assert photo_obj.location == (None, None)


def test_photo_date(photoslib):
import datetime
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
date = datetime.datetime.fromisoformat(photo["date"])
assert photo_obj.date == date
photo_obj.date = datetime.datetime(2020, 9, 14)
assert photo_obj.date == datetime.datetime(2020, 9, 14)

def test_photo_filename(photoslib):
import photoscript

for photo in PHOTOS_DICT:
photo_obj = photoscript.Photo(photo["uuid"])
assert photo_obj.filename == photo["filename"]

0 comments on commit e5ea6d5

Please sign in to comment.