Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SMS alerts using Ruby, BeautifulSoup and Twitter

  • Loading branch information...
commit 95453eb0b0f71ba7761b94d3624e3b9fdc02599c 0 parents
Duncan Gough authored
0  README
No changes.
38 helpers/config_store.rb
... ... @@ -0,0 +1,38 @@
  1 +class ConfigStore
  2 + attr_reader :file
  3 +
  4 + def initialize(file)
  5 + @file = file
  6 + end
  7 +
  8 + def load
  9 + @config ||= YAML::load(open(file))
  10 + self
  11 + end
  12 +
  13 + def [](key)
  14 + load
  15 + @config[key]
  16 + end
  17 +
  18 + def []=(key, value)
  19 + @config[key] = value
  20 + end
  21 +
  22 + def delete(*keys)
  23 + keys.each { |key| @config.delete(key) }
  24 + save
  25 + self
  26 + end
  27 +
  28 + def update(c={})
  29 + @config.merge!(c)
  30 + save
  31 + self
  32 + end
  33 +
  34 + def save
  35 + File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
  36 + self
  37 + end
  38 +end
83 traintimes.rb
... ... @@ -0,0 +1,83 @@
  1 +require 'rubygems'
  2 +require 'rubyful_soup'
  3 +require 'open-uri'
  4 +require 'twitter'
  5 +require File.join(File.dirname(__FILE__), 'helpers', 'config_store')
  6 +
  7 +# Run via a cron job at 7:45am and 5:30pm
  8 +
  9 +# load the page for the 8:13 train and the 6.23 train
  10 +# scrape the results
  11 +# send me a DM with the line containing those trains
  12 +
  13 +config = ConfigStore.new(File.join(File.dirname(__FILE__), '.twitter'))
  14 +oauth = Twitter::OAuth.new(config['token'], config['secret'])
  15 +
  16 +if config['atoken'] && config['asecret']
  17 + oauth.authorize_from_access(config['atoken'], config['asecret'])
  18 + twitter = Twitter::Base.new(oauth)
  19 +
  20 +elsif config['rtoken'] && config['rsecret']
  21 + oauth.authorize_from_request(config['rtoken'], config['rsecret'], config['pin'])
  22 + twitter = Twitter::Base.new(oauth)
  23 +
  24 + config.update({
  25 + 'atoken' => oauth.access_token.token,
  26 + 'asecret' => oauth.access_token.secret,
  27 + }).delete('rtoken', 'rsecret')
  28 +else
  29 + config.update({
  30 + 'rtoken' => oauth.request_token.token,
  31 + 'rsecret' => oauth.request_token.secret,
  32 + })
  33 +
  34 + # A better implementation is available at http://gist.github.com/524376
  35 + puts "Authorize this request at the following url:"
  36 + puts oauth.request_token.authorize_url
  37 + puts "Then add an entry for ping to the .twitter config store"
  38 +end
  39 +
  40 +
  41 +trains = {
  42 + :morning => {
  43 + :time => '08:13',
  44 + :info => 'Dartford to Charing Cross',
  45 + :url => 'http://traintimes.org.uk/dartford/london+charing+cross/08:00/today/overtake=1'
  46 + },
  47 + :evening => {
  48 + :time => '18:23',
  49 + :info => 'Charing Cross to Dartford',
  50 + :url => 'http://traintimes.org.uk/london+charing+cross/dartford/18:15/today/overtake=1'
  51 + },
  52 + :test => {
  53 + :time => '22:39',
  54 + :info => 'Charing Test to Darttest',
  55 + :url => 'http://traintimes.org.uk/london+charing+cross/dartford/22:20/today/overtake=1'
  56 + }
  57 +}
  58 +
  59 +# If we're running at 5pm, look for evening trains. Otherwise, look for morning trains.
  60 +# - todo, make this better by putting all the trains data into a config file
  61 +# - and use the :time key to check what trains to search for
  62 +#
  63 +# Run @hourly, check each of the hour keys in the trains hash, then run the rest...
  64 +hour = Time.now.hour
  65 +key = (hour == 17 ? :evening : :morning)
  66 +
  67 +url = trains[key][:url]
  68 +info = trains[key][:info]
  69 +time = trains[key][:time]
  70 +
  71 +# Digest the page and DM me the details
  72 +# - todo, only DM me if there are problems with the train
  73 +open(url) do |page|
  74 + page_content = page.read()
  75 + soup = BeautifulSoup.new(page_content)
  76 + result = soup.find_all('p', :attrs => {'align' => 'center'})[0].find_all('li')
  77 + result.each do |tag|
  78 + if tag.to_s.include?(time)
  79 + dm = tag.to_s.gsub(/<\/?[^>]*>/, '').gsub('%ndash', '-').split('iCal')[0].chop + ' ~ ' + info
  80 + twitter.direct_message_create('suttree', dm)
  81 + end
  82 + end
  83 +end

0 comments on commit 95453eb

Please sign in to comment.
Something went wrong with that request. Please try again.