To determine the IAB category for an IP address using IP2Location.io in D, you can use the IP2Location.io API to retrieve geolocation and related data. For more info, please see the IAB categories.
import std.stdio;
import std.uri;
import std.net.curl;
import std.json;
import std.conv;
void main()
{
auto key = "YOUR_API_KEY";
auto ip = "8.8.8.8";
ushort code;
auto reason = "";
auto url = "https://api.ip2location.io/?format=json&key=" ~ key ~ "&ip=" ~ ip;
string content = "";
auto client = HTTP(url); // using "HTTP" instead of just "get" due to the need to read the page content when HTTP Status <> 200
client.onReceive = (ubyte[] data) {
content = content ~ to!string(cast(char[])(data));
return data.length;
};
client.onReceiveStatusLine = (HTTP.StatusLine status) {
code = status.code;
reason = status.reason;
};
client.perform();
if (code == 200)
{
JSONValue result = parseJSON(content);
if ("ads_category" in result)
{
writefln("The IAB category for IP %s is %s.", ip, result["ads_category"].str);
}
else
{
writeln("ERROR: The IAB category field requires a paid subscription to the Security plan.");
}
}
else if ((code == 400) || (code == 401))
{
JSONValue result = parseJSON(content);
if ("error" in result)
{
writefln("ERROR: %s", result["error"]["error_message"].str);
}
else
{
writefln("ERROR: %s", reason);
}
}
else
{
writeln("ERROR: Unable to call API.");
}
}
dmd test.d
./test
This script will output the IAB category of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free