59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
|
#!/usr/bin/env ruby
|
||
|
|
||
|
ENV["RAILS_ENV"] ||= "production"
|
||
|
|
||
|
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
||
|
require File.expand_path('../../config/boot', __FILE__)
|
||
|
require APP_PATH
|
||
|
Rails.application.require_environment!
|
||
|
|
||
|
require "feed-normalizer"
|
||
|
|
||
|
User.where("weblog_feed_url <> ''").each do |u|
|
||
|
begin
|
||
|
s = Sponge.new
|
||
|
data = s.fetch(u.weblog_feed_url, :get, nil, nil, {
|
||
|
"User-Agent" => "Mozilla/5.0 (compatible; Planet Lobsters; " <<
|
||
|
"https://#{Rails.application.domain}/)" })
|
||
|
if !data
|
||
|
raise "no data returned"
|
||
|
end
|
||
|
|
||
|
feed = FeedNormalizer::FeedNormalizer.parse(data)
|
||
|
if !feed
|
||
|
raise "could not parse feed"
|
||
|
end
|
||
|
rescue => e
|
||
|
puts "error updating user #{u.id} from #{u.weblog_feed_url}: #{e}"
|
||
|
# TODO: send the user a private message on failure, probably limit to every
|
||
|
# x tries/days
|
||
|
next
|
||
|
end
|
||
|
|
||
|
feed.entries.each do |e|
|
||
|
w = u.weblogs.where(:uuid => e.id).first
|
||
|
if !w
|
||
|
w = u.weblogs.build(:uuid => e.id)
|
||
|
end
|
||
|
|
||
|
# prevent entries with bogus or future times from dominating the front page
|
||
|
if e.last_updated > Time.now
|
||
|
puts "skipping future entry #{e.id} with time #{e.last_updated}"
|
||
|
next
|
||
|
end
|
||
|
|
||
|
# TODO: compare these manually, AR seems to always mark them dirty even if
|
||
|
# the content is the same
|
||
|
w.created_at = e.last_updated
|
||
|
w.url = e.url
|
||
|
w.title = e.title
|
||
|
w.content = e.content
|
||
|
w.tags = e.categories
|
||
|
|
||
|
w.site_url = feed.url
|
||
|
w.site_title = feed.title
|
||
|
|
||
|
w.save
|
||
|
end
|
||
|
end
|