From fc316bba8b9eb0709618c641d8fdff38aae390ed Mon Sep 17 00:00:00 2001 From: ClF3 Date: Tue, 30 Apr 2024 09:24:02 +0800 Subject: [PATCH] using database --- .gitignore | 1 + backend/randpic/migrations/0001_initial.py | 25 ++++++++++++++++++++++ backend/randpic/update_pics.py | 4 ++++ 3 files changed, 30 insertions(+) create mode 100644 backend/randpic/migrations/0001_initial.py diff --git a/.gitignore b/.gitignore index 879a4f3..4bc092e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ backend/__pycache__ backend/randpic/__pycache__ +backend/randpic/migrations/__pycache__ backend/tianyi/__pycache__ backend/db.sqlite3 backend/secret.txt diff --git a/backend/randpic/migrations/0001_initial.py b/backend/randpic/migrations/0001_initial.py new file mode 100644 index 0000000..fc53f7f --- /dev/null +++ b/backend/randpic/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 5.0.4 on 2024-04-30 01:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Picture', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('description', models.TextField()), + ('author', models.CharField(max_length=100)), + ('author_url', models.URLField()), + ('original_url', models.URLField()), + ], + ), + ] diff --git a/backend/randpic/update_pics.py b/backend/randpic/update_pics.py index d786dbc..6e66091 100644 --- a/backend/randpic/update_pics.py +++ b/backend/randpic/update_pics.py @@ -2,6 +2,7 @@ from azure.storage.blob import BlobServiceClient import os import json +from models import Picture 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: @@ -10,6 +11,9 @@ def update_pictures(container='tianyi-random', remotedir='', secretdir='/home/ti 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)] + for file in existing_files: + if not Picture.objects.filter(name=file.split('.')[0]).exists(): + Picture.objects.create(name=file.split('.')[0]) print("File number: " + str(len(existing_files))) with open(jsondir, 'w') as f: json.dump(existing_files, f, indent=4, sort_keys=True)