-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
226 lines (178 loc) · 7.93 KB
/
app.py
File metadata and controls
226 lines (178 loc) · 7.93 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from flask import Flask, render_template, request, jsonify
import requests
from static.scripts.season_new_releases import get_seasonal_anime
from static.scripts.search_by_name import anime_name_search
from static.scripts.search_by_genre import get_genre_list, fetch_anime_for_genre
from static.scripts.search_by_studio import get_studio_id, search_anime_by_studio, search
from static.scripts.top_anime import fetch_top_anime
from static.scripts.upcoming_anime import fetch_upcoming_anime
from static.scripts.manga_explorer import fetch_top_manga, GENRE_MAP
# Initialize Flask application
app = Flask(__name__)
# Base URL for Jikan API (Not used directly but kept for reference)
JIKAN_API_URL = "https://api.jikan.moe/v4"
# Home Page Route
@app.route('/')
@app.route('/home')
def home():
"""
Renders the homepage (index.html).
"""
"""
Renders the homepage (index.html).
"""
return render_template('index.html')
# Route for New Anime Releases based on Season and Year
@app.route('/season_new_releases', methods=['GET']) # Define the route for the '/season_new_releases' webpage with a GET request
def season_new_releases():
# Retrieve the year and season parameter from the request URL.
input_year = request.args.get('year', type=int) # set user's year input
input_season = request.args.get('season', type=str) # set user's season input
page = request.args.get('page', default=1, type=int) # ser user's page input so we can get next 25 anime lists
# Grabing the animation list from the season_new_anime_releases.py
anime_data, input_year, season, current_page = get_seasonal_anime(input_year, input_season, page)
_, max_year, _, _ = get_seasonal_anime() # Extracts the default year
# Render the 'season_new_releases.html' template with data given from season_new_anime_releases.py
return render_template(
'season_new_releases.html', # render it to HTML template
anime_list=anime_data, # List of anime releases to display in the template
year=input_year, # Processed year to be displayed
season=season, # Season such as Spring, Summer, Fall, Winter to be displayed
page=current_page, # set the page to see next 25 anime list
max_year=max_year
)
# Search by Genre Route
@app.route('/search_by_genre')
def search_by_genre():
"""
Handles anime search by genre with pagination.
- If no genre is selected, the page loads with a message prompting the user to select one.
- If a genre is selected, fetch anime from the Jikan API.
- If an invalid genre is entered, display an error message.
"""
# Get genre ID and page number from request arguments
genre_id = request.args.get("genre_id")
page = request.args.get("page", default=1, type=int)
# Allow page to load normally without a selected genre
if not genre_id:
return render_template("search_by_genre.html",
genres=get_genre_list(),
selected_genre_id=None,
selected_genre_name=None,
anime_list=None,
current_page=page,
has_next_page=False,
error_message="Please select a genre.")
try:
# Validate genre_id to ensure it's a number
if not genre_id.isdigit():
raise ValueError("Invalid genre ID. Please select a valid genre.")
# Convert genre_id to an integer
genre_id = int(genre_id)
# Initialize error_message before calling fetch_anime_for_genre
error_message = None
# Fetch anime data for the selected genre and page
anime_list, has_next, selected_genre_name, error_message = fetch_anime_for_genre(genre_id, page)
# Render the results on the page
return render_template("search_by_genre.html",
genres=get_genre_list(),
selected_genre_id=genre_id,
selected_genre_name=selected_genre_name,
anime_list=anime_list,
current_page=page,
has_next_page=has_next,
error_message=error_message)
except ValueError as e:
print(f"ValueError: {e}")
return render_template("404.html", error_message=str(e)), 404
except Exception as e:
print(f"Unexpected Error: {e}")
return render_template("404.html", error_message="An unexpected error occurred. Please try again later."), 500
# Search by Name Route
@app.route('/search_by_name', methods=['GET'])
def search_by_name():
"""
Handles anime search by name using the Jikan API.
- If no name is entered, it prompts the user to enter one.
- If a name is entered, it fetches results from the API.
"""
anime_name = request.args.get("name", "").strip()
# If no name is entered, show an empty search page
if not anime_name:
return render_template("search_by_name.html", anime_list=None, error_message="Enter an anime name to search.")
# Fetch anime details
result = anime_name_search(anime_name)
# Handle API errors
if "error" in result:
return render_template("search_by_name.html", anime_list=None, error_message=result["error"])
return render_template("search_by_name.html", anime_list=result["anime_list"], error_message=None)
# Route for Search by Studio Page
@app.route('/search_by_studio')
def search_by_studio():
return render_template('search_by_studio.html') # Return of the search_by_studio.html
# Route for handling search requests
@app.route('/search', methods=['GET'])
def search_route():
studio_name = request.args.get('studio') # Get the studio name from the request parameters
results = search(studio_name) # Perform the search using the studio name
return jsonify(results) # Return the search results as JSON
# Route for Top Anime Rankings
@app.route('/top_anime')
def top_anime():
top_anime_list = fetch_top_anime()[:10] # Limits the list to top 10 anime
return render_template('top_anime.html', anime=top_anime_list)
# Route for Upcoming Releases
@app.route('/upcoming_search')
def upcoming_search():
page = request.args.get('page', default=1, type=int)
anime_list, has_next = fetch_upcoming_anime(page=page)
return render_template("upcoming_search.html",
anime_list=anime_list,
current_page=page,
has_next=has_next)
# Route for Manga Explorer
@app.route("/manga_explorer")
def manga_explorer():
genre = request.args.get("genre")
manga_type = request.args.get("type")
search = request.args.get("search")
page = request.args.get("page", 1, type=int)
# Clean up string "None" to actual NoneType
if genre == "None":
genre = None
if manga_type == "None":
manga_type = None
if search == "None":
search = None
mangas, has_next, error = fetch_top_manga(
genre=genre,
search=search,
manga_type=manga_type,
page=page
)
title = f"Search Results for: {search}" if search else f"Top {genre} Manga" if genre else "Top Manga"
return render_template(
"manga_explorer.html",
mangas=mangas,
error=error,
page_title=title,
genre=genre,
type=manga_type,
search=search,
page=page,
has_next=has_next,
GENRE_MAP=GENRE_MAP
)
# Custom 404 Error Page
@app.errorhandler(404)
def page_not_found(e):
"""
Handles 404 errors by rendering a custom error page.
"""
"""
Handles 404 errors by rendering a custom error page.
"""
return render_template('404.html'), 404
# Run Flask Application
if __name__ == '__main__':
app.run(debug=True)