Orbit/lib/remote_announcement.rb

88 lines
3.4 KiB
Ruby
Raw Normal View History

2013-09-26 05:38:27 +00:00
require 'mongo'
require 'open-uri'
require 'json'
require 'date'
@db = Mongo::Connection.new("localhost", 27017).db("test_site")
2013-09-26 05:38:27 +00:00
@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
2013-09-26 05:38:27 +00:00
end
def get_remote_bulletins_json(url)
data = ""
open(url){|f| f.each_line {|line| data << line} }
2013-09-26 05:38:27 +00:00
bulletins = JSON.parse data
2013-09-26 05:38:27 +00:00
bulletins.each do |b|
2013-09-26 05:38:27 +00:00
lang = b[1]=='zh-tw' ? "zh_tw".to_sym : "en".to_sym
next if b[2]==""
next if @categories["#{b[7]}"].nil?
bulletin = { _type: "Bulletin",
postdate: Time.parse(b[8]),
created_at: Time.parse(b[8]),
updated_at: Time.parse(b[8]),
2013-09-26 05:38:27 +00:00
is_checked: true,
is_pending: false,
is_rejected: false,
bulletin_category_id: @categories["#{b[7]}"],
title: {lang => b[2]},
text: {lang => b[5]},
2013-09-26 05:38:27 +00:00
available_for_zh_tw: true,
available_for_en: true,
2013-09-26 05:38:27 +00:00
create_user_id: "",
public: true,
sn: b[0],
site: b[9],
site_id: b[10],
2013-09-26 05:38:27 +00:00
is_top: false,
is_hot: false,
is_hidden: false }
this_bulletin = @coll_bulletin.find_one(:$and => [{ :sn => bulletin[:sn]},{ :site => bulletin[:site]}])
# p bulletin
unless this_bulletin
@coll_bulletin.save(bulletin)
# puts "Inserted "+lang.to_s+" "+bulletin[:title][lang]
# p bulletin
2013-09-26 05:38:27 +00:00
else
@coll_bulletin.update(
{"_id"=>this_bulletin["_id"]},
{
:_type => "Bulletin",
:postdate => bulletin[:postdate],
:created_at => bulletin[:created_at],
:updated_at => bulletin[:updated_at],
:is_checked => this_bulletin["is_checked"],
:is_pending => this_bulletin["is_pending"],
:is_rejected => this_bulletin["is_rejected"],
:bulletin_category_id => bulletin[:bulletin_category_id],
:title => bulletin[:title],
:text => bulletin[:text],
:available_for_zh_tw => this_bulletin["available_for_zh_tw"],
:available_for_en => this_bulletin["available_for_en"],
:create_user_id => "",
:public => this_bulletin["public"],
:sn => bulletin[:sn],
:site => bulletin[:site],
:site_id => bulletin[:site_id],
:is_top => this_bulletin["is_top"],
:is_hot => this_bulletin["is_hot"],
:is_hidden => this_bulletin["is_hidden"]
})
# puts "Updated "+lang.to_s+" "+bulletin[:title][lang]
# p this_bulletin["_id"]
2013-09-26 05:38:27 +00:00
end
end
2013-09-26 05:38:27 +00:00
end
get_remote_bulletins_json("http://president.fgu.edu.tw/remote_annoucement/get_news2.php")
get_remote_bulletins_json("http://car.fgu.edu.tw/remote_annoucement/get_news2.php")