-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoLocation.cs
63 lines (57 loc) · 2.05 KB
/
GeoLocation.cs
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
using System;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ComputerAnalytics
{
public class GeoLocationQueryResponse
{
public string status { get; set; } = "";
public string country { get; set; } = "";
public string countryCode { get; set; } = "";
public string region { get; set; } = "";
public string regionName { get; set; } = "";
public string city { get; set; } = "";
public string zip { get; set; } = "";
public double lat { get; set; } = 0.0;
public double lon { get; set; } = 0.0;
public string timezone { get; set; } = "";
public string isp { get; set; } = "";
public string org { get; set; } = "";
[JsonPropertyName("as")]
public string _as { get; set; } = "";
public string query { get; set; } = "";
}
public class AnonymisedGeoLocationQueryResponse
{
public string countryCode { get; set; } = "";
public string timezone { get; set; } = "";
public static explicit operator AnonymisedGeoLocationQueryResponse(GeoLocationQueryResponse v)
{
AnonymisedGeoLocationQueryResponse g = new AnonymisedGeoLocationQueryResponse();
g.countryCode = v.countryCode.ToLower();
g.timezone = v.timezone;
return g;
}
}
public class GeoLocationClient
{
public static GeoLocationQueryResponse GetGeoLocation(string ip)
{
WebClient c = new WebClient();
string glqr = c.DownloadString("http://ip-api.com/json/" + ip);
try
{
GeoLocationQueryResponse geo = JsonSerializer.Deserialize<GeoLocationQueryResponse>(glqr);
return geo;
} catch
{
return null;
}
}
public static AnonymisedGeoLocationQueryResponse GetAnonymisedGeoLocation(string ip)
{
return (AnonymisedGeoLocationQueryResponse)GetGeoLocation(ip.Split(',')[0]);
}
}
}