/[projects]/miscJava/SpejdernetScraper/src/dk/thoerup/spejdernetscraper/ScraperWorker.java
ViewVC logotype

Contents of /miscJava/SpejdernetScraper/src/dk/thoerup/spejdernetscraper/ScraperWorker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2252 - (show annotations) (download)
Sun Dec 28 20:36:40 2014 UTC (9 years, 4 months ago) by torben
File size: 4261 byte(s)
-
1 package dk.thoerup.spejdernetscraper;
2
3 import java.util.Map;
4 import java.util.TreeMap;
5
6 import org.apache.commons.lang3.exception.ExceptionUtils;
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 // /////////////////////////////////
49 // Data.csv
50
51 String dataCsvUrl = "http://medlemssystem.spejdernet.dk/csvx.ashx?q=JQBOZXRtZXN0ZXIuS0ZVTS5NZW1iZXIsIE5ldG1lc3Rlci5LRlVNPgB4LlVuaXQuR3JvdXAuSWQgPT0gImd1aWQ6NWMwY2Y2MzgtZGM4Ny00MTNmLTkxYjYtOWM3MzAwYzcwMTM3Ig==&type=Member&view=csv";
52 //And this is the easieste way I've found to remain in session
53 Response data = Jsoup.connect(dataCsvUrl).cookies(cookies).timeout(10000).execute();
54 if ( ! data.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) {
55
56 rb.addString("Data.csv - has the wrong content type: " + data.contentType());
57 return;
58 }
59 String dataCsv = data.body();
60 rb.addString("Got data.csv");
61
62 // /////////////////////////////////
63 // Roller.csv
64 /*
65 String rollerCsvUrl = "http://medlemssystem.spejdernet.dk/DataExport.aspx?Id=5c0cf638-dc87-413f-91b6-9c7300c70137&listid=e8c5ae9d-5ea7-4a00-bea4-a0ce00ea891e&execute=true";
66 Response roller = Jsoup.connect(rollerCsvUrl).cookies(cookies).timeout(10000).execute();
67 if ( ! roller.contentType().equalsIgnoreCase("text/x-csv; charset=iso-8859-1") ) {
68 rb.addString("Roller.csv - has the wrong content type: " + roller.contentType());
69 return;
70 }
71 String rollerCsv = roller.body();
72 rb.addString("Got roller.csv");*/
73 String rollerCsv = ""; //Temporary fix for PrematureEof exception thingie
74
75 Map<String,String> postData = new TreeMap<String,String>();
76 postData.put("roller", rollerCsv);
77 postData.put("data", dataCsv);
78
79 String params = HttpUtil.encodeParams( postData );
80 byte resp[] = HttpUtil.postContent("http://horsensspejder.t-hoerup.dk/data/postdata.php", params, 3000);
81
82 String response = new String(resp);
83 rb.addString("POST Response: " + response);
84
85 rb.addString("Done !");
86
87 } catch (Exception e) {
88 rb.addString("Error occurred: " + e.toString() );
89 rb.addString( ExceptionUtils.getStackTrace(e) );
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 /*
108 *
109 * Just fore reference, here's the old login method
110 res = Jsoup
111 .connect("http://medlemssystem.spejdernet.dk/Login.aspx?changeuser=1")
112 .data("ctl00$main$loginForm$_txtUserName", "<enter-username-here>",
113 "ctl00$main$loginForm$_txtPassword", "<enter-password-here>",
114 "ctl00$main$loginForm$_buttonLogin", "Login",
115 "__EVENTTARGET", "",
116 "__EVENTARGUMENT", "",
117 "__VIEWSTATE", viewState.attr("value"),
118 "ctl00$ctl04$hiddenTab", ""
119 )
120 .method(Method.POST)
121 .cookies(cookies)
122 .execute();*/
123
124 }

  ViewVC Help
Powered by ViewVC 1.1.20