Skip to content

Commit

Permalink
new geo ip resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 12, 2016
1 parent 03cc56d commit c1508a8
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/netius/common/geo.py
Expand Up @@ -84,11 +84,29 @@ def _get_db(cls):
if cls._db: return cls._db
try: import maxminddb
except: return None
if not os.path.exists(cls.DB_NAME): cls._download_db()
if not os.path.exists(cls.DB_NAME): return None
cls._db = maxminddb.open_database(cls.DB_NAME)
path = cls._try_all()
if not path: return None
cls._db = maxminddb.open_database(path)
return cls._db

@classmethod
def _try_all(cls):
path = cls._try_db(path = cls.DB_NAME)
if path: return path
path = cls._try_db(path = "~/" + cls.DB_NAME)
if path: return path
path = cls._try_db(path = "/" + cls.DB_NAME)
if path: return path
return None

@classmethod
def _try_db(cls, path = DB_NAME):
path = os.path.expanduser(path)
path = os.path.normpath(path)
if not os.path.exists(path): cls._download_db(path = path)
if not os.path.exists(path): return None
return path

@classmethod
def _download_db(cls, path = DB_NAME):
result = netius.clients.HTTPClient.method_s(
Expand All @@ -114,3 +132,6 @@ def _store_db(cls, contents, path = DB_NAME):
finally: file.close()
os.remove(path_gz)
return path

if __name__ == "__main__":
GeoResolver._try_db(path = "~/" + GeoResolver.DB_NAME)

0 comments on commit c1508a8

Please sign in to comment.