Permalink
Please sign in to comment.
Browse files
fetch and cache text of stories
useful for reading the summary of a page when the site is down, and allows us to show the content of the story in the rss feed as well as making its content searchable. whether this is legal/responsible is up for debate, so this is not being committed on master yet.
- Loading branch information...
Showing
with
83 additions
and 7 deletions.
- +7 −0 app/assets/stylesheets/application.css
- +8 −0 app/models/story.rb
- +3 −0 app/views/home/rss.erb
- +22 −6 app/views/stories/show.html.erb
- +9 −0 db/migrate/20121004153529_add_story_text.rb
- +2 −1 db/schema.rb
- +32 −0 extras/diffbot.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 |
| @@ -0,0 +1,32 @@ | ||
| class Diffbot | ||
| cattr_accessor :API_KEY | ||
| # this needs to be overridden in config/initializers/production.rb | ||
| @@API_KEY = nil | ||
| API_URL = "http://www.diffbot.com/api/article" | ||
| def self.get_url_text(url) | ||
| if !@@API_KEY | ||
| return | ||
| end | ||
| db_url = "#{API_URL}?token=#{@@API_KEY}&url=#{CGI.escape(url)}" | ||
| begin | ||
| s = Sponge.new | ||
| s.timeout = 4 | ||
| 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 |
0 comments on commit
67b61b5