导出Google Reader的Feed条目生成xml的ruby小程序源代码

本主题由 隐藏人物 创建于 2009-10-23 2:23:12


require 'net/https'
require 'uri'

def getSID(email, passwd)
uri = URI.parse('https://www.google.com/accounts/ClientLogin')

req = Net::HTTP:ost.new(uri.path)
req.set_form_data({'Email'=>email, 'Passwd'=>passwd});

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
res = http.start {|h| h.request(req)}

case res
when Net::HTTPSuccess
res.body.split("\n").each { |s|
return s if s[0,4] == 'SID='
}
else
res.error!
end
end

def parseXML(src)
m = src.match('([^<]+)')
if m then
return m[1]
else
return ''
end
end

def exportGRCache(feedurl, email, passwd, fn = '000001', c = '')
sid = getSID(email, passwd)

begin
# n尽量大一点,方便后续处理
url = 'http://www.google.com/reader/atom/feed/' + feedurl + '?n=100'
url += '&c=' + c if c != ''
uri = URI.parse(url)

headers = { 'Cookie' => sid, }
puts 'DL ' + url
res = Net::HTTP.start(uri.host, uri.port) { |h| h.get(uri.request_uri, headers) }

puts 'Save ' + fn + '.xml ...'
f = File.new(fn + '.xml', 'w')
fn.next!
f.write(res.body)

c = parseXML(res.body)
end while c != ''
end

# Google Reader中订阅的Feed地址
url = 'http://BLOG.DOMAIN.com/PATH.xml'

# Google Account,Reader API目前必须要验证才能用
email = 'USERNAME@gmail.com'
passwd = 'USERPASSWORD'

exportGRCache(url, email, passwd)

来自八百里的blog:
http://otnth.blogspot.com/2009/02/export-cached-feed-from-google-reader.html

放心注册,GeeKaa不会虚耗你半点时间,你只会发现更多乐趣。 立即注册