2 |
|
|
3 |
|
|
4 |
import java.net.URL; |
import java.net.URL; |
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
5 |
import java.util.Map; |
import java.util.Map; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
|
|
|
8 |
import org.jsoup.nodes.Document; |
import org.jsoup.nodes.Document; |
9 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
10 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
11 |
|
|
12 |
|
import dk.thoerup.android.traininfo.common.MetroBean; |
13 |
|
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
14 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
15 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
|
import dk.thoerup.traininfoservice.StationBean; |
|
|
import dk.thoerup.traininfoservice.StationDAO; |
|
17 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
18 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
public class MetroFetcher { |
public class MetroFetcher { |
|
public static class MetroEntry { |
|
|
String metro; |
|
|
String destination; |
|
|
String minutes; |
|
|
} |
|
|
|
|
|
public static class MetroBean { |
|
|
String head; |
|
|
String operationInfo; //driftsinfo |
|
|
String plan; // koereplan |
|
|
|
|
|
List<MetroEntry> entries = new ArrayList<MetroEntry>(); |
|
|
} |
|
23 |
|
|
24 |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
25 |
StationDAO stationDAO = new StationDAO(); |
StationDAO stationDAO = new StationDAO(); |
43 |
|
|
44 |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
45 |
|
|
46 |
StationBean station = stationDAO.getById(stationID) ; |
StationEntry station = stationDAO.getById(stationID) ; |
47 |
|
|
48 |
|
|
49 |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
61 |
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
62 |
Elements contentElems = content.children(); |
Elements contentElems = content.children(); |
63 |
|
|
64 |
Element departureTable = content.getElementsByTag("table").get(0); |
Elements contentTables = content.getElementsByTag("table"); |
65 |
|
if ( contentTables.size() > 0 ) { |
|
Elements rows = departureTable.getElementsByTag("tr"); |
|
|
|
|
|
for (int i=1; i<rows.size(); i++) { |
|
|
Element row = rows.get(i); |
|
|
Elements fields = row.children(); |
|
66 |
|
|
67 |
String metro = fields.get(0).text(); |
Element departureTable = content.getElementsByTag("table").get(0); |
68 |
String destination = fields.get(1).text(); |
|
69 |
Elements minuteFields = fields.get(2).children(); |
Elements rows = departureTable.getElementsByTag("tr"); |
70 |
String minutes =""; |
|
71 |
|
for (int i=1; i<rows.size(); i++) { |
72 |
for (Element elm : minuteFields) { |
Element row = rows.get(i); |
73 |
minutes = minutes + elm.text() + " "; |
Elements fields = row.children(); |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
Elements metroFields = fields.get(0).children(); |
78 |
|
String metro =""; |
79 |
|
for (Element elm : metroFields) { |
80 |
|
metro = metro + elm.text() + " "; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
String destination = fields.get(1).text(); |
86 |
|
|
87 |
|
Elements minuteFields = fields.get(2).children(); |
88 |
|
String minutes =""; |
89 |
|
for (Element elm : minuteFields) { |
90 |
|
minutes = minutes + elm.text() + " "; |
91 |
|
} |
92 |
|
|
93 |
|
MetroEntry entry = new MetroEntry(); |
94 |
|
entry.metro = metro.trim(); |
95 |
|
entry.destination = destination.trim(); |
96 |
|
entry.minutes = minutes.trim(); |
97 |
|
|
98 |
|
bean.entries.add(entry); |
99 |
} |
} |
|
|
|
|
MetroEntry entry = new MetroEntry(); |
|
|
entry.metro = metro; |
|
|
entry.destination = destination; |
|
|
entry.minutes = minutes; |
|
|
|
|
|
bean.entries.add(entry); |
|
100 |
} |
} |
101 |
|
|
102 |
bean.operationInfo = contentElems.get(3).text(); |
|
103 |
|
for (int i=0; i<contentElems.size(); i++) { |
104 |
int planIndex; |
String currentText = contentElems.get(i).text().trim(); |
105 |
if (contentElems.get(4).text().trim().equalsIgnoreCase("stationsinformation")) { |
if (currentText.equalsIgnoreCase("driftsinformation")) { |
106 |
planIndex = 7; |
bean.operationInfo = contentElems.get(i+1).text(); |
107 |
} else { |
} |
|
planIndex = 5; |
|
|
} |
|
108 |
|
|
109 |
bean.plan = contentElems.get(planIndex).text(); |
if (currentText.equalsIgnoreCase("køreplan")) { |
110 |
|
bean.plan = contentElems.get(i+1).text(); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
return bean; |
return bean; |
117 |
} |
} |
118 |
|
|