-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (56 loc) · 1.87 KB
/
main.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 os
from lookups import geo_lookup
from lookups import rdap_lookup
from extract import extract
from tqdm import tqdm
from utils import utils
import redis
import json
def main(file_name="ips.txt"):
'''
Summary line.
Main function to check if the ip's data is already cached or not, if not, fetch it
Parameters
----------
file_name : str
This is the filename of the text file.
Returns
-------
None
Description of return value
'''
# fetch current working directory
current_dir = os.getcwd()
# joins together string absolute path with string filename
file_name = os.path.join(current_dir, file_name)
# set up redis connection
redis_client = redis.Redis(host='localhost', port=6379, db=0)
# extract ip from the text
ips = extract.extract(file_name)
# get the length of ips
length_of_list = len(ips)
# iterates through each ip address, gathering geo and rdap information for each ip address.
# Also shows the progress of the database being populated.
for i in tqdm(range(length_of_list)):
# check if the ip is already cached
ip = ips[i]
isGeoCached = redis_client.get(ip + "geo")
isRDAPCached = redis_client.get(ip + "rdap")
# if the ip is already cached, print it out
# otherwise fetch it
if isGeoCached is not None:
print(isGeoCached)
else:
geo_info = geo_lookup.geo_lookup(ip)
redis_client.mset({ip + "geo": json.dumps(geo_info)})
print(geo_info)
if isRDAPCached is not None:
print(isRDAPCached)
else:
rdap_info = rdap_lookup.rdap_lookup(ip)
redis_client.mset({ip + "rdap": json.dumps(rdap_info)})
print(rdap_info)
# execute main program.
if __name__ == "__main__":
args = utils.input_args()
main(args.filename)