-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLoader.cs
109 lines (90 loc) · 3.84 KB
/
EventLoader.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
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using HtmlAgilityPack;
namespace FIRST
{
public delegate void ProgressEventHandler(object sender, UploadProgressChangedEventArgs e);
public delegate void EventsUpdatedHandler(object sender, EventArgs e);
class EventLoader
{
public event ProgressEventHandler DownloadProgressChanged;
public event EventsUpdatedHandler EventsUpdated;
public string[] EventTypes = new string[] { "Championship",
"Regional",
"MI FRC State Championship",
"MI District"};
public static string LassoLocation = "https://my.usfirst.org/myarea/index.lasso";
public List<Event> events;
private WebClient client;
private NameValueCollection values;
public EventLoader()
{
events = new List<Event>();
client = new WebClient();
values = new NameValueCollection();
values.Add("page", "searchresults");
values.Add("skip_events", "0");
values.Add("skip_teams", "1");
values.Add("programs", "FRC");
values.Add("season_FRC", "2011");
values.Add("reports", "events");
values.Add("area", "ALL");
values.Add("results_size", "250");
values.Add("year", "2011");
client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
client.UploadValuesCompleted += new UploadValuesCompletedEventHandler(client_UploadValuesCompleted);
}
void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
if (DownloadProgressChanged != null)
DownloadProgressChanged(this, e);
}
void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
{
ParseResults(e.Result);
if (EventsUpdated != null)
EventsUpdated(this, new EventArgs());
}
public void GetEvents(int year)
{
values["year"] = year.ToString();
client.UploadValuesAsync(new Uri(LassoLocation), values);
}
public void ParseResults(byte[] response)
{
string responseFromServer = "";
responseFromServer = Encoding.ASCII.GetString(response);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(responseFromServer);
HtmlNode node = doc.DocumentNode;
events = new List<Event>();
foreach (HtmlNode row in node.GetNodesByClass("tr"))
{
HtmlNode temp = row.FirstChildWithClass("td");
if (temp != null)
{
if (EventTypes.Contains(temp.InnerText))
{
Event e = new Event();
List<HtmlNode> descriptions = row.GetNodesByClass("td").ToList();
HtmlNode linker = descriptions[1].FirstChildWithClass("a");
string link = linker.Attributes["href"].Value;
e.Add("Type", descriptions[0].InnerText);
e.Add("Name", linker.InnerText);
e.Add("Link", link);
e.Add("Event Venue", descriptions[2].InnerText);
e.Add("Location", descriptions[3].InnerText);
e.Add("Date", descriptions[4].InnerText.Trim());
events.Add(e);
}
}
}
}
}
}