Skip to content

Commit

Permalink
Added getCountry to pygeoip, ircbot uses pygeoip lib
Browse files Browse the repository at this point in the history
  • Loading branch information
greghaynes committed Mar 21, 2010
1 parent 706fa5e commit d16d863
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/pyscripts/plugins/GeoIp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
from xsbs.players import player

import string
import pygeoip

db = pygeoip.GeoIP('pygeoip/GeoIP.dat')
from pygeoip import getCountry

conf = PluginConfig('geoip')
template = '${green}${user}${white} connected from ${orange}${country}'
template = conf.getOption('Config', 'template', template)
del conf

def getCountry(ip):
country = db.country_name_by_addr(ipLongToString(ip))
if country == '':
country = 'Unknown'
return country

@eventHandler('player_connect_delayed')
def announce(cn):
Expand Down
4 changes: 2 additions & 2 deletions src/pyscripts/plugins/ircbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sbserver

from xsbs.players import player
import GeoIp
from pygeoip import getCountry
import string

config = {
Expand Down Expand Up @@ -141,7 +141,7 @@ def dotemplate(ability, **args):
factory.broadcast(string.Template(config['Templates'][ability]).substitute(irccolordict, **args))

event_abilities = {
'player_connect': ('player_connect', lambda x: dotemplate('player_connect', name=sbserver.playerName(x), cn=x, country=GeoIp.getCountry(player(x).ipLong()))),
'player_connect': ('player_connect', lambda x: dotemplate('player_connect', name=sbserver.playerName(x), cn=x, country=getCountry(player(x).ipLong()))),
'player_disconnect': ('player_disconnect', lambda x: dotemplate('player_disconnect', name=sbserver.playerName(x), cn=x)),
'message': ('player_message', lambda x, y: dotemplate('message', name=sbserver.playerName(x), cn=x, message=y)),
'map_change': ('map_changed', lambda x, y: dotemplate('map_change', map=x, mode=sbserver.modeName(y))),
Expand Down
Binary file added src/pyscripts/pygeoip/GeoIP.dat
Binary file not shown.
10 changes: 9 additions & 1 deletion src/pyscripts/pygeoip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,12 @@ def region_by_name(self, hostname):
"""
addr = socket.gethostbyname(hostname)
return self.region_by_addr(addr)


db = pygeoip.GeoIP('pygeoip/GeoIP.dat')

def getCountry(ip):
country = db.country_name_by_addr(ipLongToString(ip))
if country == '':
country = 'Unknown'
return country

8 changes: 8 additions & 0 deletions src/pyscripts/pygeoip/geoip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
db = pygeoip.GeoIP('pygeoip/GeoIP.dat')

def getCountry(ip):
country = db.country_name_by_addr(ipLongToString(ip))
if country == '':
country = 'Unknown'
return country

0 comments on commit d16d863

Please sign in to comment.