-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
147 lines (117 loc) · 4.41 KB
/
app.py
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
from flask import Flask, render_template, request, redirect, url_for, make_response, send_from_directory
import requests
app = Flask(__name__)
def format_anime_name(name):
if ' ' in name:
name = ' '.join(name.split())
elif name.endswith(" "):
name = name.rstrip()
name = name.lstrip()
return name.lower().replace(' ', '-')
@app.route('/')
def index():
episode = int(request.args.get('episode', '1'))
episode_type = request.args.get('type', 'sub')
embed_url = f"https://2anime.xyz/embed/one-piece{'-dub' if episode_type == 'dub' else ''}-episode-{episode}"
response = make_response(render_template('index.html', embed_url=embed_url, episode=episode, episode_start=(
episode - 1) // 100 * 100 + 1, episode_type=episode_type))
response.set_cookie('last_episode', str(episode),
max_age=30*24*60*60) # Save for 30 days
return response
@app.route('/search')
def search():
query = request.args.get('query', '')
if query:
formatted_name = format_anime_name(query)
return redirect(url_for('details', name=formatted_name))
return redirect(url_for('index'))
import requests
def fetch_anime_details_from_anilist(name):
# Define the GraphQL query
query = '''
query ($search: String) {
Media(search: $search, type: ANIME) {
id
title {
romaji
english
native
}
description
episodes
startDate {
year
month
day
}
status
averageScore
coverImage {
large
}
}
}
'''
# Define the variables for the query
variables = {
'search': name
}
# Define the endpoint and send the request
url = 'https://graphql.anilist.co'
response = requests.post(url, json={'query': query, 'variables': variables})
if response.status_code != 200:
return None
data = response.json()
# Extract the data
media = data.get('data', {}).get('Media', {})
if media:
return {
'title_romaji': media.get('title', {}).get('romaji', 'No title available.'),
'title_english': media.get('title', {}).get('english', 'No title available.'),
'title_native': media.get('title', {}).get('native', 'No title available.'),
'description': media.get('description', 'No description available.'),
'episodes': media.get('episodes', 'Not specified.'),
'start_date': f"{media.get('startDate', {}).get('year', 'Unknown Year')}-{media.get('startDate', {}).get('month', '00')}-{media.get('startDate', {}).get('day', '00')}",
'status': media.get('status', 'No status available.'),
'average_score': media.get('averageScore', 'No score available.'),
'cover_image': media.get('coverImage', {}).get('large', 'No cover image available.')
}
else:
return None
@app.route('/details/<name>')
def details(name):
anime_details = fetch_anime_details_from_anilist(name)
if anime_details:
return render_template('details.html', anime=anime_details)
else:
return "Anime not found."
@app.route('/anime/<name>')
def anime(name):
episode = int(request.args.get(
'episode', request.cookies.get(f'{name}_last_episode', '1')))
episode_type = request.args.get('type', 'sub')
episode_start = (episode - 1) // 100 * 100 + 1
embed_url = f"https://2anime.xyz/embed/{name}{'-dub' if episode_type == 'dub' else ''}-episode-{episode}"
response = make_response(render_template('anime.html', embed_url=embed_url, anime_name=name, episode=episode, episode_start=episode_start, episode_type=episode_type))
response.set_cookie(f'{name}_last_episode', str(episode), max_age=30*24*60*60) # Save for 30 days
return response
@app.route('/favicon.ico')
def favicon():
return send_from_directory(app.static_folder, 'favicon.ico')
@app.route('/contact-us')
def contact_us():
return render_template('contact.html')
@app.route('/version')
def version():
return render_template('version.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/privacy-policy')
def privacy_policy():
return render_template('privacy_policy.html')
@app.route('/terms-of-service')
def terms_of_service():
return render_template('terms_of_service.html')
if __name__ == '__main__':
app.run()