Skip to content

Commit

Permalink
Add 2-letter country code to ip mapping table
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed May 8, 2019
1 parent 45b2d86 commit 3051f7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions install/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
17 changes: 15 additions & 2 deletions tools/generate-ip-mappings.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand All @@ -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))

0 comments on commit 3051f7d

Please sign in to comment.