From 25c1f191b2eeb24d1e409b5ea9f4032e21db9554 Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Thu, 26 Sep 2013 13:38:27 +0800 Subject: [PATCH] Added read rss2 annoucement feature --- app/jobs/get_remote_announcement.rb | 7 ++++ config/resque_schedule.yml | 12 +++++-- lib/remote_announcement.rb | 53 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 app/jobs/get_remote_announcement.rb create mode 100644 lib/remote_announcement.rb diff --git a/app/jobs/get_remote_announcement.rb b/app/jobs/get_remote_announcement.rb new file mode 100644 index 000000000..6c6f621d6 --- /dev/null +++ b/app/jobs/get_remote_announcement.rb @@ -0,0 +1,7 @@ +class GetRemoteAnnouncement + @queue = :high + + def self.perform() + %x(ruby "#{Rails.root}/lib/remote_announcement.rb") + end +end \ No newline at end of file diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index 83f580122..01979baa1 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -11,7 +11,8 @@ backup_server: description: BackupServer and remove old backups update_tag_cloud: - cron: 0 0 [0,12] * * * + # cron: 0 0 [0,12] * * * + cron: 0 0 0 * * * class: UpdateTagCloud args: description: UpdateTagCloud @@ -23,7 +24,14 @@ generate_system_summary: description: Generate the system status such as disk free space,package version list for showing at site tab email_cron: - cron: 0 0 [10,16,22] * * * + # cron: 0 0 [10,16,22] * * * + cron: 0 0 10 * * * class: EmailCron args: description: EmailCron + +get_remote_announcement: + cron: */5 * * * * * + class: GetRemoteAnnouncement + args: + description: GetRemoteAnnouncement diff --git a/lib/remote_announcement.rb b/lib/remote_announcement.rb new file mode 100644 index 000000000..fb6ef6cf9 --- /dev/null +++ b/lib/remote_announcement.rb @@ -0,0 +1,53 @@ +require 'mongo' +require 'open-uri' +require 'json' +require 'date' + +@db = Mongo::Connection.new("localhost", 27017).db("fgu_orbit") +@coll_bulletin = @db["bulletins"] +@coll_cat = @db["bulletin_categories"] + +@categories = @coll_cat.find().to_a.inject({}) do |categories, category| + categories[category['key']] = category['_id'] + categories +end + +def get_remote_bulletins_json(url) + data = "" + open(url){|f| f.each_line {|line| data << line} } + + bulletins = JSON.parse data + + bulletins.each do |b| + + bulletin = { _type: "Bulletin", + postdate: Time.now, + created_at: Time.now, + updated_at: Time.now, + is_checked: true, + is_pending: false, + is_rejected: false, + bulletin_category_id: @categories["#{b[7]}"], + title: {:zh_tw => b[2]}, + text: {:zh_tw => b[5]}, + available_for_zh_tw: true, + available_for_en: false, + create_user_id: "", + public: true, + sn: b[0], + site: b[9], + is_top: false, + is_hot: false, + is_hidden: false } + + unless @coll_bulletin.find_one(:$and => [{ :sn => bulletin[:sn]},{ :site => bulletin[:site]}]) + @coll_bulletin.save(bulletin) + # puts "Inserted" + else + # puts "Exist" + end + end +end + +get_remote_bulletins_json("http://president.fgu.edu.tw/remote_annoucement/get_news.php") +get_remote_bulletins_json("http://car.fgu.edu.tw/remote_annoucement/get_news.php") \ No newline at end of file