Last active
May 19, 2019 06:44
-
-
Save edwin/48d7b5ebc141e826aa24c680fa3fcc05 to your computer and use it in GitHub Desktop.
Bot untuk download hasil scan formulir c1 pilpres 2019 milik KPU (pemilu2019.kpu.go.id)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.edw.situng; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.cert.CertificateException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* | |
* @author Muhammad Edwin < edwinkun at gmail dot com > | |
* 19 May 2019 12:54 | |
*/ | |
public class Main2 { | |
private String kdProv = "14086"; | |
private String kdKab = "14630"; | |
private String kdKec = "930145"; | |
private String kdKel = "14676"; | |
private String kdTps = "900075432"; | |
public static void main(String[] args) throws Exception { | |
new Main2().crawlAndDoCapture(); | |
} | |
public Main2(){ | |
} | |
/** | |
* lets rock and roll | |
* | |
* @throws Exception | |
*/ | |
private void crawlAndDoCapture() throws Exception { | |
// count time needed | |
Long start = System.currentTimeMillis(); | |
// crawl images name and location | |
String url = String.format("https://pemilu2019.kpu.go.id/static/json/hhcw/ppwp/%s/%s/%s/%s/%s.json", | |
kdProv, | |
kdKab, | |
kdKec, | |
kdKel, | |
kdTps); | |
OkHttpClient client = getOkHttpClient(); | |
Request request = new Request.Builder() | |
.url(url) | |
.build(); | |
Response response = client.newCall(request).execute(); | |
Map<String, Object> jsonResponse = new ObjectMapper().readValue(response.body().string(), Map.class); | |
// get c1 lembar 1 | |
String c1ImageName = (String)((List) jsonResponse.get("images")).get(0); | |
// fetch and download c1 forms | |
String imageUrl = String.format("https://pemilu2019.kpu.go.id/img/c/%s/%s/%s/%s", | |
kdTps.substring(0, 3), | |
kdTps.substring(3, 6), | |
kdTps, | |
c1ImageName); | |
String fileName = String.format("%s.%s.c1.png", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), kdTps); | |
request = new Request.Builder() | |
.url(imageUrl) | |
.build(); | |
response = client.newCall(request).execute(); | |
Files.copy(response.body().byteStream(), Paths.get("E:\\tmp\\"+fileName)); | |
// end | |
Long end = System.currentTimeMillis(); | |
System.out.println("Time needed "+(end-start)+"ms"); | |
} | |
/** | |
* get instance of okhttp | |
* | |
* @return instance of OKHttpClients | |
* @throws NoSuchAlgorithmException | |
* @throws KeyManagementException | |
*/ | |
private OkHttpClient getOkHttpClient() throws NoSuchAlgorithmException, KeyManagementException { | |
OkHttpClient client = new OkHttpClient(); | |
client.setSslSocketFactory(getSslContext().getSocketFactory()); | |
client.setConnectTimeout(200, TimeUnit.SECONDS); | |
client.setReadTimeout(200, TimeUnit.SECONDS); | |
return client; | |
} | |
/** | |
* override kpu SSL issue | |
* | |
* @return instance of SSLContext | |
* @throws NoSuchAlgorithmException | |
* @throws KeyManagementException | |
*/ | |
private SSLContext getSslContext() throws NoSuchAlgorithmException, KeyManagementException { | |
TrustManager[] trustAllCerts = new TrustManager[]{ | |
new X509TrustManager() { | |
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new java.security.cert.X509Certificate[]{}; | |
} | |
} | |
}; | |
SSLContext sslContext = SSLContext.getInstance("SSL"); | |
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | |
return sslContext; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.edw.situng</groupId> | |
<artifactId>Situng Capturer</artifactId> | |
<version>1.0</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-java</artifactId> | |
<version>3.141.59</version> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>htmlunit-driver</artifactId> | |
<version>2.33.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-firefox-driver</artifactId> | |
<version>3.141.59</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>1.3.2</version> | |
</dependency> | |
<dependency> | |
<groupId>com.squareup.okhttp</groupId> | |
<artifactId>okhttp</artifactId> | |
<version>2.7.5</version> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>2.9.9</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment