-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKpuScraper.java
156 lines (128 loc) · 4.5 KB
/
KpuScraper.java
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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
/**
* Work with this bash command:
*
* cd ~/workspace/pilkada
* for i in $(cat list_kelurahan.txt);
* do
* echo "REGION=$i" >> pilkada_gabung_ringkas2.txt;
* cat "pilkada2017.kpu.go.id/hasil/t1/$i" |grep "rekapHasil.push([0-9]*)"|tr -d '\040\011' >> pilkada_gabung_ringkas2.txt;
* echo "PEMILIH" >> pilkada_gabung_ringkas2.txt;
* cat "pilkada2017.kpu.go.id/hasil/t1/$i" | grep " <td>[0-9][0-9]*</td>" | tr -d '\040\011' >> pilkada_gabung_ringkas2.txt;
* echo "REGION_DONE" >> pilkada_gabung_ringkas2.txt
* done
*
* Return a txt (csv actually) files with province, regencies, districts, village, tps number, votes (from 1 to 10), voter, vote user.
*
* @author: Keenan Mandela Gebze
* @version: 1.0.0
*
*/
public class KpuScraper {
public static void main( String[] args ) throws IOException {
if(args.length < 1){
System.err.println("Usage:\n KpuScraper2 [-k] file");
return;
}
String file = "";
boolean rekapKelurahan = false;
if(args.length == 1){
file = args[0];
}else if(args.length > 1){
if(args[0].equals("-k")){
rekapKelurahan = true;
}
file = args[args.length-1];
}
BufferedReader br = new BufferedReader(new FileReader(file));
String result = "";
String line = "";
String line2 = "";
ArrayList<TpsData> hehe = new ArrayList<>();
int tpsCounter = 0;
int[] votes = new int[10];
int[] voters = new int[2];
TpsData td = null;
while( (line = br.readLine()) != null) {
if( line.contains("REGION=") ){
String url = line.split("=")[1];
String loc[] = url.replace("_", " ").split("/");
int len = loc.length;
String provinsi = loc[len-4];
String kabupaten = loc[len-3];
String kecamatan = loc[len-2];
String kelurahan = loc[len-1];
tpsCounter = 0;
hehe.clear();
while ( (line2 = br.readLine()) != null){
if( ! "PEMILIH".equals(line2) ){
votes[0] = Integer.parseInt((line2.split("\\(")[1].split("\\)")[0]));
votes[1] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[2] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[3] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[4] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[5] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[6] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[7] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[8] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
votes[9] = Integer.parseInt(br.readLine().split("\\(")[1].split("\\)")[0]);
tpsCounter++;
td = new TpsData(10);
td.setLokasi(provinsi, kabupaten, kecamatan, kelurahan, tpsCounter);
td.setVotes(votes[0], votes[1], votes[2], votes[3], votes[4], votes[5], votes[6], votes[7], votes[8], votes[9]);
hehe.add(td);
}else{
break;
}
}
for(int i=0; i<tpsCounter-1; i++){
TpsData td1 = hehe.get(i);
String a = "";
String b = "";
try {
a = br.readLine();
a = a.split(">")[1];
b = br.readLine();
b = b.split(">")[1];
a = a.split("<")[0];
b = b.split("<")[0];
}catch(ArrayIndexOutOfBoundsException e){
System.err.println(a);
System.err.println(b);
System.err.println(e);
System.err.println(td1);
a="0";
b="0";
System.exit(1);
}
//System.err.println(a);
//System.err.println(b);
//mungkin regexnya tak cuma \\. melainkan semua non-digit..
if(a.contains(".")){
a = a.replaceAll("\\.","");
}
voters[0] = Integer.parseInt(a);
if(b.contains(".")){
b = b.replaceAll("\\.","");
}
voters[1] = Integer.parseInt(b);
//System.out.printf("%d, %d", voters[0], voters[1]);
td1.setPemilih(voters[0]);
td1.setPenggunaHakPilih(voters[1]);
System.out.println(td1.toString().replace(", ", ",").trim());
}
br.readLine(); //region_done
}
}
}
}