diff options
| author | Bobby <[email protected]> | 2024-10-04 17:19:54 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-10-04 17:19:54 -0400 |
| commit | 17149c90290d80fc9b0b1b74a2777369e2da7c18 (patch) | |
| tree | 6b2cdf543f96500d076a6d5de46b2d4bcbd04263 | |
| parent | 4e801314b76ebc1556ab37fa8d7f0897c6e238ed (diff) | |
| download | yugen-17149c90290d80fc9b0b1b74a2777369e2da7c18.tar.xz yugen-17149c90290d80fc9b0b1b74a2777369e2da7c18.zip | |
Added global meta middleware
| -rw-r--r-- | authentication/utils.py | 10 | ||||
| -rw-r--r-- | detail/views.py | 44 | ||||
| -rw-r--r-- | middleware/authentication.py | 11 | ||||
| -rw-r--r-- | middleware/globalmetamiddleware.py | 90 | ||||
| -rw-r--r-- | templates/partials/base.html | 8 | ||||
| -rw-r--r-- | yugen/settings.py | 1 |
6 files changed, 113 insertions, 51 deletions
diff --git a/authentication/utils.py b/authentication/utils.py index b106c80..8a8beba 100644 --- a/authentication/utils.py +++ b/authentication/utils.py @@ -165,8 +165,18 @@ def get_discord_user(access_token, token_type): user["is_authorized"] = False user["rate_limited"] = False + user["banner"] = user["banner"] if "banner" in user else "" return user +def update_user_discord_info(user_data): + user = User.objects.get(discord_id=user_data["id"]) + print("Updating User Info for", user.username) + user.discord_username = user_data["username"] + user.discord_avatar = user_data["avatar"] + user.discord_banner = user_data["banner"] + user.discord_global_name = user_data["global_name"] + user.discord_guild_name = user_data["guild_name"] + user.save() def authenticate_user(exchange_response): access_token = exchange_response.get("access_token") diff --git a/detail/views.py b/detail/views.py index d25c3bf..5e89355 100644 --- a/detail/views.py +++ b/detail/views.py @@ -36,47 +36,3 @@ def detail(request, anime_id): context["nextAiringEpisode"] = anime_data["nextAiringEpisode"] return render(request, "detail/detail.html", context) - - - # zid support is dropped - - - - # anime_data, provider, gd = get_anime_data(anime_id) - # if not anime_data: - # return render(request, "detail/detail.html", {"error": "Anime not found"}, status=404) - - # anime_episodes = get_anime_episodes(anime_id) - # if "message" in anime_episodes: - # anime_data, provider, gd = get_anime_data(anime_id, provider="gogo") - # anime_episodes, _ = get_anime_episodes_gogo(anime_id) - - # if anime_episodes: - # attach_episode_metadata(anime_data, anime_episodes) - - # if request.user.mal_access_token and anime_data.get("malId"): - # mal_data = get_single_anime_mal(request.user.mal_access_token, anime_data["malId"]) - # else: - # mal_data = None - - # context = { - # "anime": anime_data, - # "episodes": anime_episodes, - # "related": anime_data.get("relations", []), - # "recommendations": anime_data.get("recommendations", []), - # } - - # zid = anime_data["episodes"][0]["id"].split("$")[0] if len(anime_data["episodes"]) > 0 else None - # if zid and provider == "zoro": - # seasons = get_seasons_by_zid(zid) - # if seasons: - # context["seasons"] = seasons - - # if "nextAiringEpisode" in anime_data: - # context["nextAiringEpisode"] = anime_data["nextAiringEpisode"] - - # if mal_data: - # context["mal_data"] = mal_data - # context["mal_episode_range"] = range(1, mal_data["num_episodes"] + 1) - - # return render(request, "detail/detail.html", context) diff --git a/middleware/authentication.py b/middleware/authentication.py index 8ce52b1..ba22b54 100644 --- a/middleware/authentication.py +++ b/middleware/authentication.py @@ -8,6 +8,7 @@ from django.http import HttpResponse from authentication.utils import ( get_redirect_uri, get_discord_user, + update_user_discord_info, ) class AuthMiddleware: @@ -44,13 +45,11 @@ class AuthMiddleware: cookie_data["verified_at"] ) if timezone.now() > verified_at + timedelta(hours=24): - # Verification expired, need to re-check - pass + verification_cookie = None except (json.JSONDecodeError, ValueError): - # Cookie is invalid or expired, need to re-check - pass + verification_cookie = None else: - # No verification cookie, need to check guild membership + verification_cookie = None pass if not verification_cookie or not self._is_authorized(request): @@ -65,7 +64,7 @@ class AuthMiddleware: response = render(request, "messages/unauthorized.html", {"redirect_uri": get_redirect_uri()}) response.delete_cookie("guild_verified") # Ensure cookie is removed return response - + update_user_discord_info(user) # Set the verification cookie response = self.get_response(request) response.set_cookie( diff --git a/middleware/globalmetamiddleware.py b/middleware/globalmetamiddleware.py new file mode 100644 index 0000000..d0734fb --- /dev/null +++ b/middleware/globalmetamiddleware.py @@ -0,0 +1,90 @@ +import re +from watch.utils import get_anime_data + +class GlobalMetaMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def get_anime_title_description(self, anime_info, title_lang="english"): + if "description" in anime_info: + description = anime_info["description"] + description = re.sub('<[^<]+?>', '', description) + else: + description = "Welcome to Yugen! Stream anime and read manga. Get the latest anime schedule and browse and add anime to your watchlist." + + if title_lang == "english" and "english" in anime_info["title"]: + title = anime_info["title"]["english"] + elif title_lang == "romaji" and "romaji" in anime_info["title"]: + title = anime_info["title"]["romaji"] + else: + title = anime_info["title"]["native"] + + return title, description + + def __call__(self, request): + request.meta = { + "title": "Yugen — Stream Anime | Read Manga | Anime Schedule | Anime List", + "description": "Welcome to Yugen! Stream anime and read manga. Get the latest anime schedule and browse and add anime to your watchlist.", + "image": "https://anime.rize.moe/static/icons/yugen.png", + "url": "{}://{}{}".format(request.scheme, request.get_host(), request.path), + "robots": "index, follow", + } + + full_path = request.get_full_path() + if '/search' in full_path: + request.meta["title"] = "Search Anime | Yugen" + request.meta["description"] = "Search for your favorite anime on Yugen. Stream and watch the latest anime episodes. Read manga and explore the latest anime schedule." + + if '/search?sort=%5B%22TRENDING_DESC%22%5D' in full_path or '/search?sort=[%22TRENDING_DESC%22]' in full_path: + request.meta["title"] = "Trending Anime | Yugen" + request.meta["description"] = "Discover the most popular anime on Yugen. Stream and watch the latest anime episodes. Read manga and explore the latest anime schedule." + + if '/schedule' in full_path: + request.meta["title"] = "Anime Schedule | Yugen" + request.meta["description"] = "Explore the latest anime schedule on Yugen. Stream and watch the latest anime episodes. Read manga and discover the most popular anime." + + if '/watchlist' in full_path: + request.meta["title"] = "Watchlist | Yugen" + request.meta["description"] = "Explore your watchlist on Yugen. Stream and watch the latest anime episodes. Read manga and discover the most popular anime." + + if '/profile' in full_path: + request.meta["title"] = "Profile | Yugen" + request.meta["description"] = "Explore your profile on Yugen. Stream and watch the latest anime episodes. Read manga and discover the most popular anime." + + if '/detail/' in full_path: + requested_id = request.path.split("/")[2] + anime_info = get_anime_data(requested_id) + + if request.user.is_authenticated: + title, description = self.get_anime_title_description(anime_info, request.user.preferences.title_language) + else: + title, description = self.get_anime_title_description(anime_info) + + request.meta["title"] = f"{title} | Yugen" + request.meta["description"] = description + request.meta["image"] = anime_info["image"] + + if '/watch/' in full_path: + requested_id = request.path.split("/")[2] + episode = request.path.split("/")[3] if len(request.path.split("/")) > 3 else 1 + episode = int(episode) + anime_info = get_anime_data(requested_id) + + if len(anime_info["episodes"] or []) < episode: + episode = len(anime_info["episodes"]) + + if episode <= 0: + episode = 1 + + if request.user.is_authenticated: + title, description = self.get_anime_title_description(anime_info, request.user.preferences.title_language) + else: + title, description = self.get_anime_title_description(anime_info) + + request.meta["title"] = f"Watch {title} Episode {episode} | Yugen" + request.meta["description"] = description + request.meta["image"] = anime_info["image"] + + response = self.get_response(request) + + return response
\ No newline at end of file diff --git a/templates/partials/base.html b/templates/partials/base.html index de6d8a1..3698547 100644 --- a/templates/partials/base.html +++ b/templates/partials/base.html @@ -5,13 +5,19 @@ <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> + <meta name="description" content="{{ request.meta.description }}" /> + <meta name="robots" content="{{ request.meta.robots }}" /> + <meta property="og:title" content="{{ request.meta.title }}" /> + <meta property="og:description" content="{{ request.meta.description }}" /> + <meta property="og:image" content="{{ request.meta.image }}" /> + <meta property="og:url" content="{{ request.meta.url }}" /> <link rel="icon" href="{% static 'icons/favicon.ico' %}" /> <link rel="preconnect" href="https://fonts.googleapis.com" /> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link href="https://fonts.googleapis.com/css2?family=Karla:ital,wght@0,200..800;1,200..800&display=swap" rel="stylesheet"/> <link rel="stylesheet" href="{% static 'css/main.css' %}" /> {% block css %} {% endblock %} - <title>Yugen</title> + <title>{{ request.meta.title }}</title> </head> <body class="bg-black text-white"> {% include "partials/navbar.html" %} diff --git a/yugen/settings.py b/yugen/settings.py index 1ed5025..5371efb 100644 --- a/yugen/settings.py +++ b/yugen/settings.py @@ -69,6 +69,7 @@ MIDDLEWARE = [ "django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.gzip.GZipMiddleware", "middleware.remove_slash.RemoveSlashMiddleware", + "middleware.globalmetamiddleware.GlobalMetaMiddleware", "middleware.authentication.AuthMiddleware", "middleware.preferences.PreferencesMiddleware", "middleware.mal_token_refresh.MALTokenRefreshMiddleware", |
