Efficiency improvements on getNextDepartures

Precompute the terminal stops using scriptSQL to speed up the SQL query
in getNextDepartures.