Created
August 3, 2010 01:19
-
-
Save huacnlee/505660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RSS采集并自动发帖到某网站 | |
require 'mechanize' | |
require 'simple-rss' | |
puts "loading exists guid list..." | |
# 读取已存在的guids | |
old_guids = [] | |
guid_file = File.open('guids.dat',"a+") | |
guid_file.each_line { |line| old_guids << line.gsub("\n",'') } | |
# 登陆到 网站 | |
puts "login to website..." | |
agent = WWW::Mechanize.new | |
page = agent.get("http://renjian.com/dd/index") | |
form = page.forms.first | |
form.username = 'user' | |
form.password = '123123' | |
# 登陆,并进入首页 | |
page_home = form.submit | |
# 取得最新的Feed | |
feed_urls = [ | |
'http://github.com/feeds/mangos/commits/mangos/master', | |
'http://www.javaeye.com/rss/ask', | |
'http://www.javaeye.com/rss/forum', | |
'http://news.163.com/special/00011K6L/rss_newstop.xml', | |
'http://news.163.com/special/00011K6L/rss_gn.xml', | |
'http://news.163.com/special/00011K6L/rss_gj.xml', | |
'http://news.163.com/special/00011K6L/rss_sh.xml', | |
'http://news.163.com/special/00011K6L/rss_war.xml', | |
'http://news.163.com/special/00011K6L/historyrss.xml', | |
'http://sports.163.com/special/00051K7F/rss_sportslq.xml', | |
'http://sports.163.com/special/00051K7F/rss_sportsyc.xml', | |
'http://sports.163.com/special/00051K7F/rss_sportsyj.xml', | |
'http://news.cnblogs.com/rss', | |
'http://www.cnblogs.com/rss', | |
'http://www.cnbeta.com/backend.php', | |
'http://feeds.digg.com/digg/container/technology/popular.rss', | |
'http://feeds.digg.com/digg/topic/programming/popular.rss', | |
'http://rss.sina.com.cn/news/marquee/ddt.xml' | |
] | |
feed_urls.each do |url| | |
puts "read from #{url}" | |
begin | |
feed = SimpleRSS::parse open(url) | |
rescue | |
feed = nil | |
end | |
if feed != nil | |
feeds = [] | |
feed.items.each do |item| | |
# 判断以前有没有发过 | |
if old_guids.index(item.guid) == nil | |
feeds << { | |
"guid" => item.guid, | |
"title" => item.title, | |
"link" => item.link, | |
"summary" => item.content == nil ? item.summary : item.content, | |
"author" => item.author, | |
} | |
end | |
end | |
if feeds.length > 0 | |
puts "save to exist guid list..." | |
new_guids = '' | |
feeds.each { |feed| new_guids += "#{feed['guid']}\n"} | |
guid_file.puts new_guids | |
puts "There have #{feeds.length} feeds will be send." | |
# 分享表单 | |
form_share = page_home.forms[1] | |
feeds.each do |feed| | |
form_share.link_type = "LINK" | |
author = feed['author'] == '' ? "" : "#{feed['author']}:" | |
form_share.text = "#{author}#{feed['title']}" | |
form_share.link_url = feed['link'] | |
form_share.original_url = feed['link'] | |
form_share.link_des = feed['summary'] | |
begin | |
form_share.submit | |
puts "#{feed['title']} submited." | |
rescue | |
puts "errored." | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment