create a hash rss site list from mongodb

This commit is contained in:
thomaschen 2013-11-18 17:38:11 +08:00
parent c807d8a290
commit 3445cb8e69
2 changed files with 7 additions and 5 deletions

View File

@ -8,10 +8,10 @@ defaults: &defaults
development: development:
<<: *defaults <<: *defaults
database: test_site database: production_0
test: test:
<<: *defaults <<: *defaults
database: test_site database: production_0
# set these environment variables on your prod server # set these environment variables on your prod server
production: production:
# host: <%= ENV['MONGOID_HOST'] %> # host: <%= ENV['MONGOID_HOST'] %>

View File

@ -2,12 +2,14 @@
require 'rss' require 'rss'
require 'mongo' require 'mongo'
#create a hash rss site list
SITES = Hash[ AnnouncementFeed.all.map {|feed| [feed.name, feed.link]} ]
#base on your DB #base on your DB
DB_BASE_NAME = "production_0" DB_BASE_NAME = "production_0"
#create a hash rss site list from mongodb
db = Mongo::Connection.new("localhost", 27017).db("#{DB_BASE_NAME}")
SITES = Hash[ db["announcement_feeds"].find().entries.collect { |f| [ f["name"], f["link"] ]} ]
yesterday = Time.now - 86400 yesterday = Time.now - 86400
two_weeks_ago = Time.new - 60 * 60 * 24 * 14 two_weeks_ago = Time.new - 60 * 60 * 24 * 14
@ -22,7 +24,7 @@ SITES.each do |name, url|
feed.items.each do |item| feed.items.each do |item|
category = item.category.to_s.gsub(/\<(\/)*category\>/, '') category = item.category.to_s.gsub(/\<(\/)*category\>/, '')
if item.pubDate > yesterday if item.pubDate > two_weeks_ago
recent_feed[item.title.strip] = { date: item.pubDate, description: item.description.gsub("\r\n", '<br/>').strip, recent_feed[item.title.strip] = { date: item.pubDate, description: item.description.gsub("\r\n", '<br/>').strip,
link: item.link, category: category, source: name } link: item.link, category: category, source: name }
end end