diff --git a/install/install.sql b/install/install.sql index 397e9857..eb8ad9a4 100644 --- a/install/install.sql +++ b/install/install.sql @@ -725,10 +725,11 @@ CREATE TABLE IF NOT EXISTS `v3_stats` ( -- see tools/generate-ip-mappings.py for generation -- CREATE TABLE IF NOT EXISTS `v3_ipv4_mapping` ( - `ip_start` INT UNSIGNED NOT NULL, - `ip_end` INT UNSIGNED NOT NULL, - `latitude` FLOAT NOT NULL DEFAULT '0.0', - `longitude` FLOAT NOT NULL DEFAULT '0.0', + `ip_start` INT UNSIGNED NOT NULL, + `ip_end` INT UNSIGNED NOT NULL, + `latitude` FLOAT NOT NULL DEFAULT '0.0', + `longitude` FLOAT NOT NULL DEFAULT '0.0', + `country_code` VARCHAR(2) NOT NULL DEFAULT '', PRIMARY KEY (`ip_start`), UNIQUE KEY `ip_start` (`ip_start`), UNIQUE KEY `ip_end` (`ip_end`) diff --git a/tools/generate-ip-mappings.py b/tools/generate-ip-mappings.py index 22d64c10..870b701e 100755 --- a/tools/generate-ip-mappings.py +++ b/tools/generate-ip-mappings.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # Download from https://dev.maxmind.com/geoip/geoip2/geolite2/ -# You only need GeoLite2-City-Blocks-IPv4.csv from GeoLite2 City +# You need GeoLite2-City-Blocks-IPv4.csv and GeoLite2-City-Locations-en.csv from GeoLite2 City # license of the DB is CC-BY-SA 4.0 # # This product includes GeoLite2 data created by MaxMind, available from @@ -25,7 +25,19 @@ CSV_WEB_LINK = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip' CSV_FILE = 'GeoLite2-City-Blocks-IPv4.csv' +CSV_LOCATION = 'GeoLite2-City-Locations-en.csv' +if not os.path.exists(CSV_LOCATION): + print("File = {} does not exist. Download it from = {} ".format(CSV_FILE, CSV_WEB_LINK)) + sys.exit(1) + +COUNTRY_DICT = {} +with open(CSV_LOCATION, 'r') as csvfile: + locationlist = csv.reader(csvfile, delimiter=',', quotechar='"') + # Skip header + next(locationlist) + for row in locationlist: + COUNTRY_DICT[row[0]] = row[4] if not os.path.exists(CSV_FILE): print("File = {} does not exist. Download it from = {} ".format(CSV_FILE, CSV_WEB_LINK)) @@ -47,4 +59,5 @@ latitude = float(row[7]) longitude = float(row[8]) - print('%d,%d,%f,%f' % (ip_start, ip_end, latitude, longitude)) + country = COUNTRY_DICT.get(row[1], "") + print('%d,%d,%f,%f,%s' % (ip_start, ip_end, latitude, longitude, country))