Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moving the train times to a .yml file, updating the README, and creat…

…ing some example files
  • Loading branch information...
commit 2f9b705d74871513169704284dd33500d2dd635c 1 parent e2f32e0
@suttree authored
Showing with 67 additions and 52 deletions.
  1. +27 −1 README
  2. +3 −0  dot-twitter-example
  3. +4 −0 trains-example.yml
  4. +33 −51 traintimes.rb
View
28 README
@@ -1 +1,27 @@
-trainee - SMS travel alerts using the traintimes.org.uk website, the incredible BeautifulSoup parser and Twitter DMs that you can choose to have sent as SMS messages.
+=trainee=
+
+==SMS travel alerts using the traintimes.org.uk website, the incredible BeautifulSoup parser and Twitter DMs that you can choose to have sent as SMS messages.==
+
+All you need are a few ruby gems, an oauth token and secret from twitter.com/apps and a cron job to periodically run the script
+
+INSTALL
+
+- install the twitter and rubyful_soup gems
+- setup the .twitter file with your twitter auth details, an example file (dot-twitter-example) is provided
+- setup the trains.yml file as required, an example file (trains-example.yml) is provided
+- add an entry to your cron file to run the script as often as you'd like
+
+
+CRON EXAMPLES
+
+- Early each weekday morning:
+30 6 * * 1-5 /usr/bin/ruby /home/suttree/src/twitter/traintimes.rb >> /dev/null 2>&1
+
+- On the hour, every hour:
+@hourly /usr/bin/ruby /home/suttree/src/twitter/traintimes.rb >> /dev/null 2>&1
+
+TWITTER SETUP
+
+- Go to http://twitter.com/apps and register an application that you will use to send DMs, and subsequently SMSs, to yourself.
+- Then edit your twitter mobile settings at http://twitter.com/devices
+ - Run once, add something about pin entry in the .twitter file too
View
3  dot-twitter-example
@@ -0,0 +1,3 @@
+token: 1234567890
+secret: 1234567890
+pin: 1234567
View
4 trains-example.yml
@@ -0,0 +1,4 @@
+---
+'12:26':
+ :url: http://traintimes.org.uk/london+charing+cross/sidcup/12:20/today/overtake=1
+ :info: Charing Cross to Sidcup
View
84 traintimes.rb
@@ -1,15 +1,10 @@
require 'rubygems'
-require 'rubyful_soup'
-require 'open-uri'
-require 'twitter'
-require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
-
-# Run via a cron job at 7:45am and 5:30pm
-# load the page for the 8:13 train and the 6.23 train
-# scrape the results
-# send me a DM with the line containing those trains
+require 'twitter'
+require 'open-uri'
+require 'rubyful_soup'
+require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
config = ConfigStore.new(File.join(File.dirname(__FILE__), '.twitter'))
oauth = Twitter::OAuth.new(config['token'], config['secret'])
@@ -31,53 +26,40 @@
'rsecret' => oauth.request_token.secret,
})
- # A better implementation is available at http://gist.github.com/524376
- puts "Authorize this request at the following url:"
- puts oauth.request_token.authorize_url
- puts "Then add an entry for ping to the .twitter config store"
+ puts <<EOS
+Visit #{oauth.request_token.authorize_url} in your browser to authorize the app,
+then enter the PIN you are given:
+EOS
+
+ pin = STDIN.readline.chomp
+ config.update({ 'pin' => pin })
+ exit('Run this script again, now that you are authorised')
end
-
-trains = {
- :morning => {
- :time => '08:13',
- :info => 'Dartford to Charing Cross',
- :url => 'http://traintimes.org.uk/dartford/london+charing+cross/08:00/today/overtake=1'
- },
- :evening => {
- :time => '18:23',
- :info => 'Charing Cross to Dartford',
- :url => 'http://traintimes.org.uk/london+charing+cross/dartford/18:15/today/overtake=1'
- },
- :test => {
- :time => '22:39',
- :info => 'Charing Test to Darttest',
- :url => 'http://traintimes.org.uk/london+charing+cross/dartford/22:20/today/overtake=1'
- }
-}
+trains = YAML::load(File.read(File.join(File.dirname(__FILE__), 'trains.yml')))
+hours = trains.keys.collect{ |time| time.split(':')[0].to_i }
-# If we're running at 5pm, look for evening trains. Otherwise, look for morning trains.
-# - todo, make this better by putting all the trains data into a config file
-# - and use the :time key to check what trains to search for
-#
-# Run @hourly, check each of the hour keys in the trains hash, then run the rest...
-hour = Time.now.hour
-key = (hour == 17 ? :evening : :morning)
+if hours.include?(Time.now.hour)
+ train = trains.select{ |k, v| (k.split(':')[0].to_i == Time.now.utc.hour) }
-url = trains[key][:url]
-info = trains[key][:info]
-time = trains[key][:time]
+ trains.each do |time|
+ if (Time.parse(time[0]).hour == Time.now.hour)
+ info = time[1][:info]
+ url = time[1][:url]
-# Digest the page and DM me the details
-# - todo, only DM me if there are problems with the train
-open(url) do |page|
- page_content = page.read()
- soup = BeautifulSoup.new(page_content)
- result = soup.find_all('p', :attrs => {'align' => 'center'})[0].find_all('li')
- result.each do |tag|
- if tag.to_s.include?(time)
- dm = tag.to_s.gsub(/<\/?[^>]*>/, '').gsub('%ndash', '-').split('iCal')[0].chop + ' ~ ' + info
- twitter.direct_message_create('suttree', dm)
+ # Digest the page and DM me the details
+ open(url) do |page|
+ page_content = page.read()
+ soup = BeautifulSoup.new(page_content)
+ result = soup.find_all('p', :attrs => {'align' => 'center'})[0].find_all('li')
+ result.each do |tag|
+ if tag.to_s.include?(time[0].to_s)
+ dm = tag.to_s.gsub(/<\/?[^>]*>/, '').gsub('%ndash', '-').split('iCal')[0].chop + ' ~ ' + info
+ puts "Sending dm: #{dm}..."
+ twitter.direct_message_create('suttree', dm)
+ end
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.