-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
224 lines (188 loc) · 8.19 KB
/
server.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from typing import cast
from flask import Flask, request, render_template, url_for, jsonify, session, redirect
from flask.json import tag
from flask_session import Session
import hydrus
import base64
import json
import os
import secrets
import sqlite3 as sql
import requests
import re
app = Flask(__name__)
SESSION_TYPE = 'filesystem'
SESSION_FILE_DIR = "hrt_session/"
app._static_folder = os.path.abspath("templates/static/")
app.secret_key = "cookiesonfire"
app.config.from_object(__name__)
Session(app)
def search_files(api_key, api_url, search_tags, inboxBool, archiveBool):
cl = hydrus.Client(api_key, api_url)
fids = cl.search_files(search_tags, inboxBool, archiveBool)
return fids
def get_services(api_key, api_url):
cl = hydrus.Client(api_key, api_url)
return cl.get_tag_services()
def save_session(api_key, api_url, service):
session['api_key'] = api_key
session['api_url'] = api_url
session['service'] = service
def generate_session_id():
session['session_id'] = secrets.token_hex(10)
def save_sql(fids):
session_id = session['session_id']
fids = ','.join(str(e) for e in fids)
with sql.connect("session.db") as con:
cur = con.cursor()
cur.execute("REPLACE INTO session (session_id, file_ids) VALUES (?,?)", (str(
session_id), str(fids)))
con.commit()
def get_fids_from_sql():
session_id = session['session_id']
with sql.connect("session.db") as con:
cur = con.cursor()
cur.execute(
"SELECT file_ids FROM session WHERE session_id IS (?)", (str(session_id),))
fids = cur.fetchone()
return fids
defaultnamespaceColors = [
# ["className","regex","hexColor"], apply top to bottom
["character", "^character:.*$", "#00aa00"],
["creator", "^creator:.*$", "#ff0000"],
["meta", "^meta:.*$", "#6f6f6f"], # default #111111
["person", "^person:.*$", "#008000"],
["series", "^series:.*$", "#d200d2"],
["studio", "^studio:.*$", "#ff0000"],
["namespaced", "^.*:.*$", "#72a0c1"],
["unnamespaced", "^(?!.*:).*$", "#00aaff"]
]
@app.route('/index', methods=['GET', 'POST'])
def ad():
try:
if request.method == 'GET':
return redirect(url_for('index'))
try:
session['session_id']
except KeyError:
generate_session_id()
# start POST processing
save_session(request.form.get('api_key'), request.form.get(
'api_url'), request.form.get('service'))
api_key = session['api_key']
api_url = session['api_url']
if 'archive' in request.form.getlist('location'):
archiveBool = True
else:
archiveBool = False
if 'inbox' in request.form.getlist('location'):
inboxBool = True
else:
inboxBool = False
session_id = session['session_id']
post_tags = request.form.get('tags')
tags = post_tags.split()
clean_tags = []
for tag in tags:
clean_tags.append(tag.replace('_', ' '))
fids = search_files(api_key, api_url, clean_tags,
inboxBool, archiveBool)
total_ids = len(fids)
save_sql(fids)
session['appendTag'] = []
for tag in request.form.get('appendTags').split():
session['appendTag'].append(tag.replace('_', ' '))
return render_template('results.html', tagrepo=get_services(api_key, api_url), ids=total_ids, tags=post_tags)
except hydrus.InsufficientAccess:
return render_template('index.html', error="Insufficient access to Hydrus API", namespaces=session['namespaceColors'])
except hydrus.ServerError:
return render_template('index.html', error="Hydrus API encountered a server error", namespaces=session['namespaceColors'])
except hydrus.APIError:
return render_template('index.html', error="Hydrus API encountered a server error", namespaces=session['namespaceColors'])
@app.route('/show-file/<id>', methods=['GET', 'POST'])
def ads(id):
try:
session['appendTagIsSet'] = False
api_key = session['api_key']
if session['api_url'].endswith('/'):
api_url = session['api_url'][:-1]
else:
api_url = session['api_url']
cl = hydrus.Client(api_key, api_url)
fids = get_fids_from_sql()
fids = list(fids)[0].split(',')
intid = int(id)
iid = int(fids[intid])
nid = str(int(id) + 1)
total_ids = len(fids)
image = api_url+"/get_files/file?file_id=" + \
str(int(fids[intid]))+"&Hydrus-Client-API-Access-Key="+api_key
next_images = [api_url+"/get_files/file?file_id="+str(int(fids[intid+1]))+"&Hydrus-Client-API-Access-Key="+api_key, api_url+"/get_files/file?file_id="+str(int(fids[intid+2]))+"&Hydrus-Client-API-Access-Key=" +
api_key, api_url+"/get_files/file?file_id="+str(int(fids[intid+3]))+"&Hydrus-Client-API-Access-Key="+api_key, api_url+"/get_files/file?file_id="+str(int(fids[intid+4]))+"&Hydrus-Client-API-Access-Key="+api_key]
metadata = json.loads(json.dumps(cl.file_metadata(file_ids=[iid])[0]))
session['metadata'] = metadata
if request.method == 'POST':
if request.form.get('tagrepo') != None:
session['selectedTagRepo'] = request.form.get('tagrepo')
def checkModifiable(tag):
try:
if tag in metadata['service_names_to_statuses_to_tags'][session['selectedTagRepo']]['0']:
return True
else:
return False
except:
return False
def matchNamespace(tag):
for namespace in session['namespaceColors']:
if re.fullmatch(namespace[1], tag):
return namespace[0]
return ""
return render_template('show-file.html', image=image, next_images=next_images, nid=nid, current_id=intid, total_ids=total_ids, meta=metadata, selectedService=session['selectedTagRepo'], checkModifiable=checkModifiable, matchNamespace=matchNamespace, namespaces=session['namespaceColors'])
except IndexError:
return redirect(url_for('index'))
except KeyError: # expired session
return redirect(url_for('index'))
@app.route('/updateTags', methods=['POST'])
def ajaxUpdate():
data = request.get_json()
tagsToAdd = data['add']
tagsToDel = data['del']
hash = data['hash']
tag_repo = session['selectedTagRepo']
if not session['appendTagIsSet']:
try:
if not all(tag in session['metadata']['service_names_to_statuses_to_tags'][tag_repo]['0'] for tag in session['appendTag']):
for tag in session['appendTag']:
if tag not in session['metadata']['service_names_to_statuses_to_tags'][tag_repo]['0'] and tag not in tagsToAdd:
tagsToAdd.append(tag)
session['appendTagIsSet'] = True
except KeyError:
session['appendTagIsSet'] = True
for tag in session['appendTag']:
tagsToAdd.append(tag)
matchedTags = {}
for tag in tagsToAdd:
for namespace in session['namespaceColors']:
if re.fullmatch(namespace[1], tag):
matchedTags.update({tag: namespace[0]})
break
listOfTags = {tag_repo: {"0": tagsToAdd, "1": tagsToDel}}
cl = hydrus.Client(session['api_key'], session['api_url'])
cl.add_tags([hash], service_to_action_to_tags=listOfTags)
listOfTags.update({"matches": matchedTags})
return jsonify(listOfTags) # updated lsit of tags
@app.route('/updatePrefs', methods=['POST'])
def updatePrefs():
data = request.get_json()
if data['namespaceColors'] == "":
# return default namespace colors if there is no custom namespaceColors
session['namespaceColors'] = defaultnamespaceColors
return jsonify({"namespaceColors": session['namespaceColors']})
session['namespaceColors'] = data['namespaceColors']
# print(session['namespaceColors'])
return jsonify({"namespaceColors": session['namespaceColors']})
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8243, debug=True)