added argparse to sync.py

This commit is contained in:
ClF3 2024-04-24 11:01:26 +08:00
parent 0544602000
commit 79e3836a51
2 changed files with 15 additions and 10 deletions

23
sync.py
View File

@ -1,29 +1,34 @@
import os, uuid import os, uuid
from azure.identity import DefaultAzureCredential from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--container', help='Name of the container', default='blog')
parser.add_argument('--remote', help='Path to the remote images', default='posts')
parser.add_argument('--local', help='Path to the local images', default='webp')
parser.add_argument('--ignore', help='Files to ignore', default='background.webp', action='append')
args=parser.parse_args()
container_name = "blog" if args.remote[-1] != '/':
remote_path="posts/" args.remote += '/'
ignore_files = ["background.webp"]
img_path = "./webp"
try: try:
with open('secret.txt', 'r') as f: with open('secret.txt', 'r') as f:
connect_str = f.read() connect_str = f.read()
blob_service_client = BlobServiceClient.from_connection_string(connect_str) blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name) container_client = blob_service_client.get_container_client(args.container)
existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=remote_path)] existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=args.remote)]
# print("Existing files in the container:") # print("Existing files in the container:")
# print(existing_files) # print(existing_files)
for root,fir,files in os.walk(img_path): for root,fir,files in os.walk(args.local):
for file in files: for file in files:
if file in existing_files or file in ignore_files: if file in existing_files or file in args.ignore:
print("skipped: " + file) print("skipped: " + file)
continue continue
try: try:
print("Uploading: " + file) print("Uploading: " + file)
img_path = os.path.join(root,file) img_path = os.path.join(root,file)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=remote_path+file) blob_client = blob_service_client.get_blob_client(container=args.container, blob=args.remote+file)
with open(file=img_path, mode="rb") as data: with open(file=img_path, mode="rb") as data:
blob_client.upload_blob(data) blob_client.upload_blob(data)
except Exception as ex: except Exception as ex: