-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path__init__.py
203 lines (156 loc) · 4.68 KB
/
__init__.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
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
from configparser import ConfigParser
import re
import json
import os
from flask import *
import sys
app = Flask(__name__)
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
LOG = None # variabile usata per inviare messaggi a schermo
@app.template_filter()
def msgSafe(msg):
msg = re.sub(r"[^a-zA-Z]", "", msg)
return msg
@app.route('/favicon.ico')
def favicon():
return redirect(url_for('static', filename='favicon.ico'), code=302)
@app.route('/append_anime', methods=['POST']) # Per aggiungere un anime
def append_anime():
res = request.form
data = {
"title": request.form['title'],
"season": "absolute",
"absolute": ("absolute" in request.form),
"links": request.form.getlist('link')
}
if not data["absolute"]:
data["season"]= request.form['season']
# print(data, file=sys.stderr)
global LOG
LOG = appendAnime(data)
return redirect(url_for('index'))
@app.route('/delete_anime', methods=['POST']) # Per cancellare un anime
def delete_anime():
res = request.form
# print(res, flush=True)
deleteAnime(res['delete_anime'])
return redirect(url_for('index'))
@app.route('/settings')
def settings():
setts = ReadSettings()
env = getmyenv()
return render_template('settings.html', settings=setts, env=env)
@app.route('/settings_update', methods=['POST'])
def settings_update():
res = request.form
settings = {
"LogLevel": request.form.get("LogLevel"),
"RenameEp": False if request.form.get("RenameEp") is None or request.form.get("MoveEp") is None else True,
"MoveEp": False if request.form.get("MoveEp") is None else True,
"ScanDalay": int(request.form.get("ScanDalay"))
}
WriteSettings(settings)
# print(request.form.get("RenameEp"), file=sys.stderr)
# print(res, file=sys.stderr)
return redirect(url_for('settings'))
@app.route('/index')
@app.route('/')
def index():
log = get_log()
anime = readData()
env = getmyenv()
return render_template('index.html', infos=anime, log=log, env=env)
####### DATA
def readData():
with open('json/table.json' , 'r') as f:
return json.loads(f.read())
def writeData(table):
f = open("json/table.json", 'w')
f.write(json.dumps(table, indent=4))
f.close()
return table
def deleteAnime(title):
table = readData()
for anime in table:
if anime["title"] == title:
table.remove(anime)
break
writeData(table)
def appendAnime(data):
def myOrder(serieInfo):
return serieInfo["title"]
table = readData()
log = None # In caso di errore viene segnalato a video
for anime in table:
if data["title"] == anime["title"]: # Se esiste già l'anime nella tabella
if data["season"] in anime["seasons"]: # Se esiste già la stagione
for link in data["links"]:
if link not in anime["seasons"][data["season"]]: # Se il link non è già presente
anime["seasons"][data["season"]].append(link) # aggiunge un'altro link
log = "Nuovo link aggiunto"
else:
if not anime["absolute"] and not data["absolute"]: # Se la numerazione non è assoluta
anime["seasons"][data["season"]] = list(data["links"]) # inizializza una nuova stagione
log = f"Stagione {data['season']} di {data['title']} aggiunta"
else:
log = "ERRORE"
break
else: # se non è stato trovato nessun anime
table.append({
"title": data["title"],
"seasons": {data["season"]: data["links"]},
"absolute": data["absolute"]
})
log = f"{data['title']} aggiunto"
# print(f"\n-> È stata aggiunta la serie {SonarrTitle}.")
table.sort(key=myOrder)
writeData(table)
return log
### getenv
def getmyenv():
env = {}
env["SONARR_URL"] = os.getenv('SONARR_URL') # Indirizzo ip + porta di sonarr
env["API_KEY"] = os.getenv('API_KEY') # Chiave api di sonarr
env["CHAT_ID"] = os.getenv('CHAT_ID') # telegramm
env["BOT_TOKEN"] = os.getenv('BOT_TOKEN') # telegramm
env["VERSION"] = os.getenv('VERSION') # versione
return env
### Setting
def ReadSettings():
data = {
"LogLevel":"DEBUG",
"RenameEp":True,
"MoveEp":True,
"ScanDalay": 30
}
json_location = "json/settings.json"
updateFix = False
settings = {}
if os.path.exists(json_location):
with open(json_location, 'r') as f:
settings = json.loads(f.read())
for info in data:
if info not in settings:
settings[info] = data[info]
updateFix = True
else:
settings = data
updateFix = True
if updateFix:
with open(json_location, 'w') as f:
f.write(json.dumps(settings, indent='\t'))
return settings
def WriteSettings(settings):
json_location = "json/settings.json"
with open(json_location, 'w') as f:
f.write(json.dumps(settings, indent='\t'))
### OTHER
def get_log():
global LOG
log = LOG
LOG = None
return log
if __name__ == "__main__":
app.run(debug=False, use_evalex=False, use_reloader=False, host='0.0.0.0')