2022-08-26 04:27:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Helper script to upload pics to my photoblog."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
from urllib.request import Request, urlopen
|
|
|
|
from base64 import b64decode
|
|
|
|
|
|
|
|
HOSTNAME = b64decode(b'aW50cm92ZXJ0bnVkaXN0LmNvbQ==').decode()
|
|
|
|
USERNAME = b64decode(b'Ym9pY3VkaQ==').decode()
|
|
|
|
RE_FILENAME = re.compile(r'^(\d{2}\w{2}\d{3})[^\d]?.*?\.(gif|jpg|png)$')
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
# Map original files to short basenames.
|
|
|
|
file_map = dict()
|
|
|
|
for name in args.files:
|
|
|
|
basename = os.path.basename(name)
|
|
|
|
if args.custom:
|
|
|
|
file_map[name] = basename
|
|
|
|
continue
|
|
|
|
|
|
|
|
m = RE_FILENAME.match(name)
|
|
|
|
if not m:
|
|
|
|
print(f"File '{name}' doesn't match usual naming convention. Use --custom to upload custom file names.")
|
2023-09-17 19:47:53 +00:00
|
|
|
continue
|
2023-04-29 16:51:43 +00:00
|
|
|
|
2022-08-26 04:27:34 +00:00
|
|
|
file_map[name] = f"{m.group(1)}.{m.group(2)}"
|
2023-04-29 16:51:43 +00:00
|
|
|
|
2022-08-26 04:27:34 +00:00
|
|
|
print(f"Going to photoblog: {list(file_map.values())}")
|
|
|
|
|
|
|
|
# Test each file doesn't already exist.
|
2023-04-29 16:51:43 +00:00
|
|
|
not_existing = list()
|
2022-08-26 04:27:34 +00:00
|
|
|
if not args.lazy:
|
|
|
|
print(f"Checking each file isn't already uploaded...")
|
|
|
|
problems = False
|
|
|
|
for name in file_map.values():
|
|
|
|
test_url = f'https://{HOSTNAME}/static/photos/{name}'
|
|
|
|
req = Request(test_url, method='HEAD')
|
|
|
|
|
|
|
|
try:
|
|
|
|
resp = urlopen(req, timeout=10)
|
|
|
|
status = resp.status if hasattr(resp, 'status') else resp.getstatus()
|
|
|
|
if status == 200:
|
|
|
|
print(f"Warning: file '{name}' already existed at '{test_url}' and won't be uploaded without --lazy")
|
|
|
|
problems = True
|
|
|
|
except:
|
2023-04-29 16:51:43 +00:00
|
|
|
not_existing.append(name)
|
2022-08-26 04:27:34 +00:00
|
|
|
pass
|
2023-04-29 16:51:43 +00:00
|
|
|
|
|
|
|
print(f"New files not found on the blog: {not_existing}")
|
|
|
|
|
2023-09-17 19:47:53 +00:00
|
|
|
# Doing a copy-to?
|
|
|
|
if args.copy_to:
|
|
|
|
if os.path.isfile(args.copy_to) and not os.path.isdir(args.copy_to):
|
|
|
|
print(f"Error: {args.copy_to} already exists and is not a folder")
|
|
|
|
quit()
|
|
|
|
|
|
|
|
if not os.path.isdir(args.copy_to):
|
|
|
|
os.makedirs(args.copy_to)
|
|
|
|
|
|
|
|
for ours, theirs in file_map.items():
|
|
|
|
if not theirs in not_existing:
|
|
|
|
continue
|
|
|
|
cmd = ["cp", ours, f"{args.copy_to}/{theirs}"]
|
|
|
|
print("Exec:", " ".join(cmd))
|
|
|
|
subprocess.run(cmd)
|
|
|
|
quit()
|
|
|
|
|
2022-08-26 04:27:34 +00:00
|
|
|
if problems:
|
|
|
|
quit()
|
2023-04-29 16:51:43 +00:00
|
|
|
|
2022-08-26 04:27:34 +00:00
|
|
|
print("Looks good! Sending to server...")
|
|
|
|
if args.test:
|
|
|
|
print("NOTE: Test run (--test), not actually uploading")
|
|
|
|
|
|
|
|
for ours, theirs in file_map.items():
|
2023-09-17 19:47:53 +00:00
|
|
|
if not theirs in not_existing:
|
|
|
|
continue
|
2022-08-26 04:27:34 +00:00
|
|
|
cmd = ["scp", ours, f"{USERNAME}@{HOSTNAME}:www/static/photos/{theirs}"]
|
|
|
|
print("Exec:", " ".join(cmd))
|
|
|
|
if not args.test:
|
|
|
|
subprocess.run(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser("to-photoblog")
|
|
|
|
parser.add_argument("--custom", "-c",
|
|
|
|
action="store_true",
|
|
|
|
help="Upload files with custom names as-is without validation",
|
|
|
|
)
|
|
|
|
parser.add_argument("--lazy", "-l",
|
|
|
|
action="store_true",
|
|
|
|
help="Don't validate the image was already uploaded, just upload it",
|
|
|
|
)
|
|
|
|
parser.add_argument("--test", "-t",
|
|
|
|
action="store_true",
|
|
|
|
help="Test run; do not actually upload files",
|
|
|
|
)
|
2023-09-17 19:47:53 +00:00
|
|
|
parser.add_argument("--copy-to", "-cp",
|
|
|
|
type=str,
|
|
|
|
help="Instead of scp upload, copy the files to another folder on disk",
|
|
|
|
)
|
2022-08-26 04:27:34 +00:00
|
|
|
parser.add_argument("files",
|
|
|
|
type=str,
|
|
|
|
nargs='+',
|
|
|
|
help='Files to upload, should be in my naming convention',
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2023-04-29 16:51:43 +00:00
|
|
|
main(args)
|