65 lines
2.7 KiB
Ruby
65 lines
2.7 KiB
Ruby
#!/usr/bin/env ruby
|
|
ENV['RAILS_ENV'] = ARGV[0] || 'production'
|
|
require File.dirname(__FILE__) + '/../config/environment'
|
|
require 'open-uri'
|
|
|
|
class GoogleReaderClient
|
|
def initialize
|
|
@header = {
|
|
"UserAgent" => "ITeye Reader",
|
|
"Referer" => "http://www.iteye.com",
|
|
"Cookie" => "SID=#{get_sid('robbinfankai', 'javaeye3.0')}",
|
|
"Authorization" => "GoogleLogin auth=#{get_auth('robbinfankai', 'javaeye3.0')}"
|
|
}
|
|
end
|
|
|
|
def fetch(feed)
|
|
open("https://www.google.com/reader/atom/feed/#{CGI.escape feed.feed_url}?r=o&n=44444&ot=#{feed.fetched_at.to_i}", @header) do |response|
|
|
puts "Starting fetch #{feed.feed_url}"
|
|
feed.update_attribute(:fetched_at, Time.now)
|
|
(Nokogiri::XML(response)/"entry").each do |entry|
|
|
begin
|
|
google_id = entry.at("id").inner_html
|
|
title = CGI::unescapeHTML entry.at("title").content
|
|
link = entry.at("link")['href']
|
|
body = Dryopteris.sanitize CGI::unescapeHTML((entry.at("summary") || entry.at("content")).content)
|
|
published_at = Time.xmlschema(entry.at("published").inner_html)
|
|
modified_at = Time.xmlschema(entry.at("updated").inner_html)
|
|
|
|
article = FetchedArticle.find_by_feed_id_and_google_id!(feed.id, google_id) rescue Article.find_by_feed_id_and_google_id(feed.id, google_id)
|
|
if article.nil?
|
|
article = FetchedArticle.create(:feed_id => feed.id, :google_id => google_id, :title => title, :link => link, :body => body, :published_at => published_at, :modified_at => modified_at)
|
|
article.approve!(:sys_category_id => feed.sys_category_id, :sys_tag_id => feed.sys_tag_id) if feed.auto_pass?
|
|
puts "fetched #{article.link}"
|
|
elsif modified_at != article.modified_at
|
|
article.update_attributes!(:title => title, :link => link, :body => body, :published_at => published_at, :modified_at => modified_at)
|
|
puts "updated #{article.link}"
|
|
end
|
|
rescue
|
|
next
|
|
end
|
|
end#end Nokogiri
|
|
end
|
|
rescue Exception => e
|
|
puts "[Error] #{feed.feed_url} #{e.message}"
|
|
end
|
|
|
|
private
|
|
def get_sid(login, password)
|
|
open("https://www.google.com/accounts/ClientLogin?Email=#{login}&Passwd=#{password}") { |res| res.first.strip.sub(/^SID=/,'') }
|
|
end
|
|
|
|
def get_auth(login, password)
|
|
open("https://www.google.com/accounts/ClientLogin?Email=#{login}&Passwd=#{password}&service=reader") { |res| res.read.strip.sub(/^SID=.*Auth=/m,'') }
|
|
end
|
|
end
|
|
|
|
begin
|
|
timeout 1 do
|
|
client = GoogleReaderClient.new
|
|
Feed.find_all_by_status(Feed::APPROVED).each { |feed| client.fetch(feed) }
|
|
end
|
|
rescue TypeError, SocketError, SystemCallError, IOError, Timeout::Error => err
|
|
puts "Fetch google reader error: #{err}"
|
|
end
|