From b10eaf920292bccb1cd5d18d76b8a3215df2d937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Snorre=20Alvsv=C3=A5g?= <55574575+snorrealv@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:30:37 +0200 Subject: [PATCH] Added Cache for images --- .../commands/cache-recommended-movies.py | 57 +++++++++++++++++++ backend/web/handler/models.py | 7 ++- backend/web/handler/serializers.py | 2 + frontend/src/components/Dashboard.astro | 26 ++++++++- 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 backend/web/handler/management/commands/cache-recommended-movies.py diff --git a/backend/web/handler/management/commands/cache-recommended-movies.py b/backend/web/handler/management/commands/cache-recommended-movies.py new file mode 100644 index 0000000..3f706c2 --- /dev/null +++ b/backend/web/handler/management/commands/cache-recommended-movies.py @@ -0,0 +1,57 @@ +from django.core.management.base import BaseCommand, CommandError +from handler.models import Recommendations, MoviesRanked +import os +import requests +import random +class Command(BaseCommand): + help = 'Caches image links for movies that has a rank or has been recommended to a user' + + + + def add_arguments(self, parser): + parser.add_argument('--single-user', + type=str, + action='store', + help='If you only want to get one user', + default='', + dest='single_user' + ) + + parser.add_argument('--overwrite', + type=bool, + action='store', + help='If you want to overwrite previously cached images', + default=False, + dest='overwrite' + ) + + parser.add_argument('--clear-cache', + type=bool, + action='store', + help='To clear all cached images', + default=False, + dest='clear-cache' + ) + + def handle(self, *args, **options): + if options['clear-cache']: + ranked_movies = MoviesRanked.objects.all().delete() + else: + TMDB_KEY = os.environ.get('TMDB_KEY') + try: + ranked_movies = MoviesRanked.objects.all() + n = len(ranked_movies) + i = 0 + for ranked_movie in ranked_movies: + if ranked_movie.cached_img_url != "not_cached" or options['overwrite']: + self.stdout.write(f'{i}/{n} || Movie {ranked_movie.movie.title} already cached.') + else: + api_url = f'https://api.themoviedb.org/3/movie/{ranked_movie.tmdbId}?api_key={TMDB_KEY}' + response = requests.get(api_url) + ranked_movie.cached_img_url = response.json()["poster_path"] + ranked_movie.save() + self.stdout.write(f'{i}/{n} || Movie {ranked_movie.movie.title}, https://image.tmdb.org/t/p/w200/{response.json()["backdrop_path"]}') + + i += 1 + except Exception as e: + self.stdout.write('Something went wrong: \n' + e) \ No newline at end of file diff --git a/backend/web/handler/models.py b/backend/web/handler/models.py index 410aafb..a222630 100644 --- a/backend/web/handler/models.py +++ b/backend/web/handler/models.py @@ -27,7 +27,9 @@ class MoviesRanked(models.Model): movie = models.ForeignKey("handler.Movies", on_delete=models.CASCADE) tmdbId = models.IntegerField() rank = models.IntegerField() - + cached_img_url = models.CharField(default='not_cached', max_length=400) + cached_background_img_url = models.CharField(default='not_cached', max_length=400) + class Movies(models.Model): movieId = models.IntegerField(primary_key=True) title = models.CharField(max_length=200) @@ -77,8 +79,7 @@ class Recommendations(models.Model): def movies_default(): return {"movies":["a","b","c"]} - - #movies = models.JSONField('Recommendation', default=movies_default) + movies = models.ManyToManyField(MoviesRanked) user_description_short = models.CharField(default='', max_length=500) diff --git a/backend/web/handler/serializers.py b/backend/web/handler/serializers.py index 0f08c6b..518ce25 100644 --- a/backend/web/handler/serializers.py +++ b/backend/web/handler/serializers.py @@ -41,6 +41,8 @@ class MovieSerializer(serializers.Serializer): class MovieRankedSerializer(serializers.Serializer): tmdbId = serializers.IntegerField() rank = serializers.IntegerField() + cached_img_url = serializers.CharField() + cached_background_img_url = serializers.CharField() movie = MovieSerializer() diff --git a/frontend/src/components/Dashboard.astro b/frontend/src/components/Dashboard.astro index 4656004..ce23d94 100644 --- a/frontend/src/components/Dashboard.astro +++ b/frontend/src/components/Dashboard.astro @@ -49,7 +49,7 @@ const {url=settings.API_SERVER_URL} = Astro.props;
-

Judging by the rating history, this person seems to prefer very popular mainstream movies. Clasically, such users are treated better by standard recommendation algorithms, maintaining the same popularity level of suggested movies to watch. Not much change in reranking is expected here - the recommendation list is supposedly already rather fitting.

+

Judging by the rating history, this person seems to prefer very popular mainstream movies. Clasically, such users are treated better by standard recommendation algorithms, maintaining the same popularity level of suggested movies to watch. Not much change in reranking is expected here - the recommendation list is supposedly already rather fitting.

What our mainstream user likes

@@ -81,7 +81,7 @@ const {url=settings.API_SERVER_URL} = Astro.props;
-

This person is less fond of mainstream movies and potentially would not be satisfied with a standard recommendation with higher popularity. The reranking effect is expected to be more visible in such case, lowering the popularity level of recommended movies.

+

This person is less fond of mainstream movies and potentially would not be satisfied with a standard recommendation with higher popularity. The reranking effect is expected to be more visible in such case, lowering the popularity level of recommended movies.

@@ -137,6 +137,9 @@ const {url=settings.API_SERVER_URL} = Astro.props; @@ -214,6 +217,11 @@ const {url=settings.API_SERVER_URL} = Astro.props; } + .user-text-long { + overflow: scroll; + min-height: 15rem; + max-height: 15rem; + } .expl_one_right > a > svg { scale: 1.5; @@ -473,6 +481,12 @@ const {url=settings.API_SERVER_URL} = Astro.props; #third { } + + @media only screen and (max-width: 600px) { + body { + background-color: lightblue; + } +}