dynamic rss sites from AnnouncementFeed
This commit is contained in:
parent
e6b10fbfbd
commit
2663b6f45f
|
@ -1,5 +1,13 @@
|
||||||
<br/>
|
<br/>
|
||||||
<h2><%= t('feed.channel') %></h2>
|
<h2><%= t('feed.channel') %></h2>
|
||||||
|
|
||||||
|
<div class="control-group">
|
||||||
|
<label class="control-label" for=""><%= t('feed.enter')+" RSS "+t('feed.name')%></label>
|
||||||
|
<div class="controls">
|
||||||
|
<%= f.text_field :name, :class=>'span4', :placeholder => 'RSS '+t('feed.name')%>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<label class="control-label" for=""><%= t('feed.enter')+" RSS URL" %></label>
|
<label class="control-label" for=""><%= t('feed.enter')+" RSS URL" %></label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<tbody id="tbody_locations" class="sort-holder">
|
<tbody id="tbody_locations" class="sort-holder">
|
||||||
<% @feeds.each do |feed| %>
|
<% @feeds.each do |feed| %>
|
||||||
<tr class="with_action">
|
<tr class="with_action">
|
||||||
<td><%= feed.link %></td>
|
<td><%= feed.name %></td>
|
||||||
<%if is_manager? %>
|
<%if is_manager? %>
|
||||||
<td><%= link_to t('feed.edit'), edit_panel_feed_back_end_feed_path(feed) %></td>
|
<td><%= link_to t('feed.edit'), edit_panel_feed_back_end_feed_path(feed) %></td>
|
||||||
<td><%= link_to t('feed.cancel'), panel_feed_back_end_feed_path(feed), method: :delete , :confirm => t(:sure?) %></td>
|
<td><%= link_to t('feed.cancel'), panel_feed_back_end_feed_path(feed), method: :delete , :confirm => t(:sure?) %></td>
|
||||||
|
|
|
@ -7,3 +7,4 @@ en:
|
||||||
delete: Delete
|
delete: Delete
|
||||||
cancel: Cancel RSS
|
cancel: Cancel RSS
|
||||||
enter: Enter
|
enter: Enter
|
||||||
|
name: Name
|
|
@ -7,3 +7,4 @@ zh_tw:
|
||||||
delete: 刪除
|
delete: 刪除
|
||||||
cancel: 取消訂閱
|
cancel: 取消訂閱
|
||||||
enter: 輸入
|
enter: 輸入
|
||||||
|
name: 名稱
|
|
@ -0,0 +1,100 @@
|
||||||
|
# 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" }
|
||||||
|
|
||||||
|
SITES = Hash[ AnnouncementFeed.all.map {|feed| [feed.name, feed.link]} ]
|
||||||
|
|
||||||
|
DB_BASE_NAME = "production_0"
|
||||||
|
|
||||||
|
yesterday = Time.now - 86400
|
||||||
|
two_weeks_ago = Time.new - 60 * 60 * 24 * 14
|
||||||
|
|
||||||
|
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", '<br/>').strip,
|
||||||
|
link: item.link, category: category, source: name }
|
||||||
|
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, bulletin|
|
||||||
|
|
||||||
|
categories, coll_bulletin, coll_cat = get_mongo_and_categories
|
||||||
|
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 => title},
|
||||||
|
text: {:zh_tw => bulletin[:description]},
|
||||||
|
available_for_zh_tw: true,
|
||||||
|
rss_link: bulletin[:link],
|
||||||
|
rss_source: bulletin[:source],
|
||||||
|
is_top: false,
|
||||||
|
is_hot: false,
|
||||||
|
is_hidden: false }
|
||||||
|
|
||||||
|
coll_bulletin.save(bulletin)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue