Skip to content

Commit

Permalink
photos-to-sqlite upload photos.db dirname command, closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 18, 2020
1 parent 2ff303f commit 502efb9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
68 changes: 68 additions & 0 deletions photos_to_sqlite/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import click
import sqlite_utils
import boto3
import json
import pathlib
from .utils import calculate_hash


@click.group()
Expand Down Expand Up @@ -35,3 +37,69 @@ def s3_auth(auth):
}
)
open(auth, "w").write(json.dumps(auth_data, indent=4) + "\n")


@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument(
"directories",
nargs=-1,
type=click.Path(file_okay=False, dir_okay=True, allow_dash=False),
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def upload(db_path, directories, auth):
"Upload photos from directories to S3"
creds = json.load(open(auth))
db = sqlite_utils.Database(db_path)
client = boto3.client(
"s3",
aws_access_key_id=creds["photos_s3_access_key_id"],
aws_secret_access_key=creds["photos_s3_secret_access_key"],
)
uploads = db.table("photos", pk="sha256")
for directory in directories:
path = pathlib.Path(directory)
images = (
p.resolve()
for p in path.glob("**/*")
if p.suffix in [".jpg", ".jpeg", ".png", ".gif", ".heic"]
)
for filepath in images:
sha256 = calculate_hash(filepath)
ext = filepath.suffix.lstrip(".")
uploads.upsert({"sha256": sha256, "filepath": str(filepath), "ext": ext})
print(filepath)
keyname = "{}.{}".format(sha256, ext)
client.upload_file(
str(filepath),
"dogsheep-photos-simon",
keyname,
ExtraArgs={
"ContentType": {
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"heic": "image/heic",
}[ext]
},
)
print(
" ... uploaded: {}".format(
client.generate_presigned_url(
"get_object",
Params={"Bucket": "dogsheep-photos-simon", "Key": keyname,},
ExpiresIn=600,
)
)
)
14 changes: 14 additions & 0 deletions photos_to_sqlite/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import hashlib

HASH_BLOCK_SIZE = 1024 * 1024


def calculate_hash(path):
m = hashlib.sha256()
with path.open("rb") as fp:
while True:
data = fp.read(HASH_BLOCK_SIZE)
if not data:
break
m.update(data)
return m.hexdigest()

0 comments on commit 502efb9

Please sign in to comment.