-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
191 lines (170 loc) · 7.41 KB
/
Program.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
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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DGE_scraper
{
class Program
{
private class Candidate
{
public string Name;
public string Party;
public int Votes;
}
private class Riding
{
public string Name;
public int RejectedVotes;
public int RegisteredVoters;
public IDictionary<string, Candidate> Candidates;
}
private class PartyComparer : IComparer<string>
{
public int Compare(string a, string b)
{
if (a == string.Empty || b == string.Empty)
{
// empty strings come last, not first.
return - a.CompareTo(b);
}
if (a.StartsWith("IND"))
{
return b.StartsWith("IND") ? a.CompareTo(b) : 1;
}
if (b.StartsWith("IND"))
{
return -1;
}
return a.CompareTo(b);
}
}
static void Main()
{
var ridings = GetRidings().ToArray();
var partyNames = ridings.SelectMany(r => r.Candidates.Keys)
.Distinct()
.OrderBy(p => p, new PartyComparer())
.ToArray();
WriteVoteCsv(ridings, partyNames);
WriteCandidatesCsv(ridings, partyNames);
}
// Taken from http://monvote.qc.ca/fr/resultatsPreliminaires.asp
private static readonly int [] ridingIds = new []{579, 573, 437, 373, 535, 323, 309, 293, 153, 303, 353, 525, 473, 713, 243, 431, 377, 129, 193, 593, 559, 619, 679, 173, 613, 763, 441, 659, 683, 433, 403, 483, 273, 759, 745, 443, 731, 557, 381, 133, 481, 387, 561, 149, 143, 733, 409, 623, 429, 643, 269, 361, 773, 603, 203, 183, 545, 779, 371, 209, 363, 423, 439, 583, 663, 289, 653, 399, 213, 407, 349, 465, 711, 103, 383, 451, 495, 233, 673, 419, 411, 329, 401, 123, 421, 553, 369, 563, 599, 753, 367, 253, 283, 703, 699, 413, 783, 379, 515, 567, 389, 447, 111, 393, 263, 189, 505, 417, 343, 177, 113, 163, 223, 633, 453, 333, 793, 229, 639, 169, 249, 397, 427, 449, 391};
private static IEnumerable<Riding> GetRidings()
{
var client = new WebClient();
foreach (var i in ridingIds)
{
using (var stream = client.OpenRead(string.Format("http://monvote.qc.ca/data/resultats/{0}.js", i)))
{
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
yield return ParseRiding(content);
}
}
}
}
}
private static readonly Regex ridingRegex = new Regex("^var Circ = \"(.+)\"", RegexOptions.Compiled|RegexOptions.Multiline);
private static readonly Regex rejectedVotesRegex = new Regex("^var VotesRejTot = \"([0-9\\s]+)\"", RegexOptions.Compiled|RegexOptions.Multiline);
private static readonly Regex registeredVotersRegex = new Regex("^var NbElectInscr = \"([0-9\\s]+)\"", RegexOptions.Compiled|RegexOptions.Multiline);
private static Riding ParseRiding(string str)
{
return new Riding
{
Name = GetString(ridingRegex.Match(str)),
RejectedVotes = GetInt(rejectedVotesRegex.Match(str)),
RegisteredVoters = GetInt(registeredVotersRegex.Match(str)),
Candidates = GetCandidateTable(str)
};
}
private static string GetString(Match match)
{
return match.Success ? match.Groups[1].Value : string.Empty;
}
private static int GetInt(Match match)
{
return match.Success ? GetIntFromGroup(match.Groups[1]) : -1;
}
private static int GetIntFromGroup(Group group)
{
return int.Parse(string.Join(string.Empty, Regex.Replace(group.Value, @"\s", " ").Split(' ')));
}
private static readonly Regex candidateTableRegex = new Regex("^\\[\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"\\]", RegexOptions.Compiled|RegexOptions.Multiline);
private static IDictionary<string, Candidate> GetCandidateTable(string input)
{
var matches = candidateTableRegex.Matches(input);
var indIdx = 1;
return matches.Cast<Match>()
.Select(
match =>
new Candidate
{
Name = string.Join(" ", match.Groups[1].Value.Split(',').Reverse()).Trim(),
Party = match.Groups[2].Value != "IND" ? match.Groups[2].Value : "IND" + (indIdx++),
Votes = GetIntFromGroup(match.Groups[3])
})
.ToDictionary(c => c.Party, c => c);
}
private static void WriteVoteCsv(IEnumerable<Riding> ridings, IEnumerable<string> parties)
{
using(var stream = new StreamWriter("..\\..\\qc2012_resultats.csv",false,Encoding.UTF8))
{
// Write header
stream.Write("Circonscription,Voteurs enregistrés,Votes rejetés");
foreach (var party in parties)
{
stream.Write("," + party);
}
stream.WriteLine();
foreach (var riding in ridings)
{
stream.Write(riding.Name);
stream.Write("," + riding.RegisteredVoters);
stream.Write("," + riding.RejectedVotes);
foreach (var party in parties)
{
stream.Write(",");
if (riding.Candidates.ContainsKey(party))
{
stream.Write(riding.Candidates[party].Votes);
}
}
stream.WriteLine();
}
}
}
private static void WriteCandidatesCsv(IEnumerable<Riding> ridings, IEnumerable<string> parties)
{
using (var stream = new StreamWriter("..\\..\\qc2012_candidats.csv", false, Encoding.UTF8))
{
// Write header
stream.Write("Circonscription");
foreach (var party in parties)
{
stream.Write("," + party);
}
stream.WriteLine();
foreach (var riding in ridings)
{
stream.Write(riding.Name);
foreach (var party in parties)
{
stream.Write(",");
if (riding.Candidates.ContainsKey(party))
{
stream.Write(riding.Candidates[party].Name);
}
}
stream.WriteLine();
}
}
}
}
}