Created
May 24, 2016 22:02
-
-
Save TimSmith714/bdb555b8ef37a1ea7608b4f5f8eb5e41 to your computer and use it in GitHub Desktop.
Southern Railway status scraper
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
require 'nokogiri' | |
require 'date' | |
require 'open-uri' | |
lineCodes = ['.bm', '.ge','.re','.me'] | |
goodLines = [] | |
problemLines = [] | |
subjectLine = '' | |
body = '' | |
#Get the Southern Homepage | |
page = Nokogiri::HTML(open('http://www.southernrailway.com')) | |
# Build the lists of good and problem lines | |
lineCodes.each do | line | | |
if page.css(line + ' + span').text == ' Good service ' | |
goodLines.push(line) | |
else | |
problemLines.push(line) | |
end | |
end | |
# Build Subject Line | |
if problemLines.length == 0 | |
subjectLine = "All Southern Lines running fine" | |
else | |
subjectLine = "Train problems on: " | |
problemLines.each do |problem| | |
if problem == problemLines.last | |
subjectLine.concat(page.css(problem).text.to_s) | |
else | |
subjectLine.concat(page.css(problem).text.to_s) | |
subjectLine.concat(', ') | |
end | |
end | |
end | |
puts subjectLine | |
#Now build body of message | |
if problemLines.length > 0 | |
body.concat("There are problems on the following lines \n") | |
problemLines.each do |problem| | |
body.concat(page.css(problem).text) | |
body.concat("\n") | |
end | |
end | |
if goodLines.length > 0 | |
body.concat("\n There is a good service on the following lines\n") | |
goodLines.each do |good| | |
body.concat(page.css(good).text) | |
body.concat("\n") | |
end | |
end | |
puts body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment