commit c3680d7fda7a85c177a7c260ced21ea3320e5811 Author: ClF3 Date: Sat Apr 20 10:14:37 2024 +0800 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..362f553 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +secret.txt +original/ +webp/ \ No newline at end of file diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..d8599b5 --- /dev/null +++ b/convert.py @@ -0,0 +1,16 @@ +import os +import sys +from PIL import Image +if __name__ == '__main__': + originalfiles = [f for f in os.listdir('original') if os.path.isfile(os.path.join('original', f))] + existingfiles = [f for f in os.listdir('webp') if os.path.isfile(os.path.join('webp', f))] + for f in originalfiles: + if f.split(".")[0] + '.webp' in existingfiles: + print("skipped: " + f) + continue + try: + im = Image.open('original/' + f).convert('RGB') + im.save('webp/' + f.split(".")[0] + '.webp', 'WEBP', quality=90) + print("optimized: " + f) + except OSError: + print("Falied to optimize the picture: " + f) \ No newline at end of file diff --git a/sync.py b/sync.py new file mode 100644 index 0000000..88b8b98 --- /dev/null +++ b/sync.py @@ -0,0 +1,36 @@ +import os, uuid +from azure.identity import DefaultAzureCredential +from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient + +container_name = "blog" +remote_path="posts/" +ignore_files = ["background.webp"] +img_path = "./webp" + +try: + with open('secret.txt', 'r') as f: + connect_str = f.read() + blob_service_client = BlobServiceClient.from_connection_string(connect_str) + container_client = blob_service_client.get_container_client(container_name) + existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=remote_path)] + # print("Existing files in the container:") + # print(existing_files) + for root,fir,files in os.walk(img_path): + for file in files: + if file in existing_files or file in ignore_files: + print("skipped: " + file) + continue + try: + print("Uploading: " + file) + img_path = os.path.join(root,file) + blob_client = blob_service_client.get_blob_client(container=container_name, blob=remote_path+file) + with open(file=img_path, mode="rb") as data: + blob_client.upload_blob(data) + except Exception as ex: + print(f"Error uploading {file}.") + print(ex) + print("Upload finished.") + +except Exception as ex: + print('Exception:') + print(ex) \ No newline at end of file