以前は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

defload_data
result_array=Array.new()
filename="feeds.yml"
feeds=YAML.load_file(filename)
feeds.eachdo|feed|
putsfeed['url']
rss=open(feed['url']){|file|RSS::Parser.parse(file.read)}
lastupdate=feed['lastupdate']
rss.items.eachdo|item|
date=Date.parse(item.date.to_s)
iflastupdate==nillastupdate<date
short_url=shorten(item.link)
result_array"#{feed['comment']}/#{item.title}"
iffeed['lastupdate']==nilfeed['lastupdate']<date
feed['lastupdate']=date
end
end
end
end
#yml
open(filename,'w'){|f|f.putsfeeds.to_yaml}
#Array
putsresult_array.reverse
result_array.reverse
end
defshorten(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|
returnvalue['shortUrl']
}
end

end

SELFDIR = File.dirname(__FILE__) ARGV.each do |mode|

RSS_Twitt.newmode,"#{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