From 6d6650042e80920c4b7ffa918bf72670009fefee Mon Sep 17 00:00:00 2001 From: Bernie Chiu Date: Fri, 8 Nov 2013 17:42:48 +0800 Subject: [PATCH] Add script for NTU GA for grabbing all the bulletins --- .../front_end/bulletins_controller.rb | 2 +- .../front_end/bulletins_feeds_controller.rb | 11 --- .../front_end/bulletins/index.rss.builder | 1 + .../announcement/lib/rss_ntu_ga_aggregate.rb | 96 +++++++++++++++++++ 4 files changed, 98 insertions(+), 12 deletions(-) delete mode 100644 vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_feeds_controller.rb create mode 100644 vendor/built_in_modules/announcement/lib/rss_ntu_ga_aggregate.rb diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb index 647fc0191..10874b62c 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb @@ -67,7 +67,7 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController end end end - + end def show diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_feeds_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_feeds_controller.rb deleted file mode 100644 index 810852339..000000000 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_feeds_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Panel::Announcement::FrontEnd::BulletinFeedsController < OrbitWidgetController - - def rss - @bulletin = Bulletin.all(:order => 'created_at DESC', :limit => 50) - - respond_to do |format| - format.rss { render :layout => false } - end - end - -end diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.rss.builder b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.rss.builder index 2a5d7f85c..a71ba5fe2 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.rss.builder +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.rss.builder @@ -11,6 +11,7 @@ xml.rss :version => "2.0" do for bulletin in @bulletins xml.item do xml.title bulletin.title_translations[I18n.locale.to_s] + xml.category bulletin.bulletin_category.title xml.pubDate bulletin.postdate.to_s(:rfc822) xml.description bulletin.text_translations[I18n.locale.to_s] xml.link url_for(:action=>"show", :controller=>"panel/announcement/front_end/bulletins", :id=>bulletin ,:only_path=>false) diff --git a/vendor/built_in_modules/announcement/lib/rss_ntu_ga_aggregate.rb b/vendor/built_in_modules/announcement/lib/rss_ntu_ga_aggregate.rb new file mode 100644 index 000000000..4afa93a10 --- /dev/null +++ b/vendor/built_in_modules/announcement/lib/rss_ntu_ga_aggregate.rb @@ -0,0 +1,96 @@ +# encoding: utf-8 + +require 'rss' +require 'mongo' + +SITES = { "總務長室暨總務處秘書室" => "sec.ga.ntu.edu.tw", + "總務處文書組" => "doc.ga.ntu.edu.tw", + "總務處事務組" => "general.ga.ntu.edu.tw", + "總務處保管組" => "property.ga.ntu.edu.tw", + "總務處營繕組" => "construction.ga.ntu.edu.tw", + "總務處出納組" => "cashier.ga.ntu.edu.tw", + "總務處採購組" => "procurement.ga.ntu.edu.tw", + "總務處經營管理組" => "fss.ga.ntu.edu.tw", + "總務處駐衛警察隊" => "police.ga.ntu.edu.tw", + "社會科學院總務分處" => "social.ga.ntu.edu.tw", + "醫學院總務分處" => "medicine.ga.ntu.edu.tw" } +DB_BASE_NAME = "production_0" + +yesterday = Time.now - 86400 +recent_feed = {} + +SITES.each do |name, url| + open("http://#{url}/panel/announcement/front_end/bulletins.rss?inner=true") do |rss| + + # Giving false parameter is for skipping irregular format of the RSS + feed = RSS::Parser.parse(rss, false) + + feed.items.each do |item| + category = item.category.to_s.gsub(/\<(\/)*category\>/, '') + if item.pubDate > yesterday + recent_feed[item.title.strip] = { date: item.pubDate, description: item.description.gsub("\r\n", '
').strip, + link: item.link, category: category } + end + + end + end +end + +def get_category_id(category, categories, coll_cat) + if categories.keys.include? "rss_#{category}" + [categories["rss_#{category}"], categories] + else + cat = { + _type: "BulletinCategory", + key: "rss_#{category}", + disable: false, + title: {:zh_tw => category}, + created_at: Time.now, + updated_at: Time.now + } + categories["rss_#{category}"] = result = coll_cat.save(cat) + [result, categories] + end +end + +def get_mongo_and_categories + db = Mongo::Connection.new("localhost", 27017).db("#{DB_BASE_NAME}") + 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 + + [categories, coll_bulletin, coll_cat] +end + +recent_feed.each do |title, content| + + categories, coll_bulletin, coll_cat = get_mongo_and_categories + + content.each_value do |bulletin| + # category_id, categories = get_category_id(bulletin[:category], categories, coll_cat) + # unless coll_bulletin.find_one(rss_link: bulletin[:link]) + # bulletin = { _type: "Bulletin", + # postdate: bulletin[:date], + # created_at: bulletin[:date], + # updated_at: bulletin[:date], + # is_checked: true, + # is_pending: false, + # is_rejected: false, + # bulletin_category_id: category_id, + # title: {:zh_tw => bulletin[:title]}, + # text: {:zh_tw => bulletin[:description]}, + # available_for_zh_tw: true, + # rss_link: bulletin[:link], + # is_top: false, + # is_hot: false, + # is_hidden: false } + + # puts bulletin + # end + end + +end