added webserver capabilities to add/rm anime

This commit is contained in:
StochasticMouse 2024-01-20 17:19:11 +01:00
parent bff23b1ffd
commit e24f4911db
2 changed files with 106 additions and 0 deletions

28
templates/index.html Normal file
View File

@ -0,0 +1,28 @@
<table id="csv_table">
{% for row in csv %}
<tr>
{% for col in row %}
<td>{{ col }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<p></p>
<p></p>
<p></p>
<form action="new_animu" method = "POST">
<p>Vuoi aggiungerne uno nuovo?</p>
<table id="new_animu_table">
<tr>
<td>Url:</td><td><input type = "text" name = "field_url" /></td>
<td>Nome serie:</td><td><input type = "text" name = "field_folder" /></td>
<td>Stagione:</td><td><input type = "text" name = "field_season" /></td>
<td><input type = "submit" value = "submit" /></td>
</tr>
</table>
</form>
<form action="del_animu" method= "POST">
<p>Vuoi eliminare un anime?</p>
<input type = "text" name = "del_field" />
<input type = "submit" value = "submit" />
</form>

78
webserver.py Normal file
View File

@ -0,0 +1,78 @@
from flask import Flask, render_template, request, redirect
import csv
HOST_NAME = "localhost"
HOST_PORT = 5000
app = Flask(__name__)
@app.route("/")
def mainpage():
with open("animelist.csv") as file:
reader = csv.reader(file)
enum = list(enumerate(list(reader)))
lista = []
for el in enum:
lista.append([el[0]]+el[1])
return render_template('index.html',csv=lista)
@app.route("/new_animu", methods = ['POST', 'GET'])
def new_animu():
if request.method == 'GET':
return redirect('/')
if request.method == 'POST':
form_data = request.form
#print(form_data)
if add_animu(form_data):
return f'Anime aggiunto con successo'
return f'Errore aggiungendo anime, hai inserito correttamente i valori?'
@app.route("/del_animu", methods = ['POST', 'GET'])
def del_animu():
if request.method == 'GET':
return redirect('/')
if request.method == 'POST':
form_data = request.form
print(form_data)
if del_animu(form_data):
return f'Anime rimosso con successo'
return f'Errore rimuovendo anime, hai inserito correttamente i valori?'
def add_animu(form_data):
try:
if not (form_data['field_season'] and form_data['field_url'] and form_data['field_folder']):
return False
stagione = int(form_data['field_season'])
new_entry = [form_data['field_url'],
form_data['field_folder'] + '/Season ' + str(stagione),
'Episode S' + format(stagione,'02d'),
1,
1]
with open("animelist.csv",mode='a') as animecsv: #link,cartella>
csvfile=csv.writer(animecsv)
csvfile.writerow(new_entry)
return True
except Exception as e:
print(e)
return False
def del_animu(form_data):
try:
if not form_data['del_field']:
return False
data = []
with open("animelist.csv", 'r', newline='') as file:
csv_reader = csv.reader(file)
for index, row in enumerate(csv_reader):
if index != int(form_data['del_field']):
data.append(row)
# Write the remaining data back to the CSV file
with open("animelist.csv", 'w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerows(data)
return True
except Exception as e:
print(e)
return False
if __name__ == "__main__":
app.run(HOST_NAME, HOST_PORT)