import rss2 news

This commit is contained in:
Harry Bomrah 2014-02-26 20:30:43 +08:00
parent a3cebe78fa
commit edf055b53a
2 changed files with 99 additions and 1 deletions

View File

@ -0,0 +1,96 @@
require "net/http"
require "uri"
require 'json'
class Admin::ImportController < ApplicationController
def rss2_news
@url = "http://192.168.1.10/alreadly_removed/NKUT/NKUT_01/remote_annoucement/get_news.php"
uri = URI.parse(@url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = response.body
data = JSON.parse(data)
@categories = data['categories']
@data = data['data']
@xxx = []
@new_category_ids = {}
l = I18n.locale
I18n.locale = :zh_tw
module_id = ModuleApp.where(:key=>"announcement").first.id
@categories.each do |category|
x = Category.where(:title => category["zh_tw"]).first
if x.nil?
@xxx << category
cat = Category.new
titles = {}
@site_valid_locales.each do |locale|
titles[locale] = category[locale]
end
cat.title_translations = titles
cat.module_app_id = module_id
cat.save!
@new_category_ids[category["id"]] = cat.id
else
@new_category_ids[category["id"]] = x.id
end
end
@regexp = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/
@user = User.where(:user_id => "rulingcom").first
@data.each do |row|
bulletin = Bulletin.new
bulletin.title_translations = row['Title']
bulletin.subtitle_translations = row['Summary']
@site_valid_locales.each do |locale|
temp = row['Content'][locale]
urls = temp.scan(@regexp)
urls.each do |url|
u = url[0]
a = Asset.new
a.remote_data_url = u
a.assetable_type = "User"
a.assetable_id = @user.id
a.title = {"en" => a["data"], "zh_tw" => a["data"]}
a.save!
temp.sub!(u,a.data.to_s)
end
row['Content'][locale] = temp
end
bulletin.text_translations = row['Content']
bulletin.category_id = @new_category_ids[row["Category"]]
bulletin.view_count = row["Visits"]
bulletin.postdate = row["PostDate"]
bulletin.deadline = row['Deadline']
bulletin.remote_image_url = row["Pic"] if row["Pic"]
if row["URL"] && row['URL'] != ""
bl = BulletinLink.new
bl.url = row["URL"]
bl.title_translations = {"en" => "Link", "zh_tw" => "Link"}
bl.bulletin_id = bulletin.id
bl.save!
end
row['files'].each do |f|
bf = BulletinFile.new
bf.remote_file_url = f['url']
bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']}
bf.bulletin_id = bulletin.id
bf.save!
end
bulletin.tagged_ids = []
bulletin.save!
end
I18n.locale = l
render :json => {"success"=>true}.to_json
end
end

View File

@ -279,7 +279,7 @@ Orbit::Application.routes.draw do
match 'template_store/download_theme' => "template_store#download_theme"
match 'template_store/template/:id' => 'template_store#show', :as => :template_store_template
# match 'template_store/cool_method' => "template_store#cool_method"
match "import/rss2_news" => "import#rss2_news"
end
# end admin
@ -351,6 +351,7 @@ Orbit::Application.routes.draw do
match '/desktop_orbit/gettimelinespan' => 'desktop_orbit#gettimelinespan'
match "/facebook/register_fb" => "facebook#register_fb"
match "/facebook/get_friends" => "facebook#get_friends"
match "/facebook/get_feed" => "facebook#get_feed"
match "/facebook/disconnect" => "facebook#disconnect"
@ -362,6 +363,7 @@ Orbit::Application.routes.draw do
#match '/desktop_orbit/eventajaxload' => 'desktop_publications#delete_journal'
# match '/desktop_orbit/eventajaxload' => 'desktop_publications#update_journal'
def controller_paths(controller, actions, scope=nil)
actions.each do |action|
if action.eql? 'index'