aboutsummaryrefslogtreecommitdiff
path: root/src/due/routes
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-07-24 22:27:18 -0700
committerFuwn <[email protected]>2023-07-24 22:27:18 -0700
commitee489b4b8d924ed6c5fe25ad92c43b174c8c5b6e (patch)
treeec425b61704100e1708e9637c3c967ee6b77e683 /src/due/routes
downloadold.due.moe-ee489b4b8d924ed6c5fe25ad92c43b174c8c5b6e.tar.xz
old.due.moe-ee489b4b8d924ed6c5fe25ad92c43b174c8c5b6e.zip
feat: initial build
Diffstat (limited to 'src/due/routes')
-rw-r--r--src/due/routes/__init__.py0
-rw-r--r--src/due/routes/auth.py12
-rw-r--r--src/due/routes/index.py75
-rw-r--r--src/due/routes/oauth.py29
4 files changed, 116 insertions, 0 deletions
diff --git a/src/due/routes/__init__.py b/src/due/routes/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/due/routes/__init__.py
diff --git a/src/due/routes/auth.py b/src/due/routes/auth.py
new file mode 100644
index 0000000..6cd1e1b
--- /dev/null
+++ b/src/due/routes/auth.py
@@ -0,0 +1,12 @@
+from flask import redirect, Blueprint
+
+bp = Blueprint("auth", __name__)
+
+
+def auth_logout():
+ response = redirect("/")
+
+ response.delete_cookie("anilist")
+
+ return response
diff --git a/src/due/routes/index.py b/src/due/routes/index.py
new file mode 100644
index 0000000..b4423be
--- /dev/null
+++ b/src/due/routes/index.py
@@ -0,0 +1,75 @@
+import time
+from due.html import anime_to_html, manga_to_html, page
+from due.media import create_collection
+from flask import request, Blueprint
+import json
+import os
+
+bp = Blueprint("index", __name__)
+
+
+def home():
+ if request.cookies.get("anilist"):
+ anilist = json.loads(request.cookies.get("anilist"))
+ start = time.time()
+ (current_anime, name) = create_collection(anilist, "ANIME")
+ anime_time = time.time() - start
+ start = time.time()
+ (current_manga, _) = create_collection(anilist, "MANGA")
+ manga_time = time.time() - start
+
+ releasing_anime = [
+ media for media in current_anime if media["media"]["status"] == "RELEASING"
+ ]
+ releasing_manga = [
+ media for media in current_manga if media["media"]["status"] == "RELEASING"
+ ]
+ releasing_outdated_manga = [
+ media
+ for media in releasing_manga
+ if media["media"]["type"] == "MANGA"
+ and int(media["media"]["mediaListEntry"]["progress"])
+ >= 1 # Useful when testing
+ ]
+ releasing_outdated_anime = [
+ media
+ for media in releasing_anime
+ if media["media"]["type"] == "ANIME"
+ and int(
+ (
+ {"episode": 0}
+ if media["media"]["nextAiringEpisode"] is None
+ else media["media"]["nextAiringEpisode"]
+ )["episode"]
+ )
+ - 1
+ != int(media["media"]["mediaListEntry"]["progress"])
+ ]
+ (anime_html, anime_length) = anime_to_html(releasing_outdated_anime)
+ (manga_html, manga_length) = manga_to_html(releasing_outdated_manga)
+
+ return page(
+ f"""<a href="/auth/logout">Logout from AniList ({name})</a>
+
+ <br>""",
+ f"""<details open>
+ <summary>Anime [{anime_length}] <small style="opacity: 50%">{round(anime_time, 2)}ms</small></summary>
+ {anime_html}
+ </details>
+
+ <p></p>
+
+ <details open>
+ <summary>Manga [{manga_length}] <small style="opacity: 50%">{round(manga_time, 2)}ms</small></summary>
+ {manga_html}
+ </details>
+ """,
+ )
+ else:
+ return page(
+ f"""<a href="https://anilist.co/api/v2/oauth/authorize?client_id={os.getenv('ANILIST_CLIENT_ID')}&redirect_uri={os.getenv('ANILIST_REDIRECT_URI')}&response_type=code">Login with AniList</a>
+
+ <br>""",
+ "Please log in to view due media.",
+ )
diff --git a/src/due/routes/oauth.py b/src/due/routes/oauth.py
new file mode 100644
index 0000000..290ae1d
--- /dev/null
+++ b/src/due/routes/oauth.py
@@ -0,0 +1,29 @@
+from flask import redirect, Blueprint, request
+import requests
+import json
+import os
+
+bp = Blueprint("oauth", __name__)
+
+
[email protected]("/callback")
+def oauth_callback():
+ response = redirect("/")
+
+ response.set_cookie(
+ "anilist",
+ json.dumps(
+ requests.post(
+ "https://anilist.co/api/v2/oauth/token",
+ data={
+ "grant_type": "authorization_code",
+ "client_id": os.getenv("ANILIST_CLIENT_ID"),
+ "client_secret": os.getenv("ANILIST_CLIENT_SECRET"),
+ "redirect_uri": os.getenv("ANILIST_REDIRECT_URI"),
+ "code": request.args.get("code"),
+ },
+ ).json()
+ ),
+ )
+
+ return response