wassr→twitter

ちょっと修正

#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'rexml/document'
gem 'twitter'
require 'twitter'

TWITTER_USER = 'shokai'
TWITTER_PASS = 'your-passwd'

TWITTER_FEED = 'http://twitter.com/statuses/user_timeline/3631571.rss'
#TWITTER_FEED = 'http://localhost:10080/twitter_feed.rss'
WASSR_FEED = 'http://api.wassr.jp/user_timeline.rss?id=shokai'
#WASSR_FEED = 'http://localhost:10080/wasser_feed.rss'

def contain(word, array)
  array.each{ |item|
    return true if word == item
  }
  return false
end

def post_twitter(posts)
  twit = Twitter::Base.new(TWITTER_USER, TWITTER_PASS)
  posts.each{|post|
    puts 'posting: '<< post
    twit.update(Kconv.kconv(post, Kconv::UTF8))
  }
end

wassr_posts = Array.new
doc = REXML::Document.new(open(WASSR_FEED).read)
REXML::XPath.each(doc, '//item'){ |item|
  p = REXML::XPath.first(item,'description').text
  wassr_posts << p if !(p =~ /^D\s.*/)
}

twitter_posts = Array.new
doc = REXML::Document.new(open(TWITTER_FEED).read)
REXML::XPath.each(doc, '//item'){ |item|
  twitter_posts << REXML::XPath.first(item,'description').text.split("#{TWITTER_USER}: ")[1]
}

posts = Array.new
wassr_posts.each{ |w_post|
  if contain(w_post, twitter_posts) == false
    posts << w_post
  else
    posts.reverse!
    post_twitter(posts)
    exit(1)
  end
}

posts.reverse!
post_twitter(posts)