From f7e6aa4c45b0c8a6c49732e21171b0d3b6c36329 Mon Sep 17 00:00:00 2001 From: perro Date: Thu, 6 Apr 2023 11:48:12 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20#11=20-=20Habilitaci=C3=B3n=20de=20subt?= =?UTF-8?q?=C3=ADtulos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/main/models.py | 58 +++++++++++++++++++++++++++++++++ source/main/static/js/player.js | 3 +- source/main/views.py | 5 ++- source/static/js/player.js | 3 +- source/templates/info-body.html | 3 ++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/source/main/models.py b/source/main/models.py index 9ce8971..231063f 100644 --- a/source/main/models.py +++ b/source/main/models.py @@ -1,3 +1,4 @@ +import requests import random import time import re @@ -264,6 +265,63 @@ class MovieQuerySet(models.QuerySet): else: return f"{num}{sym}" + def get_sub_lang(self, key): + """ + Obtiene los lenguajes de los subtítulos en diversas nomenclaturas. + + La llave está en ISO-639-2 o ISO-639-3. + Cfr. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes + https://en.wikipedia.org/wiki/ISO_639-2 + OJO: hay más variantes de Tarahumara y de Mixteco + """ + langs = { + "spa": {"iso639_1": "es", "label": "Español"}, + "eng": {"iso639_1": "en", "label": "Inglés"}, + "fra": {"iso639_1": "fr", "label": "Francés"}, + "por": {"iso639_1": "pt", "label": "Portugués"}, + "rum": {"iso639_1": "ro", "label": "Rumano"}, + "cat": {"iso639_1": "ca", "label": "Catalán"}, + "glg": {"iso639_1": "gl", "label": "Gallego"}, + "grn": {"iso639_1": "gn", "label": "Guaraní"}, + "gug": {"iso639_1": "gn", "label": "Guaraní"}, + "nah": {"iso639_1": "!!", "label": "Náhuatl"}, + "myn": {"iso639_1": "!!", "label": "Maya"}, + "zap": {"iso639_1": "!!", "label": "Zapoteco"}, + "oto": {"iso639_1": "!!", "label": "Otomí"}, + "tar": {"iso639_1": "!!", "label": "Tarahumara"}, + "mix": {"iso639_1": "!!", "label": "Mixteco"}, + "apa": {"iso639_1": "!!", "label": "Apache"}, + "rus": {"iso639_1": "ru", "label": "Ruso"}, + "deu": {"iso639_1": "de", "label": "Alemán"}, + "ger": {"iso639_1": "de", "label": "Alemán"}, + "chi": {"iso639_1": "zh", "label": "Chino"}, + "zho": {"iso639_1": "zh", "label": "Chino"}, + } + if key in langs: + lang = langs[key] + lang["suffix"] = key + return lang + return {"suffix": key, "iso639_1": "!!", "label": key} + + def get_subs(self, id): + """ + Obtiene subtítulos de película por id. + + TODO: en la restructuración de la DB no debería hacer requests. + """ + file = self._fix_path_movie(Movie.objects.get(pk=id).file_name) + file = Path(re.sub(r"^https?://", "", file)) + page = requests.get(f"https://{file.parent}").text + soup = BeautifulSoup(page, 'html.parser') + subs = [] + for node in soup.find_all("a"): + if node.get("href").endswith("vtt"): + url = node.get("href") + lang = self.get_sub_lang(Path(url).suffixes[0][1:]) + lang["path"] = "https://" + str(file.parent / url) + subs.append(lang) + return subs + def get_wiki(self, movie, again=True): """ Obtiene artículo de Wikipedia. diff --git a/source/main/static/js/player.js b/source/main/static/js/player.js index 55dcc5a..fa50127 100644 --- a/source/main/static/js/player.js +++ b/source/main/static/js/player.js @@ -51,6 +51,7 @@ function remove_notice () { const player = new Plyr('#player', { keyboard: { focused: true, global: true }, controls: ['play-large', 'play', 'progress', 'current-time', 'mute', - 'volume', 'pip', 'airplay', 'download', 'fullscreen'], + 'volume', 'pip', 'airplay', 'captions', 'settings', 'download', + 'fullscreen'], listeners: { seek: add_notice() }, }); diff --git a/source/main/views.py b/source/main/views.py index a407e55..1bd26d2 100644 --- a/source/main/views.py +++ b/source/main/views.py @@ -37,7 +37,10 @@ def bugs(request): def movie(request, id): - context = {"movie": Movie.objects.get_movie_by_id(id)} + context = { + "movie": Movie.objects.get_movie_by_id(id), + "subs": Movie.objects.get_subs(id), + } return render(request, "movie.html", context) diff --git a/source/static/js/player.js b/source/static/js/player.js index 55dcc5a..fa50127 100644 --- a/source/static/js/player.js +++ b/source/static/js/player.js @@ -51,6 +51,7 @@ function remove_notice () { const player = new Plyr('#player', { keyboard: { focused: true, global: true }, controls: ['play-large', 'play', 'progress', 'current-time', 'mute', - 'volume', 'pip', 'airplay', 'download', 'fullscreen'], + 'volume', 'pip', 'airplay', 'captions', 'settings', 'download', + 'fullscreen'], listeners: { seek: add_notice() }, }); diff --git a/source/templates/info-body.html b/source/templates/info-body.html index 155647e..6735525 100644 --- a/source/templates/info-body.html +++ b/source/templates/info-body.html @@ -64,6 +64,9 @@ {% if user.is_authenticated %} {% load static %}