Skip to content

Commit 19ce2a9

Browse files
Added support for IP2Location Web Service
1 parent 3f153be commit 19ce2a9

File tree

5 files changed

+427
-24
lines changed

5 files changed

+427
-24
lines changed

README.md

Lines changed: 187 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
IP2Location Lua Package
2-
======================
1+
# IP2Location Lua Package
32

43
This Lua package provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category from IP address by using IP2Location database. This package uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category as values. It supports both IP address in IPv4 and IPv6.
54

@@ -17,16 +16,64 @@ The database will be updated in monthly basis for the greater accuracy. Free LIT
1716

1817
The paid databases are available at https://www.ip2location.com under Premium subscription package.
1918

19+
As an alternative, this library can also call the IP2Location Web Service. This requires an API key. If you don't have an existing API key, you can subscribe for one at the below:
2020

21-
Installation
22-
=======
21+
https://www.ip2location.com/web-service/ip2location
22+
23+
## Installation
2324

2425
```
2526
luarocks install ip2location
2627
```
2728

28-
Example
29-
=======
29+
## QUERY USING THE BIN FILE
30+
31+
## Dependencies
32+
33+
This package requires IP2Location BIN data file to function. You may download the BIN data file at
34+
* IP2Location LITE BIN Data (Free): https://lite.ip2location.com
35+
* IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
36+
37+
38+
## IPv4 BIN vs IPv6 BIN
39+
40+
Use the IPv4 BIN file if you just need to query IPv4 addresses.
41+
42+
Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
43+
44+
45+
## Methods
46+
47+
Below are the methods supported in this package.
48+
49+
|Method Name|Description|
50+
|---|---|
51+
|get_all|Returns the geolocation information in an object.|
52+
|get_country_short|Returns the country code.|
53+
|get_country_long|Returns the country name.|
54+
|get_region|Returns the region name.|
55+
|get_city|Returns the city name.|
56+
|get_isp|Returns the ISP name.|
57+
|get_latitude|Returns the latitude.|
58+
|get_longitude|Returns the longitude.|
59+
|get_domain|Returns the domain name.|
60+
|get_zipcode|Returns the ZIP code.|
61+
|get_timezone|Returns the time zone.|
62+
|get_netspeed|Returns the net speed.|
63+
|get_iddcode|Returns the IDD code.|
64+
|get_areacode|Returns the area code.|
65+
|get_weatherstationcode|Returns the weather station code.|
66+
|get_weatherstationname|Returns the weather station name.|
67+
|get_mcc|Returns the mobile country code.|
68+
|get_mnc|Returns the mobile network code.|
69+
|get_mobilebrand|Returns the mobile brand.|
70+
|get_elevation|Returns the elevation in meters.|
71+
|get_usagetype|Returns the usage type.|
72+
|get_addresstype|Returns the address type.|
73+
|get_category|Returns the IAB category.|
74+
|close|Closes BIN file and resets metadata.|
75+
76+
## Usage
3077

3178
```lua
3279
ip2location = require('ip2location')
@@ -62,20 +109,141 @@ ip2loc:close()
62109

63110
```
64111

65-
Dependencies
66-
============
67-
68-
The complete database is available at https://www.ip2location.com under subscription package.
69-
112+
## QUERY USING THE IP2LOCATION WEB SERVICE
70113

71-
IPv4 BIN vs IPv6 BIN
72-
====================
114+
## Methods
115+
Below are the methods supported in this package.
73116

74-
Use the IPv4 BIN file if you just need to query IPv4 addresses.
75-
Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
117+
|Method Name|Description|
118+
|---|---|
119+
|open| 3 input parameters:<ol><li>IP2Location API Key.</li><li>Package (WS1 - WS25)</li></li><li>Use HTTPS or HTTP</li></ol> |
120+
|lookup|Query IP address. This method returns an object containing the geolocation info. <ul><li>country_code</li><li>country_name</li><li>region_name</li><li>city_name</li><li>latitude</li><li>longitude</li><li>zip_code</li><li>time_zone</li><li>isp</li><li>domain</li><li>net_speed</li><li>idd_code</li><li>area_code</li><li>weather_station_code</li><li>weather_station_name</li><li>mcc</li><li>mnc</li><li>mobile_brand</li><li>elevation</li><li>usage_type</li><li>address_type</li><li>category</li><li>continent<ul><li>name</li><li>code</li><li>hemisphere</li><li>translations</li></ul></li><li>country<ul><li>name</li><li>alpha3_code</li><li>numeric_code</li><li>demonym</li><li>flag</li><li>capital</li><li>total_area</li><li>population</li><li>currency<ul><li>code</li><li>name</li><li>symbol</li></ul></li><li>language<ul><li>code</li><li>name</li></ul></li><li>idd_code</li><li>tld</li><li>is_eu</li><li>translations</li></ul></li><li>region<ul><li>name</li><li>code</li><li>translations</li></ul></li><li>city<ul><li>name</li><li>translations</li></ul></li><li>geotargeting<ul><li>metro</li></ul></li><li>country_groupings</li><li>time_zone_info<ul><li>olson</li><li>current_time</li><li>gmt_offset</li><li>is_dst</li><li>sunrise</li><li>sunset</li></ul></li><ul>|
121+
|get_credit|This method returns the web service credit balance in an object.|
76122

123+
## Usage
77124

78-
Copyright
79-
=========
80-
81-
Copyright (C) 2021 by IP2Location.com, support@ip2location.com
125+
```lua
126+
ip2locationwebservice = require('ip2locationwebservice')
127+
128+
local apikey = 'YOUR_API_KEY'
129+
local apipackage = 'WS25'
130+
local usessl = true
131+
local lang = 'fr' -- leave blank if no need
132+
local addon = 'continent,country,region,city,geotargeting,country_groupings,time_zone_info' -- leave blank if no need
133+
134+
local ip = '8.8.8.8'
135+
local ws = ip2locationwebservice:open(apikey, apipackage, usessl)
136+
137+
local result = ws:lookup(ip, addon, lang)
138+
139+
if result["response"] == nil then
140+
print("Error: Unknown error.")
141+
elseif result.response == "OK" then
142+
-- standard results
143+
print("response: " .. result.response)
144+
print("country_code: " .. result.country_code)
145+
print("country_name: " .. result.country_name)
146+
print("region_name: " .. result.region_name)
147+
print("city_name: " .. result.city_name)
148+
print("latitude: " .. result.latitude)
149+
print("longitude: " .. result.longitude)
150+
print("zip_code: " .. result.zip_code)
151+
print("time_zone: " .. result.time_zone)
152+
print("isp: " .. result.isp)
153+
print("domain: " .. result.domain)
154+
print("net_speed: " .. result.net_speed)
155+
print("idd_code: " .. result.idd_code)
156+
print("area_code: " .. result.area_code)
157+
print("weather_station_code: " .. result.weather_station_code)
158+
print("weather_station_name: " .. result.weather_station_name)
159+
print("mcc: " .. result.mcc)
160+
print("mnc: " .. result.mnc)
161+
print("mobile_brand: " .. result.mobile_brand)
162+
print("elevation: " .. result.elevation)
163+
print("usage_type: " .. result.usage_type)
164+
print("address_type: " .. result.address_type)
165+
print("category: " .. result.category)
166+
print("category_name: " .. result.category_name)
167+
print("credits_consumed: " .. result.credits_consumed)
168+
169+
-- continent addon
170+
if result["continent"] ~= nil then
171+
print("continent => name: " .. result.continent.name)
172+
print("continent => code: " .. result.continent.code)
173+
print("continent => hemisphere: " .. table.concat(result.continent.hemisphere,","))
174+
if lang ~= '' and result.continent.translations[lang] ~= nil then
175+
print("continent => translations => " .. lang .. ": " .. result.continent.translations[lang])
176+
end
177+
end
178+
179+
-- country addon
180+
if result["country"] ~= nil then
181+
print("country => name: " .. result.country.name)
182+
print("country => alpha3_code: " .. result.country.alpha3_code)
183+
print("country => numeric_code: " .. result.country.numeric_code)
184+
print("country => demonym: " .. result.country.demonym)
185+
print("country => flag: " .. result.country.flag)
186+
print("country => capital: " .. result.country.capital)
187+
print("country => total_area: " .. result.country.total_area)
188+
print("country => population: " .. result.country.population)
189+
print("country => idd_code: " .. result.country.idd_code)
190+
print("country => tld: " .. result.country.tld)
191+
print("country => is_eu: " .. tostring(result.country.is_eu))
192+
if lang ~= '' and result.country.translations[lang] ~= nil then
193+
print("country => translations => " .. lang .. ": " .. result.country.translations[lang])
194+
end
195+
196+
print("country => currency => code: " .. result.country.currency.code)
197+
print("country => currency => name: " .. result.country.currency.name)
198+
print("country => currency => symbol: " .. result.country.currency.symbol)
199+
200+
print("country => language => code: " .. result.country.language.code)
201+
print("country => language => name: " .. result.country.language.name)
202+
end
203+
204+
-- region addon
205+
if result["region"] ~= nil then
206+
print("region => name: " .. result.region.name)
207+
print("region => code: " .. result.region.code)
208+
if lang ~= '' and result.region.translations[lang] ~= nil then
209+
print("region => translations => " .. lang .. ": " .. result.region.translations[lang])
210+
end
211+
end
212+
213+
-- city addon
214+
if result["city"] ~= nil then
215+
print("city => name: " .. result.city.name)
216+
if lang ~= '' and result.city.translations[lang] ~= nil then
217+
print("city => translations => " .. lang .. ": " .. result.city.translations[lang])
218+
end
219+
end
220+
221+
-- geotargeting addon
222+
if result["geotargeting"] ~= nil then
223+
print("geotargeting => metro: " .. result.geotargeting.metro)
224+
end
225+
226+
-- country_groupings addon
227+
if result["country_groupings"] ~= nil then
228+
for i,v in ipairs(result.country_groupings) do
229+
print("country_groupings => #" .. i .. " => acronym: " .. result.country_groupings[i].acronym)
230+
print("country_groupings => #" .. i .. " => name: " .. result.country_groupings[i].name)
231+
end
232+
end
233+
234+
-- time_zone_info addon
235+
if result["time_zone_info"] ~= nil then
236+
print("time_zone_info => olson: " .. result.time_zone_info.olson)
237+
print("time_zone_info => current_time: " .. result.time_zone_info.current_time)
238+
print("time_zone_info => gmt_offset: " .. result.time_zone_info.gmt_offset)
239+
print("time_zone_info => is_dst: " .. result.time_zone_info.is_dst)
240+
print("time_zone_info => sunrise: " .. result.time_zone_info.sunrise)
241+
print("time_zone_info => sunset: " .. result.time_zone_info.sunset)
242+
end
243+
244+
local result2 = ws:get_credit()
245+
print("Credit Balance: " .. result2.response)
246+
else
247+
print("Error: " .. result.response)
248+
end
249+
```
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "ip2location"
2-
version = "8.4.0-1"
2+
version = "8.5.0-1"
33
source = {
44
url = "git://github.com/ip2location/ip2location-lua.git"
55
}
@@ -21,11 +21,15 @@ description = {
2121
}
2222
dependencies = {
2323
"lua >= 5.3",
24-
"lua-nums"
24+
"lua-nums",
25+
"json-lua",
26+
"luasocket",
27+
"urlencode"
2528
}
2629
build = {
2730
type = "builtin",
2831
modules = {
29-
["ip2location"] = "ip2location.lua"
32+
["ip2location"] = "ip2location.lua",
33+
["ip2locationwebservice"] = "ip2locationwebservice.lua"
3034
}
3135
}

ip2location.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ local usagetype_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
121121
local addresstype_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21}
122122
local category_position = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22}
123123

124-
local api_version = "8.4.0"
124+
local api_version = "8.5.0"
125125

126126
local modes = {
127127
countryshort = 0x000001,
@@ -441,7 +441,6 @@ function ip2location:checkip(ip)
441441
-- print(key, " -- " , value);
442442
-- end
443443

444-
-- only support full IPv6 format for now
445444
if #chunks == 8 then
446445
local ipnum = bn.ZERO
447446
local part = 0

ip2locationwebservice.lua

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
local http = require("socket.http")
2+
local ltn12 = require("ltn12")
3+
local json = require("JSON")
4+
local urlencode = require("urlencode")
5+
6+
-- for debugging purposes
7+
local function printme(stuff)
8+
local inspect = require('inspect')
9+
print(inspect(stuff))
10+
end
11+
12+
ip2locationwebservice = {
13+
apikey = "",
14+
apipackage = "",
15+
usessl = false
16+
}
17+
ip2locationwebservice.__index = ip2locationwebservice
18+
19+
ip2locationresult = {
20+
response = '',
21+
country_code = '',
22+
country_name = '',
23+
region_name = '',
24+
city_name = '',
25+
latitude = 0,
26+
longitude = 0,
27+
zip_code = '',
28+
time_zone = '',
29+
isp = '',
30+
domain = '',
31+
net_speed = '',
32+
idd_code = '',
33+
area_code = '',
34+
weather_station_code = '',
35+
weather_station_name = '',
36+
mcc = '',
37+
mnc = '',
38+
mobile_brand = '',
39+
elevation = 0,
40+
usage_type = '',
41+
address_type = '',
42+
category = '',
43+
category_name = '',
44+
geotargeting = nil,
45+
continent = nil,
46+
country = nil,
47+
country_groupings = nil,
48+
region = nil,
49+
city = nil,
50+
time_zone_info = nil,
51+
credits_consumed = 0
52+
}
53+
ip2locationresult.__index = ip2locationresult
54+
55+
-- initialize the component with the web service configuration
56+
function ip2locationwebservice:open(apikey, apipackage, usessl)
57+
local x = {}
58+
setmetatable(x, ip2locationwebservice) -- make ip2locationwebservice handle lookup
59+
60+
x.apikey = apikey
61+
x.apipackage = apipackage
62+
x.usessl = usessl
63+
64+
return x
65+
end
66+
67+
-- main query
68+
function ip2locationwebservice:lookup(ipaddress, addon, lang)
69+
local protocol = "http"
70+
if self.usessl then
71+
protocol = "https"
72+
end
73+
74+
local t = {}
75+
76+
local status, code, headers = http.request {
77+
method = "GET",
78+
url = protocol .. "://api.ip2location.com/v2/?key=" .. urlencode.encode_url(self.apikey) .. "&package=" .. urlencode.encode_url(self.apipackage) .. "&ip=" .. urlencode.encode_url(ipaddress) .. "&addon=" .. urlencode.encode_url(addon) .. "&lang=" .. urlencode.encode_url(lang),
79+
sink = ltn12.sink.table(t)
80+
}
81+
local jsonstr = table.concat(t)
82+
local result = json:decode(jsonstr)
83+
setmetatable(result, ip2locationresult)
84+
85+
return result
86+
end
87+
88+
-- check web service credit balance
89+
function ip2locationwebservice:get_credit()
90+
local protocol = "http"
91+
if self.usessl then
92+
protocol = "https"
93+
end
94+
95+
local t = {}
96+
97+
local status, code, headers = http.request {
98+
method = "GET",
99+
url = protocol .. "://api.ip2location.com/v2/?key=" .. urlencode.encode_url(self.apikey) .. "&check=true",
100+
sink = ltn12.sink.table(t)
101+
}
102+
local jsonstr = table.concat(t)
103+
local result = json:decode(jsonstr)
104+
setmetatable(result, ip2locationresult)
105+
106+
return result
107+
end
108+
109+
return ip2locationwebservice

0 commit comments

Comments
 (0)