-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get movies/series cover from tmdb if token is set
- Loading branch information
1 parent
0c2fad3
commit 6fcfa4a
Showing
10 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
backend/StreamServerApp/media_management/cover_downloader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import requests | ||
import os | ||
|
||
import logging | ||
logger = logging.getLogger("root") | ||
|
||
|
||
class cover_downloader: | ||
|
||
def __init__(self): | ||
self.auth_key = "" | ||
env_auth_key = os.getenv('TMBD_KEY') | ||
if not env_auth_key: | ||
logger.error("cover downloader: no auth key") | ||
return | ||
self.auth_key = env_auth_key | ||
|
||
configuration_url = 'https://api.themoviedb.org/3/configuration?&api_key={}'.format( | ||
self.auth_key ) | ||
|
||
response = requests.get(configuration_url) | ||
if response.status_code != 200: | ||
logger.error("cover downloader: Failed to get configuration") | ||
return | ||
value = response.json() | ||
|
||
self.base_url = value["images"]["base_url"] | ||
self.poster_size = value["images"]["poster_sizes"][3] | ||
|
||
def download_cover(self, name, outputfile, is_tv_show=False): | ||
|
||
if self.auth_key: | ||
|
||
if is_tv_show: | ||
api_url = 'https://api.themoviedb.org/3/search/tv?query={}&api_key={}'.format( | ||
name.replace(" ", "+"), self.auth_key) | ||
else: | ||
api_url = 'https://api.themoviedb.org/3/search/movie?query={}&api_key={}'.format( | ||
name.replace(" ", "+"), self.auth_key) | ||
response = requests.get(api_url) | ||
if response.status_code != 200: | ||
logger.error("cover downloader: Failed to search movie") | ||
return -1 | ||
value = response.json() | ||
|
||
if len(value["results"]) > 0: | ||
|
||
poster_url = "{}/{}{}".format(self.base_url, self.poster_size, | ||
value["results"][0]["poster_path"]) | ||
response = requests.get(poster_url) | ||
if response.status_code != 200: | ||
logger.error("cover downloader: Failed to download cover") | ||
return -1 | ||
|
||
with open(outputfile, mode="wb") as file: | ||
file.write(response.content) | ||
|
||
return 1 | ||
else: | ||
return -1 | ||
else: | ||
logger.error("cover downloader: No properly initialized") | ||
return -1 |
46 changes: 46 additions & 0 deletions
46
backend/StreamServerApp/migrations/0026_auto_20240618_0917.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by Django 4.2 on 2024-06-18 09:17 | ||
|
||
from django.db import migrations | ||
from StreamServerApp.models import Video, Series, Movie, Subtitle | ||
from StreamServerApp.media_management.cover_downloader import cover_downloader | ||
import os | ||
|
||
cvdwnld = cover_downloader() | ||
|
||
def get_cover(apps, schema_editor): | ||
|
||
if os.getenv('TMBD_KEY'): | ||
|
||
movies = Movie.objects.all() | ||
for movie in movies: | ||
output_file = "/usr/static/{}.jpeg".format(movie.title) | ||
ret = cvdwnld.download_cover(movie.title, output_file, True) | ||
if ret > 0: | ||
videos = movie.video_set.all() | ||
for video in videos: | ||
video.thumbnail = "/static/{}.jpeg".format(movie.title) | ||
video.save() | ||
|
||
|
||
series = Series.objects.all() | ||
for serie in series: | ||
output_file = "/usr/static/{}.jpeg".format(serie.title) | ||
ret = cvdwnld.download_cover(serie.title, output_file, True) | ||
if ret > 0: | ||
serie.thumbnail = "/static/{}.jpeg".format(serie.title) | ||
serie.save() | ||
|
||
|
||
|
||
|
||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('StreamServerApp', '0025_video_audio_path'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(code=get_cover, reverse_code=migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.test import TestCase | ||
import os | ||
from unittest import TestCase, mock | ||
from StreamServerApp.media_management.cover_downloader import cover_downloader | ||
import optparse | ||
|
||
class CoverDownloaderTest(TestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
@mock.patch.dict(os.environ, {"TMBD_KEY": ""}) | ||
def test_ensureErrorWithInvalidToken(self): | ||
|
||
cvdwnld = cover_downloader() | ||
self.assertEqual(cvdwnld.download_cover("test", "/test/test.jpeg", True), -1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
service transmission-daemon start | ||
python3 /usr/src/app/manage.py collectstatic --no-input | ||
python3 /usr/src/app/manage.py clearcache | ||
gunicorn --workers=${NUM_GUNICORN_WORKER} StreamingServer.wsgi:application --bind 0.0.0.0:8000 | ||
if [[ "$DEPLOY_ENV" == "development" ]] | ||
then | ||
./wait-for-it.sh db:5432 -- python3 /usr/src/app/manage.py runserver 0.0.0.0:8000 | ||
else | ||
gunicorn --workers=${NUM_GUNICORN_WORKER} StreamingServer.wsgi:application --bind 0.0.0.0:8000 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters