initial commit

This commit is contained in:
ClF3 2024-04-20 10:14:37 +08:00
commit c3680d7fda
3 changed files with 55 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
secret.txt
original/
webp/

16
convert.py Normal file
View File

@ -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)

36
sync.py Normal file
View File

@ -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)