-
-
Save tkawa/5891501 to your computer and use it in GitHub Desktop.
Fetch full Atom feed from Google Reader
This file contains hidden or 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
require './reader' | |
reader = Reader.new '[email protected]', PASSWORD | |
# fetch only unread items | |
feed = reader.fetch_all(FEED_URL, true) do |entry| | |
# something with each entry | |
end | |
puts feed |
This file contains hidden or 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
source 'https://rubygems.org' | |
gem 'curb' | |
gem 'nokogiri' | |
gem 'addressable' |
This file contains hidden or 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
require 'curb' | |
require 'nokogiri' | |
require 'addressable/uri' | |
require 'addressable/template' | |
class Reader | |
LoginUrl = Addressable::URI.parse('https://www.google.com/accounts/ClientLogin') | |
FeedUrl = Addressable::Template.new 'https://www.google.com/reader/atom/feed/{feed}?n={n}&c={c}&ck={ck}' | |
Params = { :n => 1000, :client => 'ruby', :xt => 'user/-/state/com.google/read' } | |
attr_reader :curl | |
def initialize(email, password) | |
@email, @password = email, password | |
end | |
class Error < StandardError | |
attr_accessor :curl | |
end | |
def login | |
login_url = LoginUrl.dup | |
login_url.query_values = { | |
:Email => @email, :Passwd => @password, | |
:service => 'reader', :source => 'ruby', | |
:continue => 'https://www.google.com/' | |
} | |
@curl_login = Curl::Easy.new(login_url) | |
@curl_login.perform | |
auth_data = @curl_login.body_str.split("\n").inject({}) { |hash, line| | |
key, value = line.split('=') | |
hash[key] = value | |
hash | |
} | |
@curl = Curl::Easy.new do |c| | |
c.headers['Authorization'] = "GoogleLogin auth=#{auth_data['Auth']}" | |
end | |
@curl.encoding = 'gzip,deflate' | |
end | |
def fetch_all(feed_url, unread = false, &block) | |
continuation = nil | |
timestamp = Time.now | |
main = nil | |
begin | |
doc = fetch(feed_url, unread, timestamp, continuation) | |
doc.at_xpath('//gr:continuation').tap { |el| continuation = el && el.text } | |
entries = doc.xpath('//xmlns:entry') | |
if main.nil? | |
main = doc | |
else | |
parent = main.at_xpath('./xmlns:feed') | |
entries.each { |entry| parent.add_child entry } | |
end | |
entries.each(&block) if block_given? | |
end while continuation | |
main | |
end | |
def fetch(feed_url, unread = false, timestamp = Time.now, continuation = nil) | |
params = Params.merge :ck => timestamp.utc.to_i, :feed => feed_url.to_s | |
params[:c] = continuation if continuation | |
params.delete(:xt) unless unread | |
login | |
@curl.url = FeedUrl.expand(params) | |
perform_request | |
Nokogiri::XML @curl.body_str, nil, 'utf-8' | |
end | |
def perform_request | |
p @curl.url.to_s | |
@curl.perform | |
unless @curl.response_code == 200 | |
raise @curl.header_str | |
end | |
@curl | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment