Skip to content

Commit

Permalink
handle async cover download
Browse files Browse the repository at this point in the history
  • Loading branch information
DerouineauNicolas committed Jun 17, 2024
1 parent 9df078d commit 283b912
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/StreamServerApp/database_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from StreamServerApp.media_management.fileinfo import createfileinfo, readfileinfo
from StreamServerApp.media_processing import prepare_video, get_video_type_and_info
from StreamServerApp.tasks import get_subtitles_async
from StreamServerApp.tasks import get_subtitles_async, download_cover_async
from StreamingServer import settings

import logging
Expand Down Expand Up @@ -222,6 +222,7 @@ def add_one_video_to_database(full_path,
return_value = 1

v.save()
download_cover_async.delay(v.id, video_type_and_info['title'], True if video_type_and_info['type'] == 'Series' else False)
for ov_subtitle_path in video_infos["ov_subtitles"]:
ov_sub = Subtitle()
webvtt_subtitles_relative_path = os.path.relpath(
Expand Down
27 changes: 27 additions & 0 deletions backend/StreamServerApp/media_management/cover_downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
import os

auth_key = os.getenv('TMBD_KEY')
if not auth_key:
print("no auth key")

configuration_url = 'https://api.themoviedb.org/3/configuration?&api_key={}'.format(auth_key)
response = requests.get(configuration_url)
value = response.json()

base_url = value["images"]["base_url"]
poster_size = value["images"]["poster_sizes"][0]

def download_cover(name, outputfile, is_tv_show=False):
if is_tv_show:
api_url = 'https://api.themoviedb.org/3/search/tv?query={}&api_key={}'.format(name, auth_key)
else:
api_url = 'https://api.themoviedb.org/3/search/movie?query={}&api_key={}'.format(name, auth_key)
response = requests.get(api_url)
value = response.json()

poster_url = "{}/{}{}".format(base_url, poster_size, value["results"][0]["poster_path"])
response = requests.get(poster_url)

with open(outputfile, mode="wb") as file:
file.write(response.content)
19 changes: 17 additions & 2 deletions backend/StreamServerApp/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from celery import shared_task
from StreamServerApp.models import Video, Series, Movie, Subtitle
import subprocess
import os
from django.conf import settings
from media_management.cover_downloader import download_cover

import logging

Expand All @@ -24,3 +23,19 @@ def get_subtitles_async(video_id, video_path, remote_url):
except Exception as e:
logger.exception(e)
return 0


@shared_task
def download_cover_async(id, name, is_tv_show=False):
output_file = "/usr/src/static/{}.jpeg".format(name)
download_cover(name, is_tv_show)
video = Video.objects.get(id=id)
if is_tv_show:
serie = Series.objects.get(id=video.series_id)
serie.thumbnail = output_file
serie.save()
else:
video.thumbnail = output_file
video.save()

return 0

0 comments on commit 283b912

Please sign in to comment.