-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeile_geoip.py
More file actions
74 lines (58 loc) · 2.3 KB
/
Copy pathmeile_geoip.py
File metadata and controls
74 lines (58 loc) · 2.3 KB
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
#!/usr/bin/env python3
import pymysql
import json
import geoip2.database
import scrtsxx
charset ='utf8mb4'
VERSION = 20240710.0126
def connDB():
db = pymysql.connect(host=scrtsxx.HOST,
port=scrtsxx.PORT,
user=scrtsxx.USERNAME,
passwd=scrtsxx.PASSWORD,
db=scrtsxx.DB,
charset=charset,
cursorclass=pymysql.cursors.DictCursor)
return db
def getMeilePing(db):
cursorObject = db.cursor()
query = "SELECT * from meileping"
cursorObject.execute(query)
return cursorObject.fetchall()
def ScrubIPsFromMeileDB(db):
scrub_query = 'UPDATE meileping SET ip = NULL;'
c = db.cursor()
c.execute(scrub_query)
db.commit()
def UpdateMeileGEOIP(MeilePingDB, db):
GEOIPDict = {'uuid' : None, 'country' : None, 'region' : None, 'city' : None, 'latitude' : None, 'longitude' : None}
for row in MeilePingDB:
print(row)
uuid = row['uuid']
ip = row['ip']
if ip:
with geoip2.database.Reader('GeoLite2-City.mmdb') as reader:
response = reader.city(ip)
GEOIPDict['uuid'] = uuid
GEOIPDict['country'] = response.country.name
GEOIPDict['region'] = response.subdivisions.most_specific.name
GEOIPDict['city'] = response.city.name
GEOIPDict['latitude'] = response.location.latitude
GEOIPDict['longitude'] = response.location.longitude
query = '''INSERT IGNORE INTO meile_geoip (uuid, country, region, city, latitude, longitude)
VALUES ("%s", "%s", "%s", "%s", "%s", "%s")
''' % (GEOIPDict['uuid'],
GEOIPDict['country'],
GEOIPDict['region'],
GEOIPDict['city'],
GEOIPDict['latitude'],
GEOIPDict['longitude'])
print(query)
cursor = db.cursor()
cursor.execute(query)
db.commit()
if __name__ == "__main__":
db = connDB()
MeilePingDB = getMeilePing(db)
UpdateMeileGEOIP(MeilePingDB, db)
ScrubIPsFromMeileDB(db)