]> git.ipfire.org Git - people/jschlag/pbs.git/blame - src/buildservice/geoip.py
Refactor Bugzilla URL generation
[people/jschlag/pbs.git] / src / buildservice / geoip.py
CommitLineData
d3e7a9fb
MT
1#!/usr/bin/python
2
3import geoip2.database
4import geoip2.errors
5import os.path
6
7from . import base
8
9from .constants import DATADIR
10
11class 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