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
This commit is contained in:
parent
68690647ab
commit
b641d0232d
|
@ -132,7 +132,8 @@ class Story < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_moderation
|
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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -234,6 +235,12 @@ class Story < ActiveRecord::Base
|
||||||
@fetched_content
|
@fetched_content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_story_cache!
|
||||||
|
if self.url.present?
|
||||||
|
self.story_cache = StoryCacher.get_story_text(self.url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def calculated_hotness
|
def calculated_hotness
|
||||||
order = Math.log([ score.abs, 1 ].max, 10)
|
order = Math.log([ score.abs, 1 ].max, 10)
|
||||||
if score > 0
|
if score > 0
|
||||||
|
|
9
db/migrate/20121004153529_add_story_text.rb
Normal file
9
db/migrate/20121004153529_add_story_text.rb
Normal file
|
@ -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
|
|
@ -89,6 +89,7 @@ ActiveRecord::Schema.define(:version => 20130526164230) do
|
||||||
t.integer "is_moderated", :limit => 1, :default => 0, :null => false
|
t.integer "is_moderated", :limit => 1, :default => 0, :null => false
|
||||||
t.decimal "hotness", :precision => 20, :scale => 10, :default => 0.0, :null => false
|
t.decimal "hotness", :precision => 20, :scale => 10, :default => 0.0, :null => false
|
||||||
t.text "markeddown_description", :limit => 16777215
|
t.text "markeddown_description", :limit => 16777215
|
||||||
|
t.text "story_cache"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "stories", ["hotness"], :name => "hotness_idx"
|
add_index "stories", ["hotness"], :name => "hotness_idx"
|
||||||
|
|
35
extras/story_cacher.rb
Normal file
35
extras/story_cacher.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue