3 API Integrated solution
ClearAll[IP2Location];
Options[IP2Location] = {Method -> Automatic};
SetAttributes[IP2Location, Listable]
IP2Location[ip_String, OptionsPattern[]] :=
Block[{url, response, methodlist, method, latlon},
methodlist = {"nekudo", "freegeoip", "ip-api"};
method = If[
OptionValue[Method] === Automatic || FreeQ[methodlist, OptionValue[Method]],
RandomChoice[methodlist],
OptionValue[Method]
];
url = Switch[method,
"nekudo", URLBuild[<|"Scheme" -> "http", "Domain" -> "geoip.nekudo.com", "Path" -> {"api", ip}|>],
"freegeoip", URLBuild[<|"Scheme" -> "http", "Domain" -> "freegeoip.net", "Path" -> {"json", ip}|>],
"ip-api", URLBuild[<|"Scheme" -> "http", "Domain" -> "ip-api.com", "Path" -> {"json", ip}|>]
];
response = Import[url, "RawJSON"];
latlon = Values[
Switch[method,
"nekudo", Query["location", {"latitude", "longitude"}, ToExpression],
"freegeoip", Query[{"latitude", "longitude"}, ToExpression],
"ip-api", Query[{"lat", "lon"}, ToExpression]
][response]
];
(*Echo[response, method];*)
GeoLocation[latlon]
]
Performance
First@AbsoluteTiming@IP2Location[iplist]
(*37.6867*)
Failed in 7 sites.
API ip-api.com
IP2Location1[ip_String] := Block[
{
response = Import[
URLBuild[
<|
"Scheme" -> "http",
"Domain" -> "ip-api.com",
"Path" -> {"json", ip}
|>
], "RawJSON"]
},
GeoLocation@Values[Query[{"lat", "lon"}, ToExpression][response]]
]
SetAttributes[IP2Location1, Listable]
AbsoluteTiming@
IP2Location1@{"206.123.112.238", "151.101.1.69", "216.58.198.206",
"220.181.57.217", "207.123.33.126", "202.32.9.55",
"194.187.168.99", "134.171.75.1"}
(* {0.539081, {GeoLocation[{38.6143, -90.4444}],
GeoLocation[{37.7697, -122.393}], GeoLocation[{37.4192, -122.057}],
GeoLocation[{39.9289, 116.388}], GeoLocation[{52.52, 13.405}],
GeoLocation[{35.6427, 139.768}], GeoLocation[{48.8573, 2.32965}],
GeoLocation[{48.25, 11.65}]}} *)
API freegeoip.net
IP2Location2[ip_String] := Block[
{
response = Import[
URLBuild[
<|
"Scheme" -> "http",
"Domain" -> "freegeoip.net",
"Path" -> {"json", ip}
|>
], "RawJSON"]
},
GeoLocation@
Values[Query[{"latitude", "longitude"}, ToExpression][response]]
]
SetAttributes[IP2Location2, Listable]
AbsoluteTiming@
IP2Location2@{"206.123.112.238", "151.101.1.69", "216.58.198.206",
"220.181.57.217", "207.123.33.126", "202.32.9.55",
"194.187.168.99", "134.171.75.1"}
(* {1.32305, {GeoLocation[{28.5383, -81.3792}],
GeoLocation[{37.7697, -122.393}], GeoLocation[{37.4192, -122.057}],
GeoLocation[{39.9289, 116.388}], GeoLocation[{37.751, -97.822}],
GeoLocation[{35.6427, 139.768}], GeoLocation[{48.8582, 2.3387}],
GeoLocation[{48.25, 11.65}]}} *)
API geoip.nekudo.com
IP2Location3[ip_String] := Block[
{
response = Import[
URLBuild[
<|
"Scheme" -> "http",
"Domain" -> "geoip.nekudo.com",
"Path" -> {"api", ip}
|>
], "RawJSON"]
},
GeoLocation@
Values[Query["location", {"latitude", "longitude"}, ToExpression][
response]]
]
SetAttributes[IP2Location3, Listable]
AbsoluteTiming@
IP2Location3@{"206.123.112.238", "151.101.1.69", "216.58.198.206",
"220.181.57.217", "207.123.33.126", "202.32.9.55",
"194.187.168.99", "134.171.75.1"}
(* {0.644036, {GeoLocation[{28.5383, -81.3792}],
GeoLocation[{37.7697, -122.393}], GeoLocation[{37.4192, -122.057}],
GeoLocation[{39.9289, 116.388}], GeoLocation[{37.751, -97.822}],
GeoLocation[{35.6427, 139.768}], GeoLocation[{48.8582, 2.3387}],
GeoLocation[{48.25, 11.65}]}} *)