1 |
package dk.thoerup.spejdernetscraper;
|
2 |
|
3 |
import java.util.Map;
|
4 |
import java.util.TreeMap;
|
5 |
|
6 |
import org.jsoup.Connection.Method;
|
7 |
import org.jsoup.Connection.Response;
|
8 |
import org.jsoup.Jsoup;
|
9 |
import org.jsoup.nodes.Document;
|
10 |
import org.jsoup.nodes.Element;
|
11 |
|
12 |
import dk.thoerup.genericjavautils.HttpUtil;
|
13 |
|
14 |
public class ScraperWorker implements Runnable {
|
15 |
|
16 |
@Override
|
17 |
public void run() {
|
18 |
|
19 |
ResultBuffer rb = ResultBuffer.getResultBuffer();
|
20 |
try {
|
21 |
|
22 |
rb.clear();
|
23 |
rb.addString("Starting");
|
24 |
|
25 |
|
26 |
Response res = Jsoup.connect("http://medlemssystem.spejdernet.dk/login.aspx?ReturnUrl=%2f").execute();
|
27 |
Map<String, String> cookies = res.cookies();
|
28 |
|
29 |
Document loginPage = res.parse();
|
30 |
|
31 |
Element viewState = loginPage.getElementById("__VIEWSTATE");
|
32 |
|
33 |
|
34 |
rb.addString("Got login page");
|
35 |
|
36 |
res = Jsoup
|
37 |
.connect("http://medlemssystem.spejdernet.dk/Login.aspx?changeuser=1")
|
38 |
.data("ctl00$main$loginForm$_txtUserName", "torbenhoerupnielsen",
|
39 |
"ctl00$main$loginForm$_txtPassword", "Pwspejder2013",
|
40 |
"ctl00$main$loginForm$_buttonLogin", "Login",
|
41 |
"__EVENTTARGET", "",
|
42 |
"__EVENTARGUMENT", "",
|
43 |
"__VIEWSTATE", viewState.attr("value"),
|
44 |
"ctl00$ctl04$hiddenTab", ""
|
45 |
)
|
46 |
.method(Method.POST)
|
47 |
.cookies(cookies)
|
48 |
.execute();
|
49 |
|
50 |
rb.addString("Login OK");
|
51 |
//This will get you cookies
|
52 |
cookies.putAll( res.cookies() );
|
53 |
//printMap(cookies);
|
54 |
|
55 |
|
56 |
String dataCsvUrl = "http://medlemssystem.spejdernet.dk/csvx.ashx?q=JQBOZXRtZXN0ZXIuS0ZVTS5NZW1iZXIsIE5ldG1lc3Rlci5LRlVNPgB4LlVuaXQuR3JvdXAuSWQgPT0gImd1aWQ6NWMwY2Y2MzgtZGM4Ny00MTNmLTkxYjYtOWM3MzAwYzcwMTM3Ig==&type=Member&view=csv";
|
57 |
//And this is the easieste way I've found to remain in session
|
58 |
Response data = Jsoup.connect(dataCsvUrl).cookies(cookies).timeout(10000).execute();
|
59 |
if ( ! data.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) {
|
60 |
|
61 |
rb.addString("Data.csv - has the wrong content type: " + data.contentType());
|
62 |
return;
|
63 |
}
|
64 |
String dataCsv = data.body();
|
65 |
rb.addString("Got data.csv");
|
66 |
|
67 |
String rollerCsvUrl = "http://medlemssystem.spejdernet.dk/DataExport.aspx?Id=5c0cf638-dc87-413f-91b6-9c7300c70137&listid=e8c5ae9d-5ea7-4a00-bea4-a0ce00ea891e&execute=true";
|
68 |
Response roller = Jsoup.connect(rollerCsvUrl).cookies(cookies).timeout(10000).execute();
|
69 |
if ( ! roller.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) {
|
70 |
rb.addString("Roller.csv - has the wrong content type: " + roller.contentType());
|
71 |
return;
|
72 |
}
|
73 |
String rollerCsv = roller.body();
|
74 |
rb.addString("Got roller.csv");
|
75 |
|
76 |
Map<String,String> postData = new TreeMap<String,String>();
|
77 |
postData.put("roller", rollerCsv);
|
78 |
postData.put("data", dataCsv);
|
79 |
|
80 |
String params = HttpUtil.encodeParams( postData );
|
81 |
byte resp[] = HttpUtil.postContent("http://horsensspejder.t-hoerup.dk/data/postdata.php", params, 3000);
|
82 |
|
83 |
String response = new String(resp);
|
84 |
rb.addString("POST Response: " + response);
|
85 |
|
86 |
rb.addString("Done !");
|
87 |
|
88 |
} catch (Exception e) {
|
89 |
rb.addString("Error occurred: " + e.toString() );
|
90 |
}
|
91 |
}
|
92 |
|
93 |
static void printMap(Map<String,String> map) {
|
94 |
for(String key : map.keySet()) {
|
95 |
System.out.println( key + ": " +map.get(key) );
|
96 |
}
|
97 |
|
98 |
}
|
99 |
|
100 |
public static void main(String[] args) throws Exception {
|
101 |
new ScraperWorker().run();
|
102 |
|
103 |
System.out.println( ResultBuffer.getResultBuffer().getString() );
|
104 |
}
|
105 |
|
106 |
|
107 |
}
|