This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathadd_library.py
executable file
·480 lines (386 loc) · 16 KB
/
add_library.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
#!/usr/bin/python3
import sys
import json
import os
import configparser
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
LIBDIR = 'opacclient/opacapp/src/main/assets/bibs/'
API_KEY_FILE = 'googlemaps_api_key.txt'
TYPES = [
'NONE', 'BOOK', 'CD', 'CD_SOFTWARE', 'CD_MUSIC', 'DVD', 'MOVIE', 'AUDIOBOOK', 'PACKAGE',
'GAME_CONSOLE', 'EBOOK', 'SCORE_MUSIC', 'PACKAGE_BOOKS', 'UNKNOWN', 'NEWSPAPER',
'BOARDGAME', 'SCHOOL_VERSION', 'MAP', 'BLURAY', 'AUDIO_CASSETTE', 'ART', 'MAGAZINE',
'GAME_CONSOLE_WII', 'GAME_CONSOLE_NINTENDO', 'GAME_CONSOLE_PLAYSTATION',
'GAME_CONSOLE_XBOX', 'LP_RECORD', 'MP3', 'URL', 'EVIDEO','EDOC','EAUDIO',
'DEVICE', 'MICROFORM']
def getInput(required=False, default=None):
if default is not None:
print("[Standard %s]" % default, end=" ")
print("> ", end="")
inp = input().strip()
if default is not None and (inp is None or inp == ''):
return default
if required and (inp is None or inp == ''):
print("Feld muss gefüllt werden.")
return getInput(required=required, default=default)
if inp is None or inp == '':
return None
return inp
def loadGeoPossibilities(data, api_key=None):
possibilities = []
if api_key:
for address in ('%s, %s, %s' % (data['title'], data['city'], data['state']),
'%s, %s, %s' % ('Bibliothek', data['city'], data['state']),
data['city']):
uri = f'https://maps.googleapis.com/maps/api/geocode/json?key={api_key}&' + \
urllib.parse.urlencode({'address': address, 'sensor': 'true'})
jsoncontent = urllib.request.urlopen(uri).read().decode()
geocode = json.loads(jsoncontent)
if geocode['status'] != 'OK':
print("ERROR!")
for res in geocode['results']:
possibilities.append(
(
", ".join([a["long_name"] for a in res['address_components']]),
[float(res['geometry']['location']['lat']), float(
res['geometry']['location']['lng'])]
)
)
else:
api_url = 'https://nominatim.openstreetmap.org/search'
query = 'format=json&polygon=0&addressdetails=0&limit=40'
parameters1 = urllib.parse.quote(','.join((data['title'], data['city'].partition('(')[0],
data['state'],data['country'])), safe=',')
parameters2 = urllib.parse.urlencode({'country': data['country'], 'state': data['state'],
'city': data['city'].partition('(')[0]})
for uri in (f'{api_url}/{parameters1}?{query}',
f'{api_url}?{query}&amenity=library&{parameters2}'):
jsoncontent = urllib.request.urlopen(uri).read().decode()
for p in json.loads(jsoncontent):
possibilities.append((p["display_name"], [float(p['lat']), float(p['lon'])]))
return possibilities
class Api:
def accountSupported(self):
return True
def prompt(self, data):
return data
class Bibliotheca(Api):
def accountSupported(self):
return True
def prompt(self, data):
datadata = data['data']
global TYPES
print("Muss eine bestimmte Datenbank geladen werden?")
print("Enter drücken, wenn nicht benötigt.")
inp = getInput(required=False)
suffix = ''
if inp is not None:
datadata['db'] = inp
print("Welchen Suffix hat diese Datenbank im System? (Bsp. _DB1)")
inp = getInput(required=False, default='')
if inp is not None:
suffix = inp
fetched = None
try:
fetched = self._fetchData(datadata['baseurl'], suffix)
datadata['accounttable'] = fetched['accounttable']
datadata['reservationtable'] = fetched['reservationtable']
datadata['copiestable'] = fetched['copiestable']
except Exception as e:
print(str(e))
print("WARNUNG! Konfiguration konnte nicht ausgelesen werden. HANDARBEIT NÖTIG!")
print("Mehr Informationen:")
print("https://github.com/raphaelm/opacclient/wiki/Supported-library-types#bibliotheca")
if fetched is not None:
if len(fetched['mediatypes']) > 0:
print("Bitte weise die Medientypen ihren Entsprechungen in der App zu.")
print("Verfügbar sind:")
print(" ".join(sorted(TYPES)))
print("")
datadata['mediatypes'] = {}
for k, v in fetched['mediatypes'].items():
inp = ''
while inp not in TYPES:
print("'%s' ('%s')?" % (v, k))
inp = getInput(required=False, default="UNKNOWN")
datadata['mediatypes'][k] = inp
data['data'] = datadata
return data
def _fetchData(self, url, suff=''):
config = configparser.RawConfigParser(allow_no_value=True, strict=False)
if os.path.exists('w3oini.txt'):
config.read_string(open('w3oini.txt', 'rb').read().decode('iso-8859-1'))
else:
config.read_string(urllib.request.urlopen(url + '/w3oini.txt').read().decode('iso-8859-1'))
data = {
'accounttable': {},
'reservationtable': {},
'copiestable': {},
}
i_acc = 0
i_res = 0
for i in range(1, 21):
conf = config.get("ANZEIGEKONTOFELDER", "konto" + str(i))
if conf == '':
continue
key = conf.split("#")[0].lower()
if key in ('exemplarnr', 'buchungsnr'):
data['accounttable']['barcode'] = i_acc
i_acc += 1
elif key == 'verf':
data['accounttable']['author'] = i_acc
data['reservationtable']['author'] = i_res
i_acc += 1
i_res += 1
elif key == 'titel':
data['accounttable']['title'] = i_acc
data['reservationtable']['title'] = i_res
i_acc += 1
i_res += 1
elif key == 'frist':
data['accounttable']['returndate'] = i_acc
i_acc += 1
elif key == 'bereit':
data['reservationtable']['availability'] = i_res
i_res += 1
elif key == 'ausleihstatus':
data['accounttable']['status'] = i_acc
i_acc += 1
elif key == 'zwst':
data['accounttable']['homebranch'] = i_acc
data['reservationtable']['branch'] = i_res
i_acc += 1
i_res += 1
elif key == 'ausleihstelle':
data['accounttable']['lendingbranch'] = i_acc
i_acc += 1
elif key == 'mediengrp' or key == 'reserviert' or key == 'saeumnisgebuehr':
i_acc += 1
elif key == 'bereit bis':
data['reservationtable']['expirationdate'] = i_res
i_res += 1
else:
print("WARNING! NOT COUNTING ", key, url)
sys.exit(0)
data['accounttable']['prolongurl'] = i_acc
data['reservationtable']['cancelurl'] = i_res
if ('lendingbranch' not in data['accounttable']
or data['accounttable']['lendingbranch'] == -1) and (
'homebranch' in data['accounttable'] and data['accounttable']['homebranch'] > 0):
data['accounttable']['lendingbranch'] = data['accounttable']['homebranch']
i_copy = 0
for i in range(1, 11):
conf = config.get("ANZEIGE_EXEMPLAR" + suff, "AE" + str(i))
if conf == '':
continue
key = conf.split("#")[1]
if key == 'buchungsnr':
data['copiestable']['barcode'] = i_copy
elif key == 'zweigstelle':
data['copiestable']['branch'] = i_copy
elif key == 'standort2':
data['copiestable']['department'] = i_copy
elif key == 'standort':
data['copiestable']['location'] = i_copy
elif key == 'exemplarstatus':
data['copiestable']['status'] = i_copy
elif key == 'rueckgabedatum':
data['copiestable']['returndate'] = i_copy
elif key == 'Auslanzvorbestakt':
data['copiestable']['reservations'] = i_copy
i_copy += 1
data['mediatypes'] = {}
for i in range(1, 100):
if not config.has_option("ANZEIGE_MEDIGRPPIC", "MEDIGRPPIC" + str(i)):
continue
conf = config.get("ANZEIGE_MEDIGRPPIC", "MEDIGRPPIC" + str(i))
if conf == '' or conf is None:
continue
split = conf.split("#")
data['mediatypes'][split[1]] = split[2]
return data
class Sisis(Api):
def accountSupported(self):
return True
def prompt(self, data):
print("Sind zusätzliche Parameter nötig?")
print("Ein häufiges Beispiel wäre sowas wie 'Login=opsb'")
inp = getInput(required=False)
if inp is not None:
data['data']['startparams'] = inp
return data
class TouchPoint(Api):
def accountSupported(self):
return False
class WebOpacNet(Api):
def accountSupported(self):
return False
class WinBiap(Api):
def accountSupported(self):
return True
class Adis(Api):
def accountSupported(self):
return True
def prompt(self, data):
print("Sind zusätzliche Parameter nötig?")
print("Ein häufiges Beispiel wäre sowas wie 'service=direct/0/Home/$DirectLink&sp=S127.0.0.1%3A23002&sp=SS10000000'")
inp = getInput(required=False)
if inp is not None:
data['data']['startparams'] = inp
return data
class Biber1992(Api):
def accountSupported(self):
return True
def prompt(self, data):
print("Opac-Ordner?")
inp = getInput(required=False, default='opax')
if inp is not None:
data['data']['opacdir'] = inp
return data
print("WARNUNG! Konfiguration kann nicht ausgelesen werden. HANDARBEIT NÖTIG!")
return data
class VuFind(Api):
def accountSupported(self):
return False
class Zones(Api):
def accountSupported(self):
return False
class Primo(Api):
def accountSupported(self):
return False
def prompt(self, data):
print("VID?")
inp = getInput(required=True)
data['data']['db'] = inp
print("Sprachen (short codes, kommagetrennt)?")
inp = getInput(required=True)
data['data']['languages'] = inp.split(",")
return data
class Pica(Api):
account = True
def accountSupported(self):
return True
def prompt(self, data):
print("DB-Nummer?")
inp = getInput(required=True)
data['data']['db'] = inp
return data
class IOpac(Api):
def accountSupported(self):
return True
class Littera(Api):
def accountSupported(self):
return False
class Open(Api):
def accountSupported(self):
return False
def prompt(self, data):
data['data']['urls'] = {}
baseurl = data['data']['baseurl'] + '/'
try:
html = urllib.request.urlopen(baseurl).read().decode('utf-8')
doc = BeautifulSoup(html, 'html.parser')
elems = doc.select('#dnn_dnnNAV_ctldnnNAV li a')
for elem in elems:
name = elem.get_text()
if name in ('Einfache Suche'):
data['data']['urls']['simple_search'] = elem['href'].replace(baseurl, '')
elif name in ('Erweiterte Suche', 'Profisuche'):
data['data']['urls']['advanced_search'] = elem['href'].replace(baseurl, '')
except urllib.error.HTTPError:
pass
if not 'simple_search' in data['data']['urls']:
print("URL für Einfache Suche?")
data['data']['urls']['simple_search'] = getInput(required=True)
if not 'advanced_search' in data['data']['urls']:
print("URL für Erweiterte Suche?")
data['data']['urls']['advanced_search'] = getInput(required=True)
return data
APIS = {
'bibliotheca' : Bibliotheca,
'sisis' : Sisis,
'touchpoint' : TouchPoint,
'biber1992' : Biber1992,
'zones' : Zones,
'iopac' : IOpac,
'pica' : Pica,
'adis' : Adis,
'webopac.net' : WebOpacNet,
'winbiap' : WinBiap,
'vufind' : VuFind,
'primo' : Primo,
'open' : Open,
'web-opac.at' : Littera,
}
data = {}
if __name__ == '__main__':
print("Hallo! Dieses Skript hilft dir, eine neue Bibliothek hinzuzufügen")
if not os.path.isdir(LIBDIR):
print("Bitte wechsle in das Verzeichnis, in dem die App liegt.")
sys.exit(0)
print("In welcher Stadt befindet sich die Bibliothek?")
print("Suffixe wie 'Frankfurt (Main)' werden in Klammern gesetzt.")
data['city'] = getInput(required=True)
print("In welchem Land liegt die Bibliothek?")
data['country'] = getInput(required=True, default="Deutschland")
print("In welchem Bundesland ist die Bibliothek?")
print("In Deutschland und Österreich werden Bundesländer benutzt, in der Schweiz Kantone.")
data['state'] = getInput(required=True)
print("Wie heißt die Bibliothek?")
print("Dies sollte etwas in dieser Stadt eindeutiges sein wie 'Stadtbibliothek', 'Unibibliothek' oder 'Ruprecht-Karls-Universität'. Der Name der Stadt soll nicht erneut vorkommen!")
data['title'] = getInput(default="Stadtbibliothek")
print("Lade Geodaten...")
try:
with open(os.path.join(os.path.dirname(__file__), API_KEY_FILE)) as f:
api_key = f.read().strip()
except:
print(f'Keine Google Maps API key Datei ({API_KEY_FILE}) gefunden, verwende OSM Nominatim.')
api_key = None
geo = loadGeoPossibilities(data, api_key)
for k, g in enumerate(geo):
print("[%d] %s" % (k + 1, g[0]))
print("Welche dieser Positionen trifft am besten zu? 0 für keine.")
print("Nummer", end=" ")
geokey = int(getInput(default="0"))
if geokey > 0:
data['geo'] = geo[geokey - 1][1]
print("Welche API-Implementierung wird genutzt?")
print("Verfügbar sind: " + " ".join(sorted(APIS.keys())))
data['api'] = ''
while data['api'] not in APIS.keys():
data['api'] = getInput(required=True)
print("URL zum OPAC")
print("Ohne abschließenden /")
data['data'] = {}
data['data']['baseurl'] = getInput(required=True)
print("URL zu einer Informationsseite")
print("Sollte Öffnungszeiten u.ä. enthalten")
data['information'] = getInput(required=True)
api = APIS[data['api']]()
data = api.prompt(data)
print("Konto unterstützt?")
inp = getInput(required=False, default='nein' if not api.accountSupported() else 'ja')
if inp.lower() in ("ja", "yes", "y", "j", "true", "1"):
data['account_supported'] = True
else:
data['account_supported'] = False
ok = False;
while not ok:
print("Dateiname")
print("Sowas wie 'Mannheim' oder 'Heidelberg_Uni'. Möglichst keine Leerzeichen und Umlaute.")
ident = getInput(required=True)
if os.path.isfile(LIBDIR + ident + '.json'):
print("ACHTUNG: Datei existiert bereits. Überschreiben? (j/n)");
value = getInput(required=True, default="n")
if value == "j":
ok = True;
else:
ok = True;
print(json.dumps(data, indent=4, sort_keys=True), end="\n\n")
with open(LIBDIR + ident + '.json', 'w') as fp:
json.dump(data, fp, sort_keys=True, indent=4)
fp.write("\n")
print("In Datei %s geschrieben." % (LIBDIR + ident + '.json'))