I'm studying ruby right now. I will write it here as a record.
getting_delayed_trains_info.rb
require 'open-uri'
require 'nokogiri' #Gem for scraping
require 'json' #Call to use json
#Parse the url
url = 'http://tetsudo.rti-giken.jp/free/delay.json'
html = open(url).read
doc = Nokogiri::HTML(html, url)
#Parse to Json format and convert to hash
hash = JSON.parse doc.css("body").inner_text
#Enter the route name you want to check as standard and input_Store in train
puts "Please enter the route for which you want to know the delay status.Example: Keihin Tohoku Line"
input_train = gets.chomp
# delayed_prepare trains array variable
delayed_trains = Array.new
# input_Delayed route name containing train string and updated date / time delayed_Store in trains
hash.each {|train| delayed_train << train["name"] + \
'(Update date and time: ' + Time.at(train["lastupdate_gmt"], in: "+09:00").strftime("%Y-%m-%d %H:%M:%S").to_s + ')' \
if train["name"].include? input_train }
#If the array is empty, there is no route to be delayed, otherwise the route name is output.
if delayed_trains.empty?
puts "Send me "#{input_train}There is no delay in the routes that include the characters"
else
puts delayed_trains.join(",") + "Seems to be delayed\n Please see the railway company website for details."
end
There are some articles that have created parrot return with LINE BOT, but if you refer to it and put in this algorithm, I think that you can make LINE BOT that will answer if you ask the route you want to ask if it is delayed. ..
After all, I am studying and motivated to make what I make into a shape.
Recommended Posts