public
Description: Ruby script that takes data from the BBC website and sends it to twitter/pollen_london
Homepage: http://twitter.com/pollen_london
Clone URL: git://github.com/snowblink/pollen-london.git
pollen-london / pollen.rb
100755 72 lines (58 sloc) 2.121 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
#
# Created by Jonathan Lim on 2008-05-13.
# Copyright (c) 2008. All rights reserved.
 
# Twitter pollen updates
 
# $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
 
require 'rubygems'
require 'open-uri'
gem 'twitter4r'
require 'twitter'
require 'hpricot'
require 'yaml'
 
Twitter::Client.configure do |conf|
  conf.application_name = 'PollenLondonBot'
  conf.application_version = '0.5'
  conf.application_url = 'https://github.com/snowblink/pollen-london/tree'
  conf.source = 'pollenlondon'
end
 
twitter_config = YAML::load_file(File.dirname(__FILE__) + '/twitter_config.yml')
twitter = Twitter::Client.new(twitter_config)
 
date = DateTime.now.strftime("%A %Y%m%d")
 
# load BBC Pollen site
doc = Hpricot(open("http://news.bbc.co.uk/weather/forecast/8/UV.xhtml"))
 
elements = doc.search("li")
to_twitter = []
 
# 20091103 - Adding type of pollen based on time of year
# Tree pollen forecasts from the 20th April to 19th May.
# Grass pollen forecasts from the 20th May to 4th August.
# Weed pollen forecasts from the 5th to 25th August.
# Fungal Spore forecasts from the 25th August to 30th November.
 
pollen_type = if ((Date.civil(Date.today.year, 4, 20))..(Date.civil(Date.today.year, 5, 19))).include?(Date.today)
  "Tree Pollen"
elsif ((Date.civil(Date.today.year, 5, 20))..(Date.civil(Date.today.year, 8, 4))).include?(Date.today)
  "Grass Pollen"
elsif ((Date.civil(Date.today.year, 8, 5))..(Date.civil(Date.today.year, 8, 25))).include?(Date.today)
  "Weed Pollen"
elsif ((Date.civil(Date.today.year, 8, 25))..(Date.civil(Date.today.year, 11, 30))).include?(Date.today)
  "Fungal Spore"
end
 
if pollen_type.nil?
  # Outside of pollen season
else
  
  elements.each do |element|
    if element.get_attribute("class") == 'pollenval'
      to_twitter << element.at("img").get_attribute("alt") + " #{pollen_type} (" + Time.now.gmtime.strftime("%a %d/%m") + ')'
    end
  end
 
  to_twitter.each do |update|
    begin
      twitter.status(:post, update)
      # puts update
    rescue Exception => e
      puts "FAILED!"
      puts e
      exit 1
    end
  end
end