Getting information about an IP address from PB
Posted: Mon Oct 02, 2023 6:11 pm
Hi,
I wrote a PB library to quickly query information about an IP address. It gives you things such as the country, city, region, zip code, ISP, and time zone of the specified IP.
For usage, see the bottom of the file.
Enjoy, and feel free to leave any feedback (I'm still not entirely sure if I should try to return a populated structure). I think not but haven't been using PB for a super long time, so am still second guessing myself.
I wrote a PB library to quickly query information about an IP address. It gives you things such as the country, city, region, zip code, ISP, and time zone of the specified IP.
For usage, see the bottom of the file.
Code: Select all
; IPLocale Library.
; Written by Quin on Monday, October 2, 2023.
EnableExplicit
;{ Structures
Structure IPLocaleInfo
Query.s
Country.s
Region.s
City.s
ZipCode.s
Latitude.f
Longitude.f
Timezone.s
ISP.s
EndStructure
;}
;{ Declares
Declare GetIPLocale(IPAddress.s, *Info.IPLocaleInfo)
;}
;{ Macros
Macro JString(Value)
GetJSONString(GetJSONMember(JSONValue(0), Value))
EndMacro
Macro JFloat(Value)
GetJSONFloat(GetJSONMember(JSONValue(0), Value))
EndMacro
;}
;{ Main
Procedure GetIPLocale(IPAddress.s, *Info.IPLocaleInfo)
Protected Request = HTTPRequest(#PB_HTTP_Get, "http://ip-api.com/json/" + IPAddress)
If Request = 0
ProcedureReturn 1
EndIf
Protected Response.s = HTTPInfo(Request, #PB_HTTP_Response)
FinishHTTP(Request)
If ParseJSON(0, Response) = 0
ProcedureReturn 1
EndIf
Protected Status.s = JString("status")
If Status = "fail"
FreeJSON(0)
ProcedureReturn 1
EndIf
With *Info
\Query = JString("query")
\Country = JString("country")
\Region = JString("regionName")
\City = JString("city")
\ZipCode = JString("zip")
\Latitude = JFloat("lat")
\Longitude = JFloat("lon")
\Timezone = JString("timezone")
\ISP = RTrim(JString("isp"), ".")
EndWith
FreeJSON(0)
ProcedureReturn 0
EndProcedure
;}
;{ Demo
CompilerIf #PB_Compiler_IsMainFile
Define Info.IPLocaleInfo
GetIPLocale("76.25.0.4", @Info)
With Info
Debug \City
Debug \Country
Debug \ISP
Debug \Latitude
Debug \Longitude
Debug \Query
Debug \Region
Debug \Timezone
Debug \ZipCode
EndWith
CompilerEndIf
;}