-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_geoDb_search.py
70 lines (49 loc) · 1.73 KB
/
test_geoDb_search.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
import ipaddress, pathlib
import lib.shared.storage
import lib.server.data
def makeGeoObj(geoData):
geoObj = {"ip": geoData['ip'], "country_code2": geoData['country_code'], "country_name": geoData['country_name'], "isp": geoData['isp']}
return geoObj
storage = lib.shared.storage.Server()
storage.init_certificate()
geoData = lib.server.data.IPGeolocation()
storage.init_geolocation()
geoNewDir = storage.saveDirs['geoDb'].joinpath('geoNew')
geoNewDir.mkdir(exist_ok = True)
geoOld = lib.shared.storage.Geolocation(storage.certificate, storage.saveDirs['geoDb'])
geoOld.database = geoOld.load_database()
geoNew = lib.shared.storage.Geolocation(storage.certificate, geoNewDir)
geoNew.index.touch(exist_ok = True)
geoNew.addrs.touch(exist_ok = True)
#geoData.FILES = storage.geolocation
#geoData.loadDatabase()
print(f"old database: {len(geoOld.database)} entries")
print("starting copy")
errors = []
counter = 0
for key in geoOld.database:
try:
data = geoOld.get_value(geoOld.database[key])
geoNew.set_value(makeGeoObj(data))
counter += 1
except KeyError:
errors.append((counter, key.hex()))
print(f"Found {len(errors)} errors")
geoNew.database = geoNew.load_database()
print(f"new database: {len(geoNew.database)}")
print("loading data handler")
dataHandler = lib.server.data.IPGeolocation()
dataHandler.FILES = geoNew
dataHandler.loadDatabase()
print("Geolocation DB search Test")
while True:
text = str(input("search >> ")).lower()
if text == 'quit': break
results = dataHandler.search(text)
print(f"Found: {len(results)} results")
if input("press 's' to show results >> ") == "s":
for x in results:
print(x)
else:
print()
print("closing")