Skip to content

Instantly share code, notes, and snippets.

@lhaagsma
Last active May 31, 2019 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhaagsma/c5d211c4746734d93ca3da8e1610cf8d to your computer and use it in GitHub Desktop.
Save lhaagsma/c5d211c4746734d93ca3da8e1610cf8d to your computer and use it in GitHub Desktop.
Lazy download maxmind mmdb
#!/usr/bin/env python
import os
import requests
import StringIO
import tarfile
try:
from geoip2.database import Reader
GEO = True
except:
GEO = False
GEO_DIR = 'resources'
GEO_F_TYPE = 'mmdb'
GEO_URL = 'http://geolite.maxmind.com/download/geoip/database/{name}.tar.gz'
def download_db(fname):
targz = requests.get(GEO_URL.format(name=fname)).content
fobj = StringIO.StringIO(targz)
fh = tarfile.open(fileobj=fobj, mode="r:gz")
for member in fh:
if member.name.endswith(GEO_F_TYPE):
return fh.extractfile(member).read()
if __name__ == '__main__':
if not os.path.exists(GEO_DIR):
os.mkdir(GEO_DIR)
for db in ['GeoLite2-ASN', 'GeoLite2-City']:
geo_file = '{}.{}'.format(db, GEO_F_TYPE)
geo_path = os.path.join(GEO_DIR, geo_file)
if not os.path.exists(geo_path):
open(geo_path, 'wb').write(download_db(db))
if GEO:
Reader(geo_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment