Last active
November 11, 2022 12:07
-
-
Save Kiougar/859dd23887bbac436fb036fd4b398f4a to your computer and use it in GitHub Desktop.
Downloads and extracts `GeoLite2-Country` and `GeoLite2-City` `mmdb` database files to the `./data` directory.
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
| import io | |
| import os | |
| import tarfile | |
| from urllib.request import urlopen | |
| def get_url(name: str): | |
| key = os.getenv('GEOLITE_LICENSE_KEY', '') | |
| assert key, 'GEOLITE_LICENSE_KEY is missing' | |
| return f'https://download.maxmind.com/app/geoip_download?edition_id={name}&license_key={key}&suffix=tar.gz' | |
| if __name__ == '__main__': | |
| for n in ['GeoLite2-Country', 'GeoLite2-City']: | |
| print(f'Downloading {n}...', end=' ', flush=True) | |
| with urlopen(get_url(n)) as response: | |
| with tarfile.open(mode='r:gz', fileobj=io.BytesIO(response.read())) as tar: | |
| for member in tar.getmembers(): | |
| if member.name.endswith(f'{n}.mmdb'): | |
| member.name = os.path.basename(member.name) | |
| tar.extract(member, path='data') | |
| print('Done!', flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment