added to_json() to PhotoInfo

This commit is contained in:
Rhet Turnbull 2019-08-18 15:21:28 -07:00
parent 07c9c31a09
commit be3d6dee8d
4 changed files with 62 additions and 26 deletions

View File

@ -29,16 +29,35 @@ Usage: osxphotos [OPTIONS] COMMAND [ARGS]...
Options:
--db <Photos database path> Specify database file
--json Print output in JSON format
--help Show this message and exit.
-h, --help Show this message and exit.
Commands:
albums print out albums found in the Photos library
dump print list of all photos & associated info from the Photos...
help print help; for help on commands: help <command>
info print out descriptive info of the Photos library database
keywords print out keywords found in the Photos library
persons print out persons (faces) found in the Photos library
query query the Photos database using 1 or more search options
```
To get help on a specific command, use `osxphotos help <command_name>`
```
$ osxphotos help query
Usage: osxphotos help [OPTIONS]
query the Photos database using 1 or more search options
Options:
--keyword TEXT search for keyword(s)
--person TEXT search for person(s)
--album TEXT search for album(s)
--uuid TEXT search for UUID(s)
-h, --help Show this message and exit.
```
## Example uses of the module
```python
@ -283,6 +302,9 @@ Returns `True` if the original image file is missing on disk, otherwise `False`.
#### `hasadjustments()`
Returns `True` if the file has been edited in Photos, otherwise `False`
#### `to_json()`
Returns a JSON representation of all photo info
Examples:
```python

View File

@ -9,6 +9,7 @@ import sys
from shutil import copyfile
import pprint
import sqlite3
import json
import yaml
import objc
@ -27,7 +28,7 @@ from . import _applescript
# which Photos library database versions have been tested
# Photos 3.0 (10.13.6) == 3301
# Photos 4.0 (10.14.5) == 4016
# Photos 4.0 (10.4.6) == 4025
# Photos 4.0 (10.4.6) == 4025
# TODO: Should this also use compatibleBackToVersion from LiGlobals?
_TESTED_DB_VERSIONS = ["4025", "4016", "3301"]
@ -594,6 +595,8 @@ class PhotosDB:
def __repr__(self):
return f"osxphotos.PhotosDB(dbfile='{self.get_db_path()}')"
"""
Info about a specific photo, contains all the details we know about the photo
including keywords, persons, albums, uuid, path, etc.
@ -682,7 +685,24 @@ class PhotoInfo:
"ismissing": self.ismissing(),
"hasadjustments": self.hasadjustments(),
}
return yaml.dump(info,sort_keys=False)
return yaml.dump(info, sort_keys=False)
def to_json(self):
""" return JSON representation """
pic = {
"uuid": self.uuid(),
"filename": self.filename(),
"date": str(self.date()),
"description": self.description(),
"name": self.name(),
"keywords": self.keywords(),
"albums": self.albums(),
"persons": self.persons(),
"path": self.path(),
"ismissing": self.ismissing(),
"hasadjustments": self.hasadjustments(),
}
return json.dumps(pic)
# compare two PhotoInfo objects for equality
def __eq__(self, other):

View File

@ -12,7 +12,10 @@ class CLI_Obj:
self.photosdb = osxphotos.PhotosDB(dbfile=db)
self.json = json
CTX_SETTINGS=dict(help_option_names=['-h','--help'])
CTX_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.group(context_settings=CTX_SETTINGS)
@click.option(
"--db",
@ -103,29 +106,16 @@ def dump(cli_obj):
pdb = cli_obj.photosdb
photos = pdb.photos()
csv_writer = csv.writer(
sys.stdout, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
if cli_obj.json:
dump = []
for p in photos:
dump.append(
{
"uuid": p.uuid(),
"filename": p.filename(),
"date": str(p.date()),
"description": p.description(),
"name": p.name(),
"keywords": p.keywords(),
"albums": p.albums(),
"persons": p.persons(),
"path": p.path(),
"ismissing": p.ismissing(),
"hasadjustments": p.hasadjustments(),
}
)
print(json.dumps(dump))
dump.append(p.to_json())
print(f"[{', '.join(dump)}]")
else:
# dump as CSV
csv_writer = csv.writer(
sys.stdout, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
dump = []
# add headers
dump.append(
@ -172,12 +162,15 @@ def dump(cli_obj):
@click.pass_context
def query(ctx, cli_obj, keyword, person, album, uuid):
""" query the Photos database using 1 or more search options """
photos = cli_obj.photosdb.photos(keywords=keyword, persons=person, albums=album, uuid=uuid)
photos = cli_obj.photosdb.photos(
keywords=keyword, persons=person, albums=album, uuid=uuid
)
print(photos)
pass
@cli.command()
@click.argument('topic', default=None, required=False, nargs=1 )
@click.argument("topic", default=None, required=False, nargs=1)
@click.pass_context
def help(ctx, topic, **kw):
""" print help; for help on commands: help <command> """
@ -186,5 +179,6 @@ def help(ctx, topic, **kw):
else:
print(cli.commands[topic].get_help(ctx))
if __name__ == "__main__":
cli()

View File

@ -38,7 +38,7 @@ with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
setup(
name="osxphotos",
version="0.10.7",
version="0.10.71",
description="Manipulate (read-only) Apple's Photos app library on Mac OS X",
long_description=long_description,
long_description_content_type="text/markdown",