Last active
June 13, 2017 21:03
-
-
Save namuan/36064ff397a1ed3b6e72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabConfig(systemClassLoader = true) | |
@Grapes([ | |
@Grab(group = 'commons-codec', module = 'commons-codec', version = '1.10'), | |
@Grab(group = 'org.jsoup', module = 'jsoup', version = '1.8.1'), | |
@Grab(group = 'commons-lang', module = 'commons-lang', version = '2.6'), | |
@Grab(group = 'joda-time', module = 'joda-time', version = '2.3') | |
]) | |
import org.jsoup.* | |
import org.jsoup.nodes.Document | |
import org.jsoup.nodes.Element | |
import org.jsoup.select.Elements | |
import java.text.MessageFormat | |
String selector = "tr.mtx" | |
String url = "http://ojp.nationalrail.co.uk/service/timesandfares/{0}/{1}/today/{2}/dep" | |
String fromStation = args.size() > 0 ? args[0] : "REI" | |
String toStation = args.size() > 1 ? args[1] : "LBG" | |
(8..10).each { | |
String timeIn24Hours = String.format("%02d00", it) | |
String formattedUrl = MessageFormat.format(url, fromStation, toStation, timeIn24Hours) | |
Document doc = Jsoup.connect(formattedUrl).userAgent("Mozilla").get(); | |
Elements journeys = doc.select(selector) | |
journeys.each { Element journey -> | |
Elements journeyCols = journey.select("td") | |
Map jd = journeyCols.collectEntries { Element col -> | |
String className = col.className() | |
[(className): getValueForClass(col, className)] | |
} | |
println("${jd.from} to ${jd.to}") | |
println("${jd.dep} -> ${jd.arr} = ${jd.dur} <=> ${jd.status}") | |
hr() | |
sleep(3 * 1000) | |
} | |
} | |
String getValueForClass(Element element, String className) { | |
if (element.className() == className) | |
return element.text() | |
return null | |
} | |
void hr() { | |
println "-" * 50 | |
} | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment