-
Notifications
You must be signed in to change notification settings - Fork 7
/
apiHandler.py
188 lines (120 loc) · 5.16 KB
/
apiHandler.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
from pyramid.response import Response
from settings import settings
import logging
import json
class ApiInterface(object):
log = logging.getLogger("Main.API")
def __init__(self, sqlInterface):
self.conn = sqlInterface
def updateName(self, request):
if not "id" in request.params or not "aName" in request.params:
return Response(body="Missing required parameters!")
print("Parameter update call!")
print(request.params)
cur = self.conn.cursor()
ret = cur.execute('SELECT id, siteName, artistName FROM %s WHERE id=?;' % settings["dbConf"]["namesDb"], (request.params["id"], ))
rets = ret.fetchone()
if not len(rets):
return Response(body=json.dumps({"Status": "Error", "Message": "No artist for specified ID!"}))
rowId, siteName, artistName = rets
if artistName == request.params["aName"]:
return Response(body=json.dumps({"Status": "Error", "Message": "No changes made?"}))
cur.execute("UPDATE %s SET artistName=?, WHERE id=?" % settings["dbConf"]["namesDb"], (request.params["aName"],
request.params["id"]))
self.conn.commit()
return Response(body=json.dumps({"Status": "Success", "Message": "Updated settings for artist."}))
def changeRating(self, request):
print("Parameter update call!")
print(request.params)
return Response(body=json.dumps({"Status": "Error", "Message": "Rating change not implemented!"}))
# 'NestedMultiDict([('id', '9'), ('aname_da', ''), ('aname_fa', ''), ('aname_hf', ''), ('aname_ib', ''), ('aname_wy', ''), ('change-upload-status', 'true')])'
def changeUploadStatus(self, request):
print("Parameter update call!")
print(request.params)
# Verify all the keys
requireKeys = ['id', 'change-upload-status']
for key in settings['artSites']:
# Skip pixiv.
if key == 'px':
continue
key = 'aname_{key}'.format(key=key)
requireKeys.append(key)
if not all([key in request.params for key in requireKeys]):
return self.errResponse("Missing required key in request params.")
setKeys = []
setParams = []
for key in settings['artSites']:
# Skip pixiv.
if key == 'px':
continue
lookupKey = 'aname_{key}'.format(key=key)
setKey = '{key}id=?'.format(key=key)
setKeys.append(setKey)
if request.params[lookupKey]:
setParams.append(request.params[lookupKey])
else:
setParams.append(None)
params = setParams + [request.params["id"]]
query = "UPDATE {table} SET {cols} WHERE id=?;".format(table=settings["dbConf"]["uploadGalleries"], cols=', '.join(setKeys))
print('query', query)
print('params', params)
cur = self.conn.cursor()
ret = cur.execute(query, params)
self.conn.commit()
return Response(body=json.dumps({"Status": "Success", "Message": "Updated artist state!", 'reload': True}))
def createEmptyRow(self, request):
cur = self.conn.cursor()
cur.execute("INSERT INTO {table} (uploadTime) VALUES (0);".format(table=settings["dbConf"]["uploadGalleries"]))
self.conn.commit()
return Response(body=json.dumps({"Status": "Success", "Message": "Inserted new artist!", 'reload': True}))
def nameAdd(self, request):
requiredKeys = ['target', 'add', 'site', 'artistName']
for item in requiredKeys:
if not item in request.params:
return Response(body=json.dumps({"Status": "Failed", "Message": "Missing required parameters."}))
site = request.params['site']
if not site in settings["artSites"]:
return Response(body=json.dumps({"Status": "Failed", "Message": "Invalid source site."}))
add = request.params['add']
if not add == "True":
return Response(body=json.dumps({"Status": "Failed", "Message": "Add is false?"}))
name = request.params['artistName']
if not name:
return Response(body=json.dumps({"Status": "Failed", "Message": "Name is empty."}))
cur = self.conn.cursor()
ret = cur.execute("SELECT * FROM %s WHERE siteName=? AND artistName=?;" % settings["dbConf"]["namesDb"], (site, name))
have = ret.fetchall()
if have:
return Response(body=json.dumps({"Status": "Failed", "Message": "Name is already in database!"}))
cur.execute("INSERT INTO %s (siteName, artistName) VALUES (?, ?);" % settings["dbConf"]["namesDb"], (site, name))
self.conn.commit()
def errResponse(self, errorMessage=None):
ret = {}
ret['Status'] = 'Failed'
if not errorMessage:
ret['Message'] = 'Error in API Call!'
else:
ret['Message'] = 'Error in API Call! \n'
ret['Message'] += 'Error message: \n'
ret['Message'] += errorMessage
return Response(body=json.dumps(ret))
def handleApiCall(self, request):
print("API Call!", request.params)
if "change-rating" in request.params:
print("Rating change!")
return self.changeRating(request)
elif "change-upload-status" in request.params:
print("Rating change!")
return self.changeUploadStatus(request)
elif "change-artist-name" in request.params:
print("Updating artist's name!")
return self.updateName(request)
elif 'target' in request.params:
print("Adding artist!")
return self.nameAdd(request)
elif 'add-new-row' in request.params:
print("Adding empty upload row!")
return self.createEmptyRow(request)
else:
print("Unknown API call!")
return self.errResponse("Unknown command: '%s'" % request.params)