-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
executable file
·135 lines (109 loc) · 3.72 KB
/
web.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
#!/usr/bin/python3
from flask import Flask, request, render_template, abort, redirect, jsonify
from database import Database, Anime, Episode
import requests
import logging
from to_dict import anime_to_dict, ep_to_dict
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)
app.config["database"] = "instance/database.db"
database = Database(app.config.get("database"))
@app.route("/")
def index():
return render_template("anime.html")
@app.route("/get_anime_list")
def get_anime_list():
anime = database.anime_all() # List of all anime
anime = anime_to_dict(anime)
return jsonify(anime)
# get specific anime with its episode list
@app.route("/get_anime_ep/<int:ID>")
def get_anime_ep(ID):
anime = database.get_anime(ID)
if anime:
ep_list = database.anime_ep_list(ID)
ep_list = ep_to_dict(ep_list)
return jsonify({"episodes": ep_list, "index": anime.index})
return abort(404)
# update/set index of anime
@app.route("/set_index/<int:ID>/<int:index>")
def set_anime_index(ID, index):
anime = database.get_anime(ID)
result = False
if anime:
ep_num = database.episodes_count(ID)
# Number of episodes should be greater than index being set because index starts with 0
if ep_num >= 0 and ep_num > index:
anime.index = index
database.update_anime(anime)
result = True
return jsonify({"result": result})
@app.route("/anime_data/<int:ID>")
def anime_data(ID):
anime = database.get_anime(ID)
if not anime:
return abort(404)
count = database.episodes_count(ID)
return jsonify({"title": anime.title, "url": anime.anime_url, "count": count})
# save new anime, or update list of episode if anime already exist
@app.route("/save_anime", methods=["POST"])
def save_anime():
anime_dict = request.get_json()
anime_id = anime_dict.get("anime_id")
anime_url = anime_dict.get("anime_url")
if not anime_id:
title = anime_dict.get("title").replace("-", " ")
anime = Anime((None, title, anime_url, None, 0))
anime = database.insert_anime(anime)
else:
anime = database.get_anime(anime_id)
if not anime:
return jsonify({"result": "No Anime Found to update"})
anime.anime_url = anime_url
ep_list = anime_dict.get("ep_list")
for i in ep_list:
ep_title = i[0]
ep_url = i[1]
episode = Episode((None, anime.id, ep_title, ep_url))
database.insert_episode(episode)
database.commit()
return jsonify({"result": True})
# Delete anime with its episodes
@app.route("/del_anime/<int:ID>")
def del_anime(ID):
anime = database.get_anime(ID)
result = False
if anime:
database.delete_anime(ID)
result = ID
return jsonify({"result": result})
# Update title of anime
@app.route("/edit_title/<int:ID>", methods=["POST"])
def edit_title(ID):
title = request.get_json().get("title")
anime = database.get_anime(ID)
if anime and title:
anime.title = title
database.update_anime(anime)
return jsonify({"result": anime.title})
return jsonify({"result": False})
@app.route("/fetch", methods=["POST"])
def fetch():
result = False
try:
url = request.get_json().get("url")
r = requests.get(url)
if r.status_code != 200:
return abort(r.status_code)
result = r.content.decode()
except Exception as err:
print(err)
return jsonify({"result": result})
if __name__ == "__main__":
HOST = "0.0.0.0"
PORT = 5050
print("+-------------------------------+")
print(f"URL: http://localhost:{PORT}")
print("+-------------------------------+")
app.run(host=HOST, port=PORT, debug=True)