]>
Commit | Line | Data |
---|---|---|
d3e7a9fb MT |
1 | #!/usr/bin/python |
2 | ||
3 | import geoip2.database | |
4 | import geoip2.errors | |
5 | import os.path | |
6 | ||
7 | from . import base | |
8 | ||
9 | from .constants import DATADIR | |
10 | ||
11 | class GeoIP(base.Object): | |
12 | def init(self): | |
13 | path = os.path.join(DATADIR, "geoip/GeoLite2-Country.mmdb") | |
14 | ||
15 | # Open the database | |
16 | self.db = geoip2.database.Reader(path) | |
17 | ||
18 | def guess_from_address(self, address): | |
19 | # Query the database | |
20 | try: | |
21 | result = self.db.country(address) | |
22 | ||
23 | # Return nothing if the address could not be found | |
24 | except geoip2.errors.AddressNotFoundError: | |
25 | return | |
26 | ||
27 | if result: | |
28 | return result.country.iso_code |