TianyiMoe/backend/randpic/update_pics.py

21 lines
1.0 KiB
Python
Raw Normal View History

2024-04-29 15:05:51 +00:00
# Description: This script is used to update the list of pictures in the Azure Blob Storage container.
from azure.storage.blob import BlobServiceClient
import os
2024-04-30 01:41:56 +00:00
from .models import Picture
2024-04-30 01:54:47 +00:00
def update_pictures(container='tianyi-random', remotedir='', secretdir='/home/tianyi/repos/TianyiMoe/backend/secret.txt'):
2024-04-29 15:05:51 +00:00
# print(os.getcwd())
try:
with open(secretdir, '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)
existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=remotedir)]
2024-04-30 01:24:02 +00:00
for file in existing_files:
if not Picture.objects.filter(name=file.split('.')[0]).exists():
Picture.objects.create(name=file.split('.')[0])
2024-04-29 15:05:51 +00:00
print("File number: " + str(len(existing_files)))
except Exception as e:
print(e)
if __name__ == '__main__':
update_pictures()