-
Notifications
You must be signed in to change notification settings - Fork 21
/
apiHandler.py
549 lines (389 loc) · 17.5 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import traceback
from pyramid.response import Response
import DbManagement.MonitorTool
import nameTools as nt
import logging
import json
import settings
import urllib.parse
import os.path
import os
import shutil
class ApiInterface(object):
log = logging.getLogger("Main.API")
def __init__(self, sqlInterface):
self.conn = sqlInterface
def updateSeries(self, request):
inserter = DbManagement.MonitorTool.Inserter()
ret = ""
# Sooooo hacky. Using **{dict} crap in ALL THE PLACES
self.log.info("Parameter update call!")
if "old_buName" in request.params and "new_buName" in request.params or \
"old_mtName" in request.params and "new_mtName" in request.params or \
"old_buId" in request.params and "new_buId" in request.params or \
"old_mtId" in request.params and "new_mtId" in request.params:
if "old_buName" in request.params:
key = "buName"
elif "old_mtName" in request.params:
key = "mtName"
elif "old_mtId" in request.params:
key = "mtId"
elif "old_buId" in request.params:
key = "buId"
else:
raise ValueError("WAT")
self.log.info("Updating {colName} Column".format(colName=key))
newName = request.params["new_{colName}".format(colName=key)].rstrip().lstrip()
oldName = request.params["old_{colName}".format(colName=key)].rstrip().lstrip()
updRowId = int(request.params["id"].rstrip().lstrip())
existingRow = {key: newName}
existingRow = inserter.getRowByValue(**existingRow)
mergeRow = inserter.getRowByValue(dbId=updRowId)
self.log.info("mergeRow= %s", mergeRow)
self.log.info("existingRow= %s", existingRow)
if key == "mtId" or key == "buId":
try:
int(newName)
except ValueError:
traceback.print_exc()
self.log.info("Values = '%s'", newName)
ret = json.dumps({"Status": "Failed", "Message": "IDs is not anm integer!"})
return Response(body=ret)
updateDat = {key: newName}
if not existingRow:
inserter.updateDbEntry(mergeRow['dbId'], **updateDat)
ret = json.dumps({"Status": "Success", "Message": "Updated Row!"})
else:
fromDict = {"dbId": updRowId}
toDict = {"dbId": existingRow['dbId']}
inserter.mergeItems(fromDict, toDict)
ret = json.dumps({"Status": "Success", "Message": "Merged Rows!"})
else:
ret = json.dumps({"Status": "Failed", "Message": "Invalid argument!"})
return Response(body=ret)
def changeRating(self, request):
self.log.info(request.params)
if not "new-rating" in request.params:
return Response(body=json.dumps({"Status": "Failed", "Message": "No new rating specified in rating-change call!"}))
mangaName = request.params["change-rating"]
newRating = request.params["new-rating"]
try:
newRating = float(newRating)
except ValueError:
return Response(body=json.dumps({"Status": "Failed", "Message": "New rating was not a number!"}))
if not mangaName in nt.dirNameProxy:
return Response(body=json.dumps({"Status": "Failed", "Message": "Specified Manga Name not in dir-dict."}))
self.log.info("Calling ratingChange")
nt.dirNameProxy.changeRating(mangaName, newRating)
self.log.info("ratingChange Complete")
return Response(body=json.dumps({"Status": "Success", "Message": "Directory Renamed"}))
def resetDownload(self, request):
self.log.info(request.params)
dbId = request.params["reset-download"]
try:
dbId = int(dbId)
except ValueError:
return Response(body=json.dumps({"Status": "Failed", "Message": "Row ID was not a integer!"}))
cur = self.conn.cursor()
cur.execute("SELECT dlState, dbid FROM MangaItems WHERE dbId=%s", (dbId, ))
ret = cur.fetchall()
if len(ret) == 0:
return Response(body=json.dumps({"Status": "Failed", "Message": "Row ID was not present in DB!"}))
if len(ret) != 1:
return Response(body=json.dumps({"Status": "Failed", "Message": "Did not receive single row for query based on RowId"}))
dlState, qId = ret[0]
if qId != dbId:
return Response(body=json.dumps({"Status": "Failed", "Message": "Id Mismatch! Wat?"}))
if dlState >= 0:
return Response(body=json.dumps({"Status": "Failed", "Message": "Row does not need to be reset!"}))
cur.execute("UPDATE MangaItems SET dlState=0 WHERE dbId=%s", (dbId, ))
cur.execute("COMMIT;")
return Response(body=json.dumps({"Status": "Success", "Message": "Download state reset."}))
def getHentaiTrigramSearch(self, request):
itemNameStr = request.params['trigram-query-hentai-str']
linkText = request.params['trigram-query-linktext']
cur = self.conn.cursor()
cur.execute("""SELECT COUNT(*) FROM hentaiitems WHERE originname %% %s;""", (itemNameStr, ))
ret = cur.fetchone()[0]
if ret:
ret = "<a href='/search-h/h?q=%s'>%s</a>" % (urllib.parse.quote_plus(itemNameStr.encode("utf-8")), linkText)
else:
ret = 'No H Items'
return Response(body=json.dumps({"Status": "Success", "contents": ret}))
def getBookTrigramSearch(self, request):
itemNameStr = request.params['trigram-query-book-str']
linkText = request.params['trigram-query-linktext']
cur = self.conn.cursor()
cur.execute("""SELECT COUNT(*) FROM book_items WHERE title %% %s;""", (itemNameStr, ))
ret = cur.fetchone()[0]
if ret:
ret = "<a href='/books/book-item?title=%s'>%s</a>" % (urllib.parse.quote_plus(itemNameStr.encode("utf-8")), linkText)
else:
ret = 'No Book Items'
return Response(body=json.dumps({"Status": "Success", "contents": ret}))
def deleteItem(self, request):
srcDict = request.params['src-dict']
srcText = request.params['src-path']
srcPathBase = settings.mangaFolders[int(srcDict)]['dir']
srcPathFrag = urllib.parse.unquote(srcText)
print(srcPathBase)
print(srcPathFrag)
fqPath = os.path.join(srcPathBase, srcPathFrag)
fqPath = os.path.abspath(fqPath)
# Prevent path traversal crap.
if not srcPathBase in fqPath:
return Response(body=json.dumps({"Status": "Failed", "contents": "Nice path traversal try!"}))
if not os.path.exists(fqPath):
return Response(body=json.dumps({"Status": "Failed", "contents": "Item does not exist?"}))
if not os.path.exists(settings.recycleBin):
os.mkdir(settings.recycleBin)
dst = fqPath.replace("/", ";")
dst = os.path.join(settings.recycleBin, dst)
self.log.info("Moving item from '%s'", fqPath)
self.log.info(" to '%s'", dst)
try:
shutil.move(fqPath, dst)
# self.addTag(fqPath, "manually-deleted")
except OSError:
self.log.error("ERROR - Could not move file!")
self.log.error(traceback.format_exc())
return Response(body=json.dumps({"Status": "Failed", "contents": 'Could not move file?'}))
return Response(body=json.dumps({"Status": "Success", "contents": 'Item moved to recycle bin!'}))
def addBookList(self, request):
newList = request.params['listName']
cur = self.conn.cursor()
cur.execute("""SELECT COUNT(*) FROM book_series_lists WHERE listname=%s;""", (newList, ))
ret = cur.fetchone()[0]
if ret:
return Response(body=json.dumps({"Status": "Failed", "contents": 'List already exists!'}))
cur = self.conn.cursor()
cur.execute("""INSERT INTO book_series_lists (listname) VALUES (%s);""", (newList, ))
cur.execute('COMMIT')
return Response(body=json.dumps({"Status": "Success", "contents": 'New list added!'}))
def removeBookList(self, request):
delList = request.params['listName']
print("listname: '%s'" % delList)
cur = self.conn.cursor()
cur.execute("""SELECT COUNT(*) FROM book_series_lists WHERE listname=%s;""", (delList, ))
ret = cur.fetchone()
if not ret:
print("List does not exist?")
return Response(body=json.dumps({"Status": "Failed", "contents": 'Cannot delete list that doesn\'t exist!'}))
cur = self.conn.cursor()
cur.execute("""DELETE FROM book_series_lists WHERE listname=%s;""", (delList, ))
cur.execute('COMMIT')
return Response(body=json.dumps({"Status": "Success", "contents": 'List Deleted!'}))
def setListForBook(self, request):
bookId = request.params['set-list-for-book']
listName = request.params['listName']
if not listName:
cur = self.conn.cursor()
cur.execute("""DELETE FROM book_series_list_entries WHERE seriesid=%s;""", (bookId, ))
return Response(body=json.dumps({"Status": "Success", "contents": 'Item list cleared!'}))
# Check if the item already is in the list table.
cur = self.conn.cursor()
cur.execute("""SELECT COUNT(*) FROM book_series_list_entries WHERE seriesid=%s;""", (bookId, ))
ret = cur.fetchone()[0]
if ret:
cur = self.conn.cursor()
cur.execute("""UPDATE book_series_list_entries SET listname=%s WHERE seriesid=%s;""", (listName, bookId))
cur.execute('COMMIT')
return Response(body=json.dumps({"Status": "Success", "contents": 'Updated list for item!'}))
cur = self.conn.cursor()
cur.execute("""INSERT INTO book_series_list_entries (seriesid, listname) VALUES (%s, %s);""", (bookId, listName))
cur.execute('COMMIT')
return Response(body=json.dumps({"Status": "Success", "contents": 'Item list updated!'}))
def setReadForBook(self, request):
bookId = request.params['set-read-for-book']
readChange = request.params['itemDelta']
try:
readChange = int(readChange)
except ValueError:
return Response(body=json.dumps({"Status": "Failed", "contents": 'Change value not an integer!'}))
# Check if the item already is in the list table.
cur = self.conn.cursor()
cur.execute("""SELECT readingprogress FROM book_series WHERE dbid=%s;""", (bookId, ))
retRow = cur.fetchone()
if not retRow:
return Response(body=json.dumps({"Status": "Failed", "contents": 'ID Not in database!'}))
curRead = retRow[0]
total = curRead + readChange
if total < -1:
total = -1
cur.execute("""UPDATE book_series SET readingprogress=%s WHERE dbid=%s;""", (total, bookId))
cur.execute('COMMIT')
if total < 0:
total = '-'
ret = {
"Status": "Success",
"contents": 'Current read-to status: %s!' % total,
"readTo": total
}
return Response(body=json.dumps(ret))
def setRatingForBook(self, request):
bookId = request.params["set-rating-for-book"]
ratingStr = request.params["rating"]
try:
newRating = float(ratingStr)
# Ratings are granular in 0.5 increments. Convert to integer fixed point (the db stores ints)
newRating = newRating * 2
except ValueError:
return Response(body=json.dumps({"Status": "Failed", "contents": 'Change value not an integer!'}))
cur = self.conn.cursor()
cur.execute("""UPDATE book_series SET rating=%s WHERE dbid=%s;""", (newRating, bookId))
cur.execute('COMMIT')
ret = {
"Status": "Success",
"contents": 'Current rating status: %s!' % (newRating/0.5),
}
return Response(body=json.dumps(ret))
def newCustomBook(self, request):
assert request.params["new-custom-book"] == 'true'
title = request.params["new-name"]
cur = self.conn.cursor()
cur.execute("""INSERT INTO book_series (itemname, itemtable) VALUES (%s, (SELECT dbid FROM book_series_table_links WHERE tablename=%s));""", (title, 'books_custom'))
cur.execute('COMMIT')
ret = {
"Status": "Success",
"contents": 'Inserted book: %s!' % title,
}
return Response(body=json.dumps(ret))
def resetCrawlDist(self, request):
assert request.params['reset-book-crawl-dist']
assert request.params['western'] == 'false'
rowid = int(request.params['reset-book-crawl-dist'])
cur = self.conn.cursor()
cur.execute("""UPDATE book_items SET distance=0 WHERE dbid=%s;""", (rowid, ))
cur.execute('COMMIT')
ret = {
"Status": "Success",
"contents": 'Reset crawl distance for item: %s!' % rowid,
}
return Response(body=json.dumps(ret))
def resetDownloadState(self, request):
assert request.params['reset-book-download-state']
rowid = int(request.params['reset-book-download-state'])
cur = self.conn.cursor()
cur.execute("""UPDATE book_items SET distance=0, dlState=0 WHERE dbid=%s;""", (rowid, ))
cur.execute('COMMIT')
ret = {
"Status": "Success",
"contents": 'Reset crawl distance for item: %s!' % rowid,
}
return Response(body=json.dumps(ret))
def deleteCustomBook(self, request):
assert request.params["delete-custom-book"] == 'true'
deleteId = request.params["delete-id"]
cur = self.conn.cursor()
cur.execute("""DELETE FROM book_series WHERE dbid=%s AND itemtable=(SELECT dbid FROM book_series_table_links WHERE tablename=%s);""", (deleteId, 'books_custom'))
cur.execute('COMMIT')
ret = {
"Status": "Success",
"contents": 'Item Deleted: %s!' % (deleteId),
}
return Response(body=json.dumps(ret))
def handleApiCall(self, request):
'''
API Call handler.
All API calls are done as GET requests. The response value is a JSON dictionary (or an error page).
All response JSON objects will have a 'Status' Field, containing the literal string 'Success' if
the API call was successful, or 'Failed' if it failed.
Each response should also have a "contents" field, containing either the call's return value, or
a human readable status message (for calls that permute server state, rather then do lookup)
Available API Methods:
- "change-rating"
Required parameters:
- "change-rating": Valid series name (e.g. something that can be looked up in the NT interface)
- "new-rating": The new rating of the series, as anything that will successfully cast to float()
Updates the rating of a series by modifying the directory name.
In general, it should only return a response once the update is complete. This isn't *completely*
functional, though. There is some odd behaviour when directories are moved that I have not
bothered to properly debug.
- "update-series"
Required parameters:
- "old_buName"
- "old_mtName"
- "old_buId"
- "old_mtId"
Used for cross-linking Mangaupdates and MangaTraders series.
Basically useless, should probably be removed.
- "reset-download"
Required parameters:
- "reset-download": database ID for the manga download to reset.
Resets the download state of a item in the database. Only functions on a download with a dlState < 0,
e.g. a failed download.
- "trigram-query-hentai-str"
Required parameters:
- 'trigram-query-hentai-str': The name of the hentai to search for
- 'trigram-query-linktext': The string content of the returned link if the search returned results
Do a trigram search (e.g. fuzzy text search) for a hentai title.
'contents' contains HTML markup for a web-interface link to a page containing the search results, or
"no entries found" text.
- "trigram-query-book-str"
Required parameters:
- 'trigram-query-book-str': The name of the book to search for
- 'trigram-query-linktext': The string content of the returned link if the search returned results
Do a trigram search (e.g. fuzzy text search) for a book title.
'contents' contains HTML markup for a web-interface link to a page containing the search results, or
"no entries found" text.
Note: This is functionally identical to the behaviour of "trigram-query-hentai-str", aside from the table it searches.
- "delete-item"
Required parameters:
- 'src-dict': The nt dictionary that the item is from
- 'src-path': the path of the item within the base path of the src-dict.
Move an item to the recycle bin.
The path is generated by taking the base path of the dict specified by 'src-dict',
concatenating `src-path` onto it.
The "deleted" item is then moved to the recycle bin directory specified in the settings.py file
################################################################################
# Book Management Stuff:
################################################################################
- "add-book-list"
Required parameters:
- 'listName'
- "remove-book-list"
- "set-list-for-book"
- "set-read-for-book"
'''
self.log.info("API Call! %s", request.params)
if request.remote_addr in settings.noHighlightAddresses:
return Response(body=json.dumps({"Status": "Failed", "contents": "API calls are blocked from the reverse-proxy IP."}))
if "change-rating" in request.params:
self.log.info("Rating change!")
return self.changeRating(request)
elif "update-series" in request.params:
self.log.info("Update series!")
return self.updateSeries(request)
elif "reset-download" in request.params:
self.log.info("Download Reset!")
return self.resetDownload(request)
elif "trigram-query-hentai-str" in request.params and "trigram-query-linktext" in request.params:
self.log.info("Trigram query existence check")
return self.getHentaiTrigramSearch(request)
elif "trigram-query-book-str" in request.params and "trigram-query-linktext" in request.params:
self.log.info("Trigram query existence check")
return self.getBookTrigramSearch(request)
elif 'delete-item' in request.params:
return self.deleteItem(request)
elif 'add-book-list' in request.params:
return self.addBookList(request)
elif 'remove-book-list' in request.params:
return self.removeBookList(request)
elif 'set-list-for-book' in request.params:
return self.setListForBook(request)
elif 'set-read-for-book' in request.params:
return self.setReadForBook(request)
elif "set-rating-for-book" in request.params:
return self.setRatingForBook(request)
elif "new-custom-book" in request.params:
return self.newCustomBook(request)
elif "delete-custom-book" in request.params:
return self.deleteCustomBook(request)
elif 'reset-book-crawl-dist' in request.params:
return self.resetCrawlDist(request)
elif 'reset-book-download-state' in request.params:
return self.resetDownloadState(request)
else:
self.log.warning("Unknown API call")
self.log.warning("Call params: '%s'", request.params)
return Response(body=json.dumps({"Status": "Failed", "contents": "Unknown API Call.\nCall parameters: '%s'." % str(list(request.params.keys()))}))