Repository navigation
Add MaxMind GeoLite Country crawler #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
m-appel
merged 3 commits into
InternetHealthReport:main
from
m-appel:add-maxmind-crawler
Jul 15, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # MaxMind -- https://www.maxmind.com/ | ||
|
|
||
| MaxMind is an IP geolocation service, that provides different kinds of IP | ||
|
m-appel marked this conversation as resolved.
Outdated
|
||
| databases, including a [free | ||
| tier](https://www.maxmind.com/en/geolite-free-ip-geolocation-data) that maps IP | ||
| prefixes to countries. We import the free database into IYP. | ||
|
|
||
| ## Graph representation | ||
|
|
||
| A prefix can also be just a single IP, resulting in /32 or /128 prefixes, which | ||
| is intended. We also import the auxiliary country data provided by MaxMind as | ||
| relationship properties. | ||
|
|
||
| ```cypher | ||
| (pfx:GeoPrefix {prefix: '202.208.0.0/12'})-[:COUNTRY {continent_name: 'Asia', is_in_european_union: 0}]->(:Country {country_code: 'JP'}) | ||
| ``` | ||
|
|
||
| ## Dependence | ||
|
|
||
| This crawler is not depending on other crawlers. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import argparse | ||
| import hashlib | ||
| import json | ||
| import logging | ||
| import os | ||
| import sys | ||
| from io import BytesIO | ||
| from ipaddress import ip_network | ||
| from zipfile import ZipFile | ||
|
|
||
| import pandas as pd | ||
| import requests | ||
|
|
||
| from iyp import BaseCrawler, set_modification_time_from_last_modified_header | ||
|
|
||
| ORG = 'MaxMind' | ||
| URL = 'https://download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip' | ||
| NAME = 'maxmind.geolite_country' | ||
|
|
||
| SHA256_URL = 'https://download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip.sha256' | ||
|
|
||
| MAXMIND_ACCOUNT_ID = '' | ||
| MAXMIND_LICENSE_KEY = '' | ||
| if os.path.exists('config.json'): | ||
| MAXMIND_ACCOUNT_ID = json.load(open('config.json', 'r'))['maxmind']['account_id'] | ||
| MAXMIND_LICENSE_KEY = json.load(open('config.json', 'r'))['maxmind']['license_key'] | ||
|
|
||
|
|
||
| class Crawler(BaseCrawler): | ||
| def __init__(self, organization, url, name): | ||
| super().__init__(organization, url, name) | ||
| self.reference['reference_url_info'] = 'https://dev.maxmind.com/geoip/geolite2-free-geolocation-data/' | ||
|
|
||
| def run(self): | ||
| """Fetch data and push to IYP.""" | ||
| session = requests.Session() | ||
| session.auth = (MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY) | ||
|
|
||
| logging.info('Fetching files...') | ||
|
|
||
| hash_res = session.get(SHA256_URL) | ||
| if hash_res.status_code != 200: | ||
| logging.error('Failed to fetch SHA256 hash.') | ||
| hash_res.raise_for_status() | ||
| sha256_hash, expected_filename = hash_res.text.split() | ||
|
|
||
| db_res = session.get(URL) | ||
| if db_res.status_code != 200: | ||
| logging.error('Failed to fetch database.') | ||
| db_res.raise_for_status() | ||
|
|
||
| session.close() | ||
|
|
||
| sha256 = hashlib.file_digest(BytesIO(db_res.content), 'sha256').hexdigest() | ||
| filename = db_res.headers['Content-Disposition'].removeprefix('attachment; filename=') | ||
| if sha256 != sha256_hash or filename != expected_filename: | ||
| logging.error('Validation of SHA256 hash or filename failed:') | ||
| logging.error(f' SHA256: {sha256}') | ||
| logging.error(f' Expected: {sha256_hash}') | ||
| logging.error(f' Filename: {filename}') | ||
| logging.error(f' Expected: {expected_filename}') | ||
| raise ValueError('Validation of SHA256 hash or filename failed.') | ||
|
|
||
| set_modification_time_from_last_modified_header(self.reference, db_res) | ||
| logging.info(f'Downloaded file: {filename} Last-Modified: {self.reference["reference_time_modification"]}') | ||
|
|
||
| logging.info('Parsing data...') | ||
|
|
||
| with ZipFile(BytesIO(db_res.content)) as zf: | ||
| path_prefix = filename.removesuffix('.zip') | ||
| try: | ||
| v4_file = zf.getinfo(f'{path_prefix}/GeoLite2-Country-Blocks-IPv4.csv') | ||
| v6_file = zf.getinfo(f'{path_prefix}/GeoLite2-Country-Blocks-IPv6.csv') | ||
| geoname_file = zf.getinfo(f'{path_prefix}/GeoLite2-Country-Locations-en.csv') | ||
| except KeyError as e: | ||
| logging.error(f'Failed to extract data from ZIP file: {e}') | ||
| raise ValueError(f'Failed to extract data from ZIP file: {e}') | ||
|
|
||
| # The geoname file maps the geoname_id to the actual country data | ||
| # (e.g., country code). | ||
| # Do not parse NA country code as NaN field... | ||
| with zf.open(geoname_file) as f: | ||
| geoname_df = pd.read_csv(f, keep_default_na=False, na_values=['']) | ||
| # Locale is static "en", so not interesting. | ||
| geoname_df.pop('locale_code') | ||
| # Data contains Asia and Europe as locations with only a continent | ||
| # code, which we do not model. | ||
| geoname_df = geoname_df[geoname_df['country_iso_code'].notna()] | ||
|
|
||
| # Only load network and geoname_id columns. Remaining columns are | ||
| # either empty, deprecated, or we do not care about them: | ||
| # https://dev.maxmind.com/geoip/docs/databases/city-and-country/#blocks-files | ||
| with zf.open(v4_file) as f: | ||
| ipv4_df = pd.read_csv(f, usecols=(0, 1)) | ||
| with zf.open(v6_file) as f: | ||
| ipv6_df = pd.read_csv(f, usecols=(0, 1)) | ||
| ip_df = pd.concat([ipv4_df, ipv6_df]) | ||
| # Some blocks only have an entry for the registered country, which | ||
| # we already cover with the delegated stats file. | ||
| ip_df = ip_df[ip_df['geoname_id'].notna()] | ||
|
|
||
| # Join IP data with geoname data based on geoname_id field (which we do | ||
| # not need afterwards). | ||
| ip_merged_df = ip_df.merge(geoname_df) | ||
| ip_merged_df.pop('geoname_id') | ||
|
|
||
| countries = set(ip_merged_df['country_iso_code'].unique()) | ||
| prefixes = set() | ||
| links = list() | ||
|
|
||
| for r in ip_merged_df.itertuples(index=False): | ||
| prefix = ip_network(r.network).compressed | ||
| prefixes.add(prefix) | ||
| links.append( | ||
| { | ||
| 'src_id': prefix, | ||
| 'dst_id': r.country_iso_code, | ||
| 'props': [ | ||
| self.reference, | ||
| { | ||
| 'continent_code': r.continent_code, | ||
| 'continent_name': r.continent_name, | ||
| 'country_iso_code': r.country_iso_code, | ||
| 'country_name': r.country_name, | ||
| 'is_in_european_union': r.is_in_european_union, | ||
| } | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| logging.info('Pushing data...') | ||
|
|
||
| country_id = self.iyp.batch_get_nodes_by_single_prop('Country', 'country_code', countries, all=False) | ||
| prefix_id = self.iyp.batch_get_nodes_by_single_prop('GeoPrefix', 'prefix', prefixes, all=False) | ||
| self.iyp.batch_add_node_label(list(prefix_id.values()), 'Prefix') | ||
|
|
||
| for link in links: | ||
| link['src_id'] = prefix_id[link['src_id']] | ||
| link['dst_id'] = country_id[link['dst_id']] | ||
|
|
||
| self.iyp.batch_add_links('COUNTRY', links) | ||
|
|
||
| def unit_test(self): | ||
| return super().unit_test(['COUNTRY']) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--unit-test', action='store_true') | ||
| args = parser.parse_args() | ||
|
|
||
| FORMAT = '%(asctime)s %(levelname)s %(message)s' | ||
| logging.basicConfig( | ||
| format=FORMAT, | ||
| filename='log/' + NAME + '.log', | ||
| level=logging.INFO, | ||
| datefmt='%Y-%m-%d %H:%M:%S', | ||
| ) | ||
|
|
||
| logging.info(f'Started: {sys.argv}') | ||
|
|
||
| crawler = Crawler(ORG, URL, NAME) | ||
| if args.unit_test: | ||
| crawler.unit_test() | ||
| else: | ||
| crawler.run() | ||
| crawler.close() | ||
| logging.info(f'Finished: {sys.argv}') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| sys.exit(0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.