-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForecast.cs
More file actions
251 lines (224 loc) · 9.07 KB
/
Forecast.cs
File metadata and controls
251 lines (224 loc) · 9.07 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using HeadlessBrowser;
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace DesktopWeather
{
public partial class Forecast : Form
{
// private string forecastPage = "https://forecast.weather.gov/MapClick.php?x=264&y=129&site=lox&zmx=&zmy=&map_x=264&map_y=129";
private string imageBase = "https://forecast.weather.gov/";
private WebBroForm myWebBrowser;
private bool hadAforceStop;
private string browseStatus;
private WebBrowser returnedWebPage;
private string returnedAddr;
private ArrayList savedPeriods = new ArrayList();
private string forecastPage = "";
private weatherForm myParent;
public int imgCounter = 0;
private struct parsedPeriod
{
public string periodName;
public string weatherImage;
public string extendedDesc;
public string restOfData;
}
public Forecast(bool preLoad)
{
InitializeComponent();
}
public Forecast(string forecastPageRef, weatherForm myParentForm)
{
myParent = myParentForm;
InitializeComponent();
forecastPage = forecastPageRef;
SetupForecast();
}
public void SetupForecast()
{
try { loadForeCastPage(); }
catch
{
this.rtbForecast.Text = "Error Loading NOAA Web Page";
this.Refresh();
return;
}
}
private void loadForeCastPage()
{
myWebBrowser = new WebBroForm(this);
hadAforceStop = false;
tmrNOAAtimeout.Enabled = true;
try { myWebBrowser.myBrowser.Navigate(forecastPage); }
catch
{
this.rtbForecast.Text = "Error Loading NOAA Web Page";
this.Refresh();
}
}
internal void BrowserIsFinished()
{
tmrNOAAtimeout.Enabled = false;
browseStatus = myWebBrowser.CurrentStatus;
if (browseStatus != "Ready")
{
this.rtbForecast.Text = "Error Loading NOAA Web Page";
return;
}
returnedWebPage = myWebBrowser.myBrowser;
returnedAddr = myWebBrowser.myAddrBar.Text;
Application.DoEvents();
try { ParseValuesFrom(returnedWebPage.DocumentText.ToString()); }
catch
{
if (hadAforceStop)
{
this.rtbForecast.Text = "Error Retrieving NOAA Web Page";
return;
}
this.rtbForecast.Text = "Error Parsing NOAA Web Page";
return;
}
UpdateForecasts();
}
private void UpdateForecasts()
{
clearImageBorders();
string fullImageLoc;
var fileInfo = new FileInfo("DesktopWeather.exe");
parsedPeriod firstWeather = (parsedPeriod)savedPeriods[0];
label1.Text = firstWeather.periodName;
fullImageLoc = getLocalImage(firstWeather.weatherImage);
fileInfo = new FileInfo(fullImageLoc);
Application.DoEvents();
if (fileInfo.Length > 0) { pbToday.Image = Image.FromFile(fullImageLoc); }
pbToday.Tag = firstWeather.extendedDesc;
parsedPeriod secondWeather = (parsedPeriod)savedPeriods[1];
label2.Text = secondWeather.periodName;
fullImageLoc = getLocalImage(secondWeather.weatherImage);
fileInfo = new FileInfo(fullImageLoc);
Application.DoEvents();
if (fileInfo.Length > 0) { pbTonight.Image = Image.FromFile(fullImageLoc); }
pbTonight.Tag = secondWeather.extendedDesc;
parsedPeriod thirdWeather = (parsedPeriod)savedPeriods[2];
if (thirdWeather.periodName.Contains("Night"))
{ thirdWeather = (parsedPeriod)savedPeriods[3]; }
label3.Text = thirdWeather.periodName;
fullImageLoc = getLocalImage(thirdWeather.weatherImage);
fileInfo = new FileInfo(fullImageLoc);
Application.DoEvents();
if (fileInfo.Length > 0) { pbTomorrow.Image = Image.FromFile(fullImageLoc); }
pbTomorrow.Tag = thirdWeather.extendedDesc;
parsedPeriod fourthWeather = (parsedPeriod)savedPeriods[4];
if (fourthWeather.periodName.Contains("Night"))
{ fourthWeather = (parsedPeriod)savedPeriods[5]; }
label4.Text = fourthWeather.periodName;
fullImageLoc = getLocalImage(fourthWeather.weatherImage);
fileInfo = new FileInfo(fullImageLoc);
Application.DoEvents();
if (fileInfo.Length > 0) { pbNextDay.Image = Image.FromFile(fullImageLoc); }
pbNextDay.Tag = fourthWeather.extendedDesc;
rtbForecast.Text = firstWeather.extendedDesc;
pbToday.BorderStyle = BorderStyle.FixedSingle;
this.Refresh();
myParent.lastForecast = DateTime.Now;
Application.DoEvents();
}
private string getLocalImage(string weatherImage)
{
string imageFileName = "imageXX.png";
imgCounter++;
string fileToUse = imageFileName.Replace("XX", imgCounter.ToString());
string imageLocBase = imageBase + weatherImage;
string imageLoc = imageLocBase.Replace("&", "&");
using (WebClient client = new WebClient())
{
client.Headers.Add("User-Agent: Other");
client.DownloadFile(new Uri(imageLoc), fileToUse);
}
return fileToUse;
}
private void clearImageBorders()
{
pbToday.BorderStyle = BorderStyle.None;
pbTonight.BorderStyle = BorderStyle.None;
pbTomorrow.BorderStyle = BorderStyle.None;
pbNextDay.BorderStyle = BorderStyle.None;
}
private void ParseValuesFrom(string NoaaWeather)
{
string periodData = NoaaWeather;
for (int i = 0; i < 6; i++)
{
parsedPeriod nextPeriodStruct = GetNextPeriod(periodData);
savedPeriods.Add(nextPeriodStruct);
try { periodData = nextPeriodStruct.restOfData; }
catch (Exception ex)
{
MessageBox.Show("Error Parsing Period " + i.ToString());
File.WriteAllText("parsingError.txt", nextPeriodStruct.restOfData);
throw ex;
}
}
}
private static parsedPeriod GetNextPeriod(string periodData)
{
string findSeperator = "period-name>";
int nextSep = periodData.IndexOf(findSeperator);
string carryOn = periodData.Substring(nextSep + 12);
int endOfPeriod = carryOn.IndexOf("<");
string periodName = carryOn.Substring(0, endOfPeriod);
string findDesc = "IGM title=\"";
int nextDesc = carryOn.IndexOf(findDesc);
string carryOn2 = carryOn.Substring(nextDesc + 11);
int endOfDesc = carryOn2.IndexOf("\"");
string extendedDesc = carryOn2.Substring(0, endOfDesc);
string findImage = "src=\"";
int nextImg = carryOn2.IndexOf(findImage);
string carryOn3 = carryOn2.Substring(nextImg + 5);
int endOfImg = carryOn3.IndexOf("\"");
string weatherImage = carryOn3.Substring(0, endOfImg);
string restOfData = carryOn3;
parsedPeriod myPeriod = new parsedPeriod();
myPeriod.periodName = periodName;
myPeriod.weatherImage = weatherImage;
myPeriod.extendedDesc = extendedDesc;
myPeriod.restOfData = restOfData;
return myPeriod;
}
private void pbToday_Click(object sender, System.EventArgs e)
{
clearImageBorders();
pbToday.BorderStyle = BorderStyle.FixedSingle;
rtbForecast.Text = pbToday.Tag.ToString();
}
private void pbTonight_Click(object sender, System.EventArgs e)
{
clearImageBorders();
pbTonight.BorderStyle = BorderStyle.FixedSingle;
rtbForecast.Text = pbTonight.Tag.ToString();
}
private void pbTomorrow_Click(object sender, System.EventArgs e)
{
clearImageBorders();
pbTomorrow.BorderStyle = BorderStyle.FixedSingle;
rtbForecast.Text = pbTomorrow.Tag.ToString();
}
private void pbNextday_Click(object sender, System.EventArgs e)
{
clearImageBorders();
pbNextDay.BorderStyle = BorderStyle.FixedSingle;
rtbForecast.Text = pbNextDay.Tag.ToString();
}
private void tmrNOAAtimeout_Tick(object sender, EventArgs e)
{
tmrNOAAtimeout.Enabled = false;
hadAforceStop = true;
myWebBrowser.processAforceStop();
}
}
}