added cronjobs

This commit is contained in:
tianyi 2024-04-29 15:05:51 +00:00
parent e69bb3fce2
commit 070ef5b846
4 changed files with 29 additions and 20 deletions

4
.gitignore vendored
View File

@ -1,5 +1,7 @@
backend/__pycache__
backend/randpic/__pycache__
backend/tianyi/__pycache__
backend/db.sqlite3
backend/secret.txt
backend/jsons/pics.json
backend/jsons
backend/logs

View File

@ -0,0 +1,20 @@
# 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
import json
def update_pictures(container='tianyi-random', remotedir='', secretdir='/home/tianyi/repos/TianyiMoe/backend/secret.txt', jsondir='/home/tianyi/repos/TianyiMoe/backend/jsons/pics.json'):
# 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)]
print("File number: " + str(len(existing_files)))
with open(jsondir, 'w') as f:
json.dump(existing_files, f, indent=4, sort_keys=True)
except Exception as e:
print(e)
if __name__ == '__main__':
update_pictures()

View File

@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_crontab',
]
MIDDLEWARE = [
@ -121,3 +122,8 @@ STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Crontab settings
CRONJOBS = [
('* * * * *', 'randpic.update_pics.update_pictures', '>> /home/tianyi/repos/TianyiMoe/backend/logs/crontab.log')
]

View File

@ -1,19 +0,0 @@
# Description: This script is used to update the list of pictures in the Azure Blob Storage container.
from azure.storage.blob import BlobServiceClient
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument('--container', type=str, default="tianyi-random")
parser.add_argument('--remotedir', type=str, default="")
args=parser.parse_args()
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(args.container)
existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=args.remotedir)]
with open('jsons/pics.json', 'w') as f:
json.dump(existing_files, f, indent=4, sort_keys=True)
except Exception as e:
print(e)
exit(1)