Check and update example code in the API docs and examples folder
documentation
The [export example in the docs](https://rhettbull.github.io/osxphotos/package_overview.html) can fail if edited path is None. Here's a better example:
```python
"""Export all photos to specified directory using album names as folders
If file has been edited, also export the edited version,
otherwise, export the original version
This will result in duplicate photos if photo is in more than album
This is not a complete export utility but is meant to show how to use the OSXPhotos API
"""
import os.path
import sys
import click
from pathvalidate import is_valid_filepath, sanitize_filepath
import osxphotos
@click.command()
@click.argument("export_path", type=click.Path(exists=True))
@click.option(
"--default-album",
help="Default folder for photos with no album. Defaults to 'unfiled'",
default="unfiled",
)
@click.option(
"--library-path",
help="Path to Photos library, default to last used library",
default=None,
)
def export(export_path, default_album, library_path):
export_path = os.path.expanduser(export_path)
library_path = os.path.expanduser(library_path) if library_path else None
if library_path is not None:
photosdb = osxphotos.PhotosDB(library_path)
else:
photosdb = osxphotos.PhotosDB()
photos = photosdb.photos()
for p in photos:
albums = p.albums
if not albums:
albums = [default_album]
for album in albums:
click.echo(f"Exporting {p.filename} in album {album}")
# make sure no invalid characters in destination path (could be in album name)
album_name = sanitize_filepath(album, platform="auto")
# create destination folder, if necessary, based on album name
dest_dir = os.path.join(export_path, album_name)
# verify path is a valid path
if not is_valid_filepath(dest_dir, platform="auto"):
sys.exit(f"Invalid filepath {dest_dir}")
# create destination dir if needed
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
# export the photo
# export unedited version
exported = p.export(
dest_dir, use_photos_export=p.ismissing or not p.path
)
if exported:
click.echo(f"Exported {p.original_filename} to {exported}")
else:
click.echo(
f"Error exporting {p.original_filename}, no files exported"
)
if p.hasadjustments:
# export edited version
exported = p.export(
dest_dir,
edited=True,
use_photos_export=p.ismissing or not p.path_edited,
)
if exported:
click.echo(
f"Exported edited version of {p.original_filename} to {exported}"
)
else:
click.echo(
f"Error exporting {p.original_filename}, no files exported"
)
if __name__ == "__main__":
export()
```
Really should check all the eamples and make sure they still work and don't throw unnecessary errors.
0 条评论