74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
import argparse
|
|
import requests
|
|
from requests.exceptions import HTTPError
|
|
from photoapp.utils import get_extension
|
|
from photoapp.types import known_extensions
|
|
|
|
|
|
class PhotoApiClient(object):
|
|
def __init__(self, base_url):
|
|
self.session = requests.Session()
|
|
self.base_url = base_url
|
|
|
|
def byhash(self, sha):
|
|
return self.get("byhash", params={"sha": sha}).json()
|
|
|
|
def get(self, url, **params):
|
|
resp = self.session.get(self.base_url + "/api/v1/" + url, **params)
|
|
resp.raise_for_status()
|
|
return resp
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="photo library cli")
|
|
parser.add_argument("-s", "--host", required=True, help="photo library server address")
|
|
|
|
sp_action = parser.add_subparsers(dest="action", help="action to take")
|
|
|
|
p_dupes = sp_action.add_parser("checkdupes", help="check if files/hash lists are already in the library")
|
|
p_dupes.add_argument("--sha-files", action="store_true", help="read hashes from a file instead of hashing images")
|
|
p_dupes.add_argument("files", nargs="+", help="files to check")
|
|
|
|
p_ingest = sp_action.add_parser("ingest", help="import images into the library")
|
|
p_ingest.add_argument("files", nargs="+", help="files to import")
|
|
p_ingest.add_argument("-c", "--copy-of", help="existing uuid the imported images will be placed under")
|
|
|
|
args = parser.parse_args()
|
|
|
|
client = PhotoApiClient(args.host)
|
|
|
|
if args.action == "checkdupes":
|
|
hashes = {}
|
|
if args.sha_files:
|
|
# sha file format should look like the output of `find ./source -type f -exec shasum -a 256 {} \;`:
|
|
# e931d286a8377ea8f49ee928e793d9e1fd18a25402cac43afdee3eec3b3b18a5 source/2018-12-26 20.54.40.jpg
|
|
# 24c1cd20c961839f14d608f5719c4f380af902c4774ddb7cb9ff747f652ca302 source/2018-12-30 22.39.27.jpg
|
|
for fname in args.files:
|
|
with open(fname) as f:
|
|
for line in f.readlines():
|
|
if not line:
|
|
continue
|
|
sha, path = line.split(maxsplit=1)
|
|
path = path.rstrip("\n")
|
|
if get_extension(path) not in known_extensions:
|
|
continue
|
|
hashes[sha] = path
|
|
else:
|
|
raise NotImplementedError("must pass --sha-files for now")
|
|
|
|
for sha, path in hashes.items():
|
|
# hit http://localhost:8080/api/v1/byhash?sha=afe49172f709725a4503c9219fb4c6a9db8ad0354fc493f2f500269ac6faeaf6
|
|
try:
|
|
client.byhash(sha)
|
|
# if the file is a dupe, do nothing
|
|
except HTTPError as he:
|
|
# if the file is original, print its path
|
|
if he.response.status_code == 404:
|
|
print(path)
|
|
else:
|
|
raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|