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