Skip to main content
added 23 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152

3 API Integrated solution (One API call per IP)

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}]}} *)

3 API Integrated solution

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}]}} *)

3 API Integrated solution (One API call per IP)

added 23 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
ClearAll[IP2Location];
Options[IP2Location] = {Method -> Automatic};
SetAttributes[IP2Location, Listable]
IP2Location[ip_String | IPAddress[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 = URLBuild@Switch[method,
     "nekudo", <|"Scheme" -> "http", "Domain" -> "geoip.nekudo.com", "Path" -> {"api", ip}|>,
     "freegeoip", <|"Scheme" -> "http", "Domain" -> "freegeoip.net", "Path" -> {"json", ip}|>,
     "ip-api", <|"Scheme" -> "http", "Domain" -> "ip-api.com", "Path" -> {"json", ip}|>
     ];
  response = Import[url, "RawJSON"];
  latlon = ToExpression@Values[
     Switch[method,
       "nekudo", Query["location", {"latitude", "longitude"}],
       "freegeoip", Query[{"latitude", "longitude"}],
       "ip-api", Query[{"lat", "lon"}]
       ][response]
     ];
  (*Echo[response,method];*)
  GeoLocation[latlon]
  ]
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 = URLBuild@Switch[method,
     "nekudo", <|"Scheme" -> "http", "Domain" -> "geoip.nekudo.com", "Path" -> {"api", ip}|>,
     "freegeoip", <|"Scheme" -> "http", "Domain" -> "freegeoip.net", "Path" -> {"json", ip}|>,
     "ip-api", <|"Scheme" -> "http", "Domain" -> "ip-api.com", "Path" -> {"json", ip}|>
     ];
  response = Import[url, "RawJSON"];
  latlon = ToExpression@Values[
     Switch[method,
       "nekudo", Query["location", {"latitude", "longitude"}],
       "freegeoip", Query[{"latitude", "longitude"}],
       "ip-api", Query[{"lat", "lon"}]
       ][response]
     ];
  (*Echo[response,method];*)
  GeoLocation[latlon]
  ]
ClearAll[IP2Location];
Options[IP2Location] = {Method -> Automatic};
SetAttributes[IP2Location, Listable]
IP2Location[ip_String | IPAddress[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 = URLBuild@Switch[method,
     "nekudo", <|"Scheme" -> "http", "Domain" -> "geoip.nekudo.com", "Path" -> {"api", ip}|>,
     "freegeoip", <|"Scheme" -> "http", "Domain" -> "freegeoip.net", "Path" -> {"json", ip}|>,
     "ip-api", <|"Scheme" -> "http", "Domain" -> "ip-api.com", "Path" -> {"json", ip}|>
     ];
  response = Import[url, "RawJSON"];
  latlon = ToExpression@Values[
     Switch[method,
       "nekudo", Query["location", {"latitude", "longitude"}],
       "freegeoip", Query[{"latitude", "longitude"}],
       "ip-api", Query[{"lat", "lon"}]
       ][response]
     ];
  (*Echo[response,method];*)
  GeoLocation[latlon]
  ]
added 215 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152

Do not apply this method to long lists, where Batch mode would be more efficient and less abusive to the API provider. See this other answer.

Do not apply this method to long lists, where Batch mode would be more efficient and less abusive to the API provider. See this other answer.

deleted 67 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 23 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 42 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 6 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 105 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 1321 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 1321 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 111 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 895 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
added 976 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152
Loading