Skip to content

Commit

Permalink
Made code look nicer and added build instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
w-henderson committed Nov 28, 2020
1 parent 9d72725 commit 9dd18ab
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 75 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ You might've seen my other project, PyTrains, which provides an identical CLI to
| ✔️ **Doesn't require a binary** and is therefore **smaller** | ❌ Requires a ~500KB binary to be installed |
| ❌ Requires Python (>100MB) and 6 dependencies | ✔️ **No dependencies**, works out-of-the-box |

In short, use PyTrains if you want access to the Python library to use in your own code, but if you're only looking for a CLI to access realtime UK train information, NimTrains' massive speed advantage makes it a better option.
In short, use PyTrains if you want access to the Python library to use in your own code, but if you're only looking for a CLI to access realtime UK train information, NimTrains' massive speed advantage makes it a better option.

## Building Yourself
It's very easy to build NimTrains yourself as it doesn't have any dependencies. You can use the supplied `make.bat` file to build for release or run `nimble build` to use Nimble.
143 changes: 70 additions & 73 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,91 +75,88 @@ proc loadDepartures(crs: string): Station =

var actualIterValue = 0
for s in 0..len(servicesXML)-1:
let serviceXML = servicesXML[s]

# If the service terminates, ignore it
if serviceXML.findAll("ServiceType")[0].attr("Type") == "Terminating":
continue

# Put together the list of stops
var callingPointsObject = serviceXML.findAll("Dest1CallingPoints")[0].findAll("CallingPoint")
var stops: seq[CallingPoint] = newSeq[CallingPoint](parseInt(serviceXML.findAll("Dest1CallingPoints")[0].attr("NumCallingPoints")) + 1)

if len(stops) > 1:
for i in 0..len(callingPointsObject)-1:
stops[i] = CallingPoint(
name: callingPointsObject[i].attr("Name"),
crs: callingPointsObject[i].attr("crs"),
scheduledDeparture: parseTime(callingPointsObject[i].attr("ttdep")),
expectedDeparture: parseTime(callingPointsObject[i].attr("etdep"))
)
try:
let serviceXML = servicesXML[s]

# If the service terminates, ignore it
if serviceXML.findAll("ServiceType")[0].attr("Type") == "Terminating":
continue

# Put together the list of stops
var callingPointsObject = serviceXML.findAll("Dest1CallingPoints")[0].findAll("CallingPoint")
var stops: seq[CallingPoint] = newSeq[CallingPoint](parseInt(serviceXML.findAll("Dest1CallingPoints")[0].attr("NumCallingPoints")) + 1)

if len(stops) > 1:
for i in 0..len(callingPointsObject)-1:
stops[i] = CallingPoint(
name: callingPointsObject[i].attr("Name"),
crs: callingPointsObject[i].attr("crs"),
scheduledDeparture: parseTime(callingPointsObject[i].attr("ttdep")),
expectedDeparture: parseTime(callingPointsObject[i].attr("etdep"))
)
stops[len(stops) - 1] = CallingPoint(
name: serviceXML.findAll("Destination1")[0].attr("name"),
crs: serviceXML.findAll("Destination1")[0].attr("crs"),
scheduledDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("ttarr")),
expectedDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("etarr"))
)
except:
try:
stops[len(stops) - 1] = CallingPoint(
name: serviceXML.findAll("Destination1")[0].attr("name"),
crs: serviceXML.findAll("Destination1")[0].attr("crs"),
scheduledDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("ttarr")),
expectedDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("etarr"))
expectedDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("ttarr"))
)
except:
try:
stops[len(stops) - 1] = CallingPoint(
name: serviceXML.findAll("Destination1")[0].attr("name"),
crs: serviceXML.findAll("Destination1")[0].attr("crs"),
scheduledDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("ttarr")),
expectedDeparture: parseTime(serviceXML.findAll("Destination1")[0].attr("ttarr"))
)
except:
discard "Ignore exception"

# Parse the last report of the train
var report = ""
if serviceXML.findAll("LastReport")[0].attr("type") == "T":
report = "At " & serviceXML.findAll("LastReport")[0].attr("station1") & " (" & timeFmt(parseTime(serviceXML.findAll("LastReport")[0].attr("time"))) & ")"
elif serviceXML.findAll("LastReport")[0].attr("type") == "B":
report = "Between " & serviceXML.findAll("LastReport")[0].attr("station1") & " and " & serviceXML.findAll("LastReport")[0].attr("station2") & " (" & timeFmt(parseTime(serviceXML.findAll("LastReport")[0].attr("time"))) & ")"
else:
report = "No report."

# Parse the platform of the train
var platform = serviceXML.findAll("Platform")[0].attr("Number")
if platform == "":
platform = "?"

# If the expected departure is different to the timetabled departure, make it known
var expectedDeparture = parseTime(serviceXML.findAll("DepartTime")[0].attr("time"))
try:
expectedDeparture = parseTime(serviceXML.findAll("ExpectedDepartTime")[0].attr("time"))
except:
discard "Ignore exception"

var coaches = 0
try:
coaches = parseInt(serviceXML.findAll("Coaches1")[0].innerText)
except:
discard "Ignore exception"

# Put everything together into a Service object
services[actualIterValue] = Service(
destination: serviceXML.findAll("Destination1")[0].attr("name"),
destinationCRS: serviceXML.findAll("Destination1")[0].attr("crs"),
origin: serviceXML.findAll("Origin1")[0].attr("name"),
scheduledDeparture: parseTime(serviceXML.findAll("DepartTime")[0].attr("time")),
expectedDeparture: expectedDeparture,
delay: parseInt("0" & serviceXML.findAll("Delay")[0].attr("Minutes")),
delayCause: noneIfNotFound(serviceXML, "DelayCause"),
platform: platform,
platformComment: noneIfNotFound(serviceXML, "PlatformComment1") & " " & noneIfNotFound(serviceXML, "PlatformComment2"),
operator: serviceXML.findAll("Operator")[0].attr("name"),
trainComment: noneIfNotFound(serviceXML, "AssociatedPageNotices"),
lastReport: report,
carriageCount: coaches,
callingPoints: stops
)
# Parse the last report of the train
var report = ""
if serviceXML.findAll("LastReport")[0].attr("type") == "T":
report = "At " & serviceXML.findAll("LastReport")[0].attr("station1") & " (" & timeFmt(parseTime(serviceXML.findAll("LastReport")[0].attr("time"))) & ")"
elif serviceXML.findAll("LastReport")[0].attr("type") == "B":
report = "Between " & serviceXML.findAll("LastReport")[0].attr("station1") & " and " & serviceXML.findAll("LastReport")[0].attr("station2") & " (" & timeFmt(parseTime(serviceXML.findAll("LastReport")[0].attr("time"))) & ")"
else:
report = "No report."

# Parse the platform of the train
var platform = serviceXML.findAll("Platform")[0].attr("Number")
if platform == "":
platform = "?"

# If the expected departure is different to the timetabled departure, make it known
var expectedDeparture = parseTime(serviceXML.findAll("DepartTime")[0].attr("time"))
try:
expectedDeparture = parseTime(serviceXML.findAll("ExpectedDepartTime")[0].attr("time"))
except:
discard "Ignore exception"

actualIterValue += 1
var coaches = 0
try:
coaches = parseInt(serviceXML.findAll("Coaches1")[0].innerText)
except:
echo servicesXML[s]
discard "Ignore exception"

# Put everything together into a Service object
services[actualIterValue] = Service(
destination: serviceXML.findAll("Destination1")[0].attr("name"),
destinationCRS: serviceXML.findAll("Destination1")[0].attr("crs"),
origin: serviceXML.findAll("Origin1")[0].attr("name"),
scheduledDeparture: parseTime(serviceXML.findAll("DepartTime")[0].attr("time")),
expectedDeparture: expectedDeparture,
delay: parseInt("0" & serviceXML.findAll("Delay")[0].attr("Minutes")),
delayCause: noneIfNotFound(serviceXML, "DelayCause"),
platform: platform,
platformComment: noneIfNotFound(serviceXML, "PlatformComment1") & " " & noneIfNotFound(serviceXML, "PlatformComment2"),
operator: serviceXML.findAll("Operator")[0].attr("name"),
trainComment: noneIfNotFound(serviceXML, "AssociatedPageNotices"),
lastReport: report,
carriageCount: coaches,
callingPoints: stops
)

actualIterValue += 1

# Return a Station object with the name, CRS code and the sequence of Service objects
return Station(
Expand Down
3 changes: 2 additions & 1 deletion src/nimtrains.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ include api # Import procs from api.nim
import terminal # For coloring text
import strutils # For string stuff
import strformat # For more string stuff
import os # For printing stuff I think
import os

# Little proc to write coloured text to the terminal
proc coloredWrite(textToWrite: string, color: enum = fgWhite) =
setForegroundColor(color)
setStyle({styleBright})
Expand Down

0 comments on commit 9dd18ab

Please sign in to comment.