以前はFriendFeedを使って複数RSSのTwitterへの通知を行っていたのですが、ある時からFriendFeedが手動で更新してあげないとRSSを更新してくれなくなってしまったので、一度HootSuiteに移行しました。でもそうしたら今度はHootSuiteが多数のフィードを通知するには有料版に移行しなくてはいけなくなってしまったので、それっきりになってました。

無いならつくろう、ということで、思い立って、自宅のサーバー上のRubyスクリプトをCronで回してRSSの取得からTwitterへの投稿を行うスクリプトを書きました。

うまく動作している・・・かな?

コードも貼りつけておきます。ツッコミあれば是非…。 «< # -*- coding: utf-8 -*-

#rssの更新を確認して、twittする

require “cgi” require “open-uri” require “rss” require “twbot2” require “date” require “yaml” require “pp” require “json” require “uri”

class RSS_Twitt < TwBot

 def load_data
   result_array = Array.new()
   filename = "feeds.yml"
   feeds = YAML.load_file(filename)
   feeds.each do |feed|
     puts feed['url']
     rss = open(feed['url']) { |file| RSS::Parser.parse(file.read)}
     lastupdate = feed['lastupdate']
     rss.items.each do |item|
       date = Date.parse(item.date.to_s)
       if lastupdate == nil || lastupdate < date
         short_url = shorten(item.link)
         result_array << "#{feed['comment']} / #{item.title} "
         if feed['lastupdate'] == nil || feed['lastupdate'] < date
           feed['lastupdate'] = date
         end
       end
     end
   end
   #ymlへの書き出し
   open(filename, 'w'){|f| f.puts feeds.to_yaml}
   #Arrayを逆順にする
   puts result_array.reverse
   result_array.reverse
 end
 def shorten(long_url)
   id = 'ebibibi'
   api_key = 'R_aa86b1df9d6f611376c6c75bbab9cd63'
   version = '2.0.1'
   long_url = URI.encode(long_url)
   query = "version=#{version}&longUrl=#{long_url}&login=#{id}&apiKey=#{api_key}"
   result = JSON.parse(Net::HTTP.get("api.bit.ly", "/shorten?#{query}"))
   result['results'].each_pair {|long_url, value|
     return value['shortUrl']
   }
 end

end

SELFDIR = File.dirname(__FILE__) ARGV.each do mode
 RSS_Twitt.new mode, "#{SELFDIR}/config.yml", "#{SELFDIR}/error.log"

end »>

feeds.ymlに読み込むRSSを指定するようにしてあります。 «< ruby — - lastupdate: 2011-01-17

 comment: !binary |
   5pel6KiY44KS5pu444GN44G+44GX44GfIQ==
 url: http://ebi.dyndns.biz/diary/no_comments.rdf

- lastupdate: 2011-01-12

 comment: !binary |
   44OW44Ot44Kw44KS5pu444GN44G+44GX44GfIQ==
 url: http://ebi.dyndns.biz/windowsadmin/feed/

- lastupdate: 2011-01-18

 comment: !binary |
   44OW44Ot44Kw44KS5pu444GN44G+44GX44GfIQ==
 url: http://ebi.dyndns.biz/learning_english/feed/

- lastupdate: 2011-01-13

 comment: !binary |
   44OW44Ot44Kw44KS5pu444GN44G+44GX44GfIQ==
 url: http://ebi.dyndns.biz/ipod_touch_iphone/feed/

- lastupdate: 2011-01-13

 comment: !binary |
   44OW44Ot44Kw44KS5pu444GN44G+44GX44GfIQ==
 url: http://d.hatena.ne.jp/ebibibi/rss2

- lastupdate: 2011-01-18

 comment: !binary |
   44OW44Ot44Kw44KS5pu444GN44G+44GX44GfIQ==
 url: http://ebi.dyndns.biz/dokuwiki/feed.php

- lastupdate: 2010-12-21

 comment: !binary |
   c21hcnQuZm3jgafml6XoqJjjgpLmm7jjgY3jgb7jgZfjgZ8h
 url: http://smart.fm/assets/users/ebibibi/journal.rss

>>>