require 'geoip'
print(GeoIP.Get("8.8.8.8").country_name) -- "United States"
The return value may not always be a string so account for that.
Here's the FUGLY(TM) code for those who want to see it:
local files=file.Find('datacraft/*.*')
print("Found",#files,"files")
data={}
local a=SysTime()
for _,thefile in pairs(files) do
local content = file.Read('datacraft/'..thefile)
if content then
local decoded = glon.decode(content)
if decoded then
data[decoded.unique_id or decoded.id]=decoded
end
end
end
local b=SysTime()
print("Decoding files took ",(b-a),"seconds")
local a=SysTime()
require'geoip'
local get=GeoIP.Get
results={}
local i=0
for plid,usr in pairs(data) do
i=i+1
local ip=usr.ip
local steamid=usr.id
local name=usr.name
local iploc=get(ip)
iploc.name=name or "???"
iploc.steamid=steamid or "???"
iploc.ip=ip or "???"
results[plid]=results[plid] and debug.Trace() or iploc
end
local b=SysTime()
print("Geolocating",i,"ips took",(b-a),"seconds")
Msg("Writing kml file...")
local tbl={[[<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>places.kml</name>
<Folder>
<name>Server Players</name>
<open>1</open>
]]}
local i=2
for plid,info in pairs(results) do
if info.latitude then -- useless without
tbl[i]=
[[
<Placemark>
<name>]]..info.ip..[[</name>
<description>]].."<![CDATA["..
"<h1>"..info.steamid.."</h1><p>"..info.name.."</p>\n]]>"..
[[</description>
<Point>
<coordinates>]]..info.longitude..","..info.latitude..[[,0</coordinates>
</Point>
</Placemark>
]]
i=i+1
end
end
tbl[i]=[[
</Folder>
</Document>
</kml>
]]
local kml=table.concat(tbl,"\n")
file.Write("locations.txt",kml)
print"done!"
Word of warning: it does not escape cdata yet so malicious injection may occur. You need to write your own way to get the user data. This code is generally an example how to not do things, but I did anyway since I wanted to just see the end result.