From 92e67aca0b79e71737b59a3102270d29cb77cb8b Mon Sep 17 00:00:00 2001 From: ClF3 Date: Tue, 30 Apr 2024 13:40:39 +0800 Subject: [PATCH] new api to get metadata --- backend/randpic/random_picture.py | 10 +++++++++- backend/randpic/urls.py | 1 + backend/randpic/views.py | 7 +++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/randpic/random_picture.py b/backend/randpic/random_picture.py index 45dfd6e..35aca28 100644 --- a/backend/randpic/random_picture.py +++ b/backend/randpic/random_picture.py @@ -5,4 +5,12 @@ from .models import Picture pics=Picture.objects.order_by("?") def get_random_picture_url(): - return "https://cdn.clf3.org/tianyi-random/"+pics[0].name+".webp" \ No newline at end of file + return "https://cdn.clf3.org/tianyi-random/"+pics[0].name+".webp" +def get_random_picture_meta(): + return { + "name":pics[0].name, + "description":pics[0].description, + "author":pics[0].author, + "author_url":pics[0].author_url, + "original_url":pics[0].original_url + } \ No newline at end of file diff --git a/backend/randpic/urls.py b/backend/randpic/urls.py index a9d7f56..fca3244 100644 --- a/backend/randpic/urls.py +++ b/backend/randpic/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ path("", views.index, name="index"), + path("metadata", views.metadata, name="metadata") ] \ No newline at end of file diff --git a/backend/randpic/views.py b/backend/randpic/views.py index 608b1fe..d5d6d45 100644 --- a/backend/randpic/views.py +++ b/backend/randpic/views.py @@ -1,6 +1,9 @@ from django.shortcuts import render, redirect from django.http import HttpResponse -from .random_picture import get_random_picture_url +from .random_picture import get_random_picture_url, get_random_picture_meta +import json # Create your views here. def index(request): - return redirect(get_random_picture_url()) \ No newline at end of file + return redirect(get_random_picture_url()) +def metadata(request): + return HttpResponse(json.dumps(get_random_picture_meta()), content_type="application/json") \ No newline at end of file