From b641d0232d838ee8df50d60835d35edd2235d8b3 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Fri, 21 Jun 2013 20:38:37 -0500 Subject: [PATCH] sort of merge cache branch but don't do anything automatically allow manual caching of story text using diffbot, if an api key is configured and Story#fetch_story_cache! is called --- app/models/story.rb | 9 +++++- db/migrate/20121004153529_add_story_text.rb | 9 ++++++ db/schema.rb | 1 + extras/story_cacher.rb | 35 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20121004153529_add_story_text.rb create mode 100644 extras/story_cacher.rb diff --git a/app/models/story.rb b/app/models/story.rb index aa2e544..2c50f1d 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -132,7 +132,8 @@ class Story < ActiveRecord::Base end def log_moderation - if self.new_record? || self.editor_user_id == self.user_id + if self.new_record? || !self.editor_user_id || + self.editor_user_id == self.user_id return end @@ -234,6 +235,12 @@ class Story < ActiveRecord::Base @fetched_content end + def fetch_story_cache! + if self.url.present? + self.story_cache = StoryCacher.get_story_text(self.url) + end + end + def calculated_hotness order = Math.log([ score.abs, 1 ].max, 10) if score > 0 diff --git a/db/migrate/20121004153529_add_story_text.rb b/db/migrate/20121004153529_add_story_text.rb new file mode 100644 index 0000000..bf678fc --- /dev/null +++ b/db/migrate/20121004153529_add_story_text.rb @@ -0,0 +1,9 @@ +class AddStoryText < ActiveRecord::Migration + def up + add_column :stories, :story_cache, :text + end + + def down + remove_column :stories, :story_cache + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ec0899..9ef9c72 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -89,6 +89,7 @@ ActiveRecord::Schema.define(:version => 20130526164230) do t.integer "is_moderated", :limit => 1, :default => 0, :null => false t.decimal "hotness", :precision => 20, :scale => 10, :default => 0.0, :null => false t.text "markeddown_description", :limit => 16777215 + t.text "story_cache" end add_index "stories", ["hotness"], :name => "hotness_idx" diff --git a/extras/story_cacher.rb b/extras/story_cacher.rb new file mode 100644 index 0000000..ba78109 --- /dev/null +++ b/extras/story_cacher.rb @@ -0,0 +1,35 @@ +class StoryCacher + cattr_accessor :DIFFBOT_API_KEY + + # this needs to be overridden in config/initializers/production.rb + @@DIFFBOT_API_KEY = nil + + DIFFBOT_API_URL = "http://www.diffbot.com/api/article" + + def self.get_story_text(url) + if !@@DIFFBOT_API_KEY + return + end + + db_url = "#{DIFFBOT_API_URL}?token=#{@@DIFFBOT_API_KEY}&url=" << + CGI.escape(url) + + begin + s = Sponge.new + # we're not doing this interactively, so take a while + s.timeout = 20 + res = s.fetch(db_url) + if res.present? + j = JSON.parse(res) + + # turn newlines into double newlines, so they become paragraphs + return j["text"].gsub("\n", "\n\n") + end + + rescue => e + Rails.logger.error "error fetching #{db_url}: #{e.message}" + end + + nil + end +end