aboutsummaryrefslogtreecommitdiff
path: root/src/due/routes/index.py
blob: df1b6da8ee2bba274a27866c66ebbec9025bf889 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time
from due.html import anime_to_html, manga_to_html, page
from due.media import create_collection, last_activity, user_id, user_name_to_id
from flask import make_response, redirect, request, Blueprint
import json
import os
import datetime

bp = Blueprint("index", __name__)


@bp.route("/")
def home():
    response = make_response("")
    disable_manga = True

    if request.args.get("show_manga") is not None:
        disable_manga = False

    if request.args.get("hide_message") is not None:
        if request.cookies.get("hide_message") is None:
            response = redirect("/")

            response.set_cookie("hide_message", "1")

    if request.args.get("toggle_missing") is not None:
        response = redirect(
            f"/{'?show_manga' if request.args.get('show_manga') is not None else ''}"
        )

        if request.cookies.get("show_missing") is None:
            response.set_cookie("show_missing", "1")
        else:
            response.delete_cookie("show_missing")

    # print(
    #     requests.post(
    #         "https://anilist.co/api/v2/oauth/token",
    #         data={
    #             "grant_type": "refresh_token",
    #             "client_id": os.getenv("ANILIST_CLIENT_ID"),
    #             "client_secret": os.getenv("ANILIST_CLIENT_SECRET"),
    #             "refresh_token": json.loads(request.cookies.get("anilist"))[
    #                 "refresh_token"
    #             ],
    #         },
    #     ).json()
    # )

    if request.cookies.get("anilist"):
        anilist = json.loads(request.cookies.get("anilist"))

        if user_id(anilist) == -1:
            response = redirect("/")

            response.delete_cookie("anilist")

            return response

        start = time.time()
        (current_anime, name) = create_collection(
            anilist, "ANIME", request.args.get("username")
        )

        print(name)

        releasing_anime = [
            media for media in current_anime if media["media"]["status"] == "RELEASING"
        ]
        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"] or {"progress": 0})["progress"])
        ]
        (anime_html, anime_length) = anime_to_html(releasing_outdated_anime)
        anime_time = time.time() - start
        start = time.time()
        manga_body = '<a href="/?show_manga">Show manga</a>'

        if not disable_manga:
            (current_manga, _) = create_collection(
                anilist, "MANGA", request.args.get("username")
            )
            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"] or {"progress": 0})["progress"]
                )
                >= 1  # Useful when testing
            ]
            (manga_html, manga_length) = manga_to_html(
                releasing_outdated_manga, request.cookies.get("show_missing")
            )
            manga_time = time.time() - start
            manga_body = f"""
            <p></p>

            <details closed>
            <summary>Manga [{manga_length}] <small style="opacity: 50%">{round(manga_time, 2)}s</small></summary>
                {manga_html}
            </details>
            """

        response.set_data(
            page(
                f"""<a href="/auth/logout">Logout from AniList ({name})</a>
                {"<p></p><p>You don't have any new activity statuses from the past day! Create one to keep your streak!</p>" if datetime.datetime.fromtimestamp(last_activity(user_name_to_id(name))).date()
            != datetime.date.today() else "<p></p>"}""",
                f"""<a href=\"/?toggle_missing{'&show_manga' if request.args.get('show_manga') is not None else ''}\">{'Show' if request.cookies.get('show_missing') else 'Hide'} unresolved</a><p></p><details open>
                <summary>Anime [{anime_length}] <small style="opacity: 50%">{round(anime_time, 2)}s</small></summary>
                {anime_html}
            </details>

            {manga_body}""",
            )
        )
    else:
        response.set_data(
            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.",
            )
        )

    return response