-
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.
- Loading branch information
1 parent
f533403
commit dce65e4
Showing
5 changed files
with
111 additions
and
39 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
80 changes: 56 additions & 24 deletions
80
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 |
---|---|---|
@@ -1,27 +1,59 @@ | ||
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) | ||
import logging | ||
logger = logging.getLogger("root") | ||
|
||
|
||
class cover_downloader: | ||
|
||
def __init__(self): | ||
|
||
env_auth_key = os.getenv('TMBD_KEY') | ||
if not auth_key: | ||
logger("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("cover downloader: Failed to get configuration") | ||
return | ||
value = response.json() | ||
|
||
self.base_url = value["images"]["base_url"] | ||
self.poster_size = value["images"]["poster_sizes"][0] | ||
|
||
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("cover downloader: Failed to search movie") | ||
return -1 | ||
value = response.json() | ||
|
||
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("cover downloader: Failed to download cover") | ||
return -1 | ||
|
||
with open(outputfile, mode="wb") as file: | ||
file.write(response.content) | ||
|
||
return 1 | ||
else: | ||
logger("cover downloader: No properly initialized") | ||
return -1 |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
# 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 | ||
|
||
def get_cover(apps, schema_editor): | ||
|
||
if os.getenv('TMBD_KEY'): | ||
|
||
movies = Movie.objects.all() | ||
for movie in movies: | ||
movie.video_set | ||
output_file = "/usr/src/static/{}.jpeg".format(movie.title) | ||
cover_downloader.download_cover(serie.title, output_file, True) | ||
|
||
series = Series.objects.all() | ||
for serie in series: | ||
output_file = "/usr/src/static/{}.jpeg".format(serie.title) | ||
ret = cover_downloader.download_cover(serie.title, output_file, True) | ||
if ret: | ||
serie.thumbnail = output_file | ||
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