-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocaleData.cs
142 lines (126 loc) · 4.44 KB
/
LocaleData.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
using System.Globalization;
namespace MonitorPhotoApp
{
public class LocaleData
{
public string _City { get; private set; }
public string _Country { get; private set; }
public string _Lat { get; private set; }
public string _Lon { get; private set; }
public string _WeatherDescription { get; private set; }
public string _Humidity { get; private set; }
public string _TempFeelsLike { get; private set; }
public string _Temp { get; private set; }
public string _TempMax { get; private set; }
public string _TempMin { get; private set; }
public string _flagIconUrl { get; private set; }
public int _Timezone { get; private set; }
public string _CountryDisplayName { get; private set; }
public string _Currency { get; private set; }
public string _CurrencySymbol { get; private set; }
public string _localTime { get; private set; }
public string _IP { get; private set; }
public void FillWeatherProperties()
{
GetWeather(GetLocation(GetIPAddress()));
}
public bool FillWeatherProperties(string ip)
{
return GetWeather(GetLocation(ip));
}
private bool GetWeather(string city)
{
if (city == null) return false;
//Assign API KEY which received from OPENWEATHERMAP.ORG
string appId = "68224e5e5f7d7f5cddbe9f980e9f164e";
//API path with CITY parameter and appId.
Log.AddToLog("Retrieving weather data..");
string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&cnt=1&APPID={1}", city, appId);
try
{
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
//Converting to OBJECT from JSON string.
RootObject weatherInfo = (new JavaScriptSerializer()).Deserialize<RootObject>(json);
// Populate properties
this._Country = weatherInfo.Sys.Country;
RegionInfo countryProperties = new RegionInfo(this._Country);
this._CountryDisplayName = countryProperties.DisplayName;
this._Currency = countryProperties.CurrencyEnglishName;
this._CurrencySymbol = countryProperties.CurrencySymbol;
this._flagIconUrl = string.Format("http://openweathermap.org/images/flags/{0}.png", this._Country.ToLower());
this._City = weatherInfo.Name;
this._Lat = Convert.ToString(weatherInfo.Coord.Lat);
this._Lon = Convert.ToString(weatherInfo.Coord.Lon);
this._WeatherDescription = weatherInfo.Weather[0].Description;
this._Humidity = Convert.ToString(weatherInfo.Main.Humidity);
this._Temp = Convert.ToString(weatherInfo.Main.Temp);
this._TempFeelsLike = Convert.ToString(weatherInfo.Main.Feels_like);
this._TempMax = Convert.ToString(weatherInfo.Main.Temp_max);
this._TempMin = Convert.ToString(weatherInfo.Main.Temp_min);
this._Timezone = weatherInfo.Timezone;
}
Log.AddToLog("Done");
return true;
}
catch (Exception)
{
return false;
}
}
private string GetIPAddress()
{
Log.AddToLog("Retrievning ip..");
String address = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
address = stream.ReadToEnd();
}
// Extract IP manually from string
int first = address.IndexOf("Address: ") + 9;
int last = address.LastIndexOf("</body>");
address = address.Substring(first, last - first);
// Store IP
this._IP = address;
Log.AddToLog("Ip = " + address);
return address;
}
private string GetLocation(string ip)
{
Log.AddToLog("Retrievning Location..");
var res = "";
try
{
WebRequest request = WebRequest.Create("http://ipinfo.io/" + ip);
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = stream.ReadLine()) != null)
{
res += line;
}
}
string city = (new JavaScriptSerializer()).Deserialize<Location>(res).City;
Log.AddToLog("Location = " + city);
return city;
}
catch (Exception e)
{
Log.AddToLog("Error when retrieving location: " + e.Message);
return "";
}
}
internal string getLocalTime()
{
return DateTime.UtcNow.AddSeconds(_Timezone).ToString("HH:mm:ss");
}
}
}