Skip to content

Instantly share code, notes, and snippets.

@tangblack
Created July 29, 2009 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tangblack/157918 to your computer and use it in GitHub Desktop.
Save tangblack/157918 to your computer and use it in GitHub Desktop.
Feed a rss to plurk new item automatically.
%w(rubygems simple-rss open-uri set mechanize).each { |f| require f }
class RssPlurker
Plurk = 'http://www.plurk.com'
def initialize(user_name, password, source)
@user_name = user_name
@password = password
@source = source
@item_link_set = Set.new
rss = get_rss(@source)
rss.items.each do |i|
@item_link_set << i.link
end
end
def plurk_if_has_new_item
puts 'Plurk if rss has new item.'
plurk_items = []
rss = get_rss(@source)
rss.items.each do |i|
unless @item_link_set.include?(i.link)
@item_link_set << i.link
plurk_items << i
end
end
if (plurk_items.size > 0)
login(@user_name, @password)
plurk_items.each do |i|
new_title = i.title.gsub(/(\(|\))/, '')
puts "Plurk this: #{i.link} (#{new_title})"
add_plurk("#{i.link} (#{new_title})", 'shares')
end
end
end
private
def get_rss source
SimpleRSS.parse open(source)
end
def login(user_name, password)
agent = WWW::Mechanize.new
begin
agent.get(Plurk) do |login_page|
timeline = login_page.form_with(:action => 'https://www.plurk.com/Users/login') do |form|
form.nick_name = user_name
form.password = password
end.submit
/var GLOBAL = \{.*"uid": ([\d]+),.*\}/imu =~ timeline.body
@uid = Regexp.last_match[1]
end
@cookies = agent.cookie_jar
@logged_in = true
rescue
false
end
end
def add_plurk(content="", qualifier="says", limited_to=[], no_comments=false, lang="en")
return false unless @logged_in
no_comments = no_comments ? 1 : 0
params = {
:posted => Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S"),
:qualifier => qualifier,
:content => content[0...140],
:lang => lang,
:uid => @uid,
:no_comments => no_comments
}
params[:limited_to] = "[#{limited_to.join(",")}]" unless limited_to.empty?
request("/TimeLine/addPlurk", :method => :post , :params => params)
end
def request(path, options = {})
begin
agent = WWW::Mechanize.new
agent.cookie_jar = @cookies
case options[:method].to_s
when "get"
agent.get(Plurk + path, options[:params])
when "post"
agent.post(Plurk + path, options[:params])
end
return agent.current_page.body
rescue WWW::Mechanize::ResponseCodeError => ex
raise Unavailable, ex.response_code
end
end
end
def every_n_minutes(n)
seconds = n * 60
loop do
before = Time.now
yield
interval = seconds - (Time.now - before)
if (interval > 0)
sleep(interval)
end
end
end
source = "改成你要餵入的rss連結"
user_name = "改成你的噗浪帳號"
password = "改成你的噗浪密碼"
plurk_bot = RssPlurker.new(user_name, password, source)
every_n_minutes(5) {plurk_bot.plurk_if_has_new_item}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment