/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 305 by torben, Thu Sep 10 09:40:27 2009 UTC revision 313 by torben, Fri Sep 11 07:13:46 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    import java.sql.Connection;
4    import java.sql.ResultSet;
5    import java.sql.Statement;
6  import java.util.ArrayList;  import java.util.ArrayList;
7    import java.util.Collections;
8  import java.util.List;  import java.util.List;
9    
10  import com.gargoylesoftware.htmlunit.ProxyConfig;  import com.gargoylesoftware.htmlunit.ProxyConfig;
# Line 9  import com.gargoylesoftware.htmlunit.htm Line 13  import com.gargoylesoftware.htmlunit.htm
13  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import com.gargoylesoftware.htmlunit.html.HtmlElement;
14  import com.gargoylesoftware.htmlunit.html.HtmlPage;  import com.gargoylesoftware.htmlunit.html.HtmlPage;
15    
16    import dk.thoerup.traininfoservice.DBConnection;
17    
18  public class DepartureFetcher {  public class DepartureFetcher {
19                            
20            
21          static ProxyConfig proxyConfig;          public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
22          static {                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
23                  proxyConfig = new ProxyConfig();                  
24                  proxyConfig.setProxyHost("rafiki.t-hoerup.dk");                  Connection conn = null;
25                  proxyConfig.setProxyPort(3128);                  try
26                    {
27                            conn = DBConnection.getConnection();
28                    
29                            String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;
30                            Statement stmt = conn.createStatement();
31                            ResultSet rs = stmt.executeQuery(SQL);
32                            
33                            if (rs.next()) {
34                                    String code = rs.getString( 1 );
35                                    if (! rs.wasNull() ) {
36                                            List<DepartureBean> list = lookupDepartures(code, "FJRN");
37                                            departureList.addAll(list);
38                                    }
39                                    
40                                    code = rs.getString(2);
41                                    if (! rs.wasNull() ) {
42                                            List<DepartureBean> list = lookupDepartures(code, "S2");
43                                            departureList.addAll(list);    
44                                    }
45                                    Collections.sort( departureList );
46                            
47                            }
48                            
49                    } finally {
50                            if (conn != null && !conn.isClosed() ) {
51                                    conn.close();
52                            }
53                    }
54                    
55                    return departureList;
56          }          }
57                    
58          public List<DepartureBean> lookupDepartures() throws Exception {          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
59                                    
60                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
61                                    
62              final WebClient webClient = new WebClient();              final WebClient webClient = new WebClient();
63              webClient.setTimeout(1000);              webClient.setTimeout(1000);
64              webClient.setProxyConfig(proxyConfig);              webClient.setJavaScriptEnabled(false);
65              webClient.setJavaScriptEnabled(false);                              
66                            
67              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=FJRN&S=BJ");              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);
68                            
69              HtmlElement table = page.getElementById("afgangtabel");              HtmlElement table = page.getElementById("afgangtabel");
70              DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");              DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
# Line 61  public class DepartureFetcher { Line 97  public class DepartureFetcher {
97                          String status = fields.get(6).asText();                          String status = fields.get(6).asText();
98                          departure.setStatus(status);                          departure.setStatus(status);
99                                                    
100                          String note = fields.get(7).asText();                          String note = extractNote( fields.get(7) );
101                          departure.setNote(note);                          departure.setNote(note);
102                                                    
103                          departureList.add(departure);                          departureList.add(departure);
# Line 89  public class DepartureFetcher { Line 125  public class DepartureFetcher {
125                  return updated;                  return updated;
126          }          }
127                    
128            private String extractNote(HtmlElement noteTd) {
129                    String note = noteTd.asText().trim();
130                    
131                    List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
132                    if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
133                            note = note.substring(0,note.length() -1 );
134    
135                    return note;
136            }
137            
138          //test          //test
139          public static void main(String args[]) throws Exception{          public static void main(String args[]) throws Exception{
140                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();
141                  List<DepartureBean> deps = f.lookupDepartures();                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
142                  for(DepartureBean d : deps) {                  for(DepartureBean d : deps) {
143                          System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +                          System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
144                                                  d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote()   );                                                  d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote()   );

Legend:
Removed from v.305  
changed lines
  Added in v.313

  ViewVC Help
Powered by ViewVC 1.1.20