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