2014-05-30 11:32:51 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
class Admin::ImportController < OrbitAdminController
|
2014-06-03 11:27:34 +00:00
|
|
|
before_action :get_data ,:except=>'index'
|
2014-05-30 11:32:51 +00:00
|
|
|
|
|
|
|
def rss2_news
|
|
|
|
@new_category_ids = {}
|
|
|
|
module_id = ModuleApp.where(:key=>"announcement").first.id
|
|
|
|
|
|
|
|
@categories.each do |category|
|
|
|
|
x = Category.where(:title => category["zh_tw"]).first
|
|
|
|
if x.nil?
|
|
|
|
cat = Category.new
|
|
|
|
titles = {}
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
titles[locale] = category[locale.to_s]
|
|
|
|
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
|
|
|
|
|
|
|
|
@data.each do |row|
|
|
|
|
bulletin = Bulletin.new
|
|
|
|
bulletin.update_user = @user
|
|
|
|
bulletin.title_translations = row['Title']
|
|
|
|
bulletin.subtitle_translations = row['Summary']
|
|
|
|
|
|
|
|
next if row['Title']==[] and row['Summary']==[] and row['Content']==[]
|
|
|
|
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
locale = locale.to_s
|
|
|
|
if row['Content'][locale]=="" and row['Summary'][locale]!=""
|
|
|
|
row['Content'][locale] = row['Summary'][locale]
|
|
|
|
row['Summary'][locale] = " "
|
|
|
|
end
|
|
|
|
|
|
|
|
temp = row['Content'][locale]
|
|
|
|
|
|
|
|
urls = Nokogiri::HTML(temp).css("img").map do |link|
|
|
|
|
|
|
|
|
if URI.parse(link.attr("src")).host == @host
|
|
|
|
link.attr("src")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
urls.each do |url|
|
|
|
|
next if url.nil?
|
|
|
|
a = Asset.new
|
|
|
|
a.remote_data_url = url
|
|
|
|
a.title_translations = {"en" => a.data.filename, "zh_tw" => a.data.filename}
|
|
|
|
a.save!
|
|
|
|
@user.assets << a
|
|
|
|
temp.gsub!(CGI::escapeHTML(url), a.data.to_s)
|
|
|
|
end
|
|
|
|
row['Content'][locale] = temp
|
|
|
|
end
|
|
|
|
|
|
|
|
bulletin.text_translations = row['Content']
|
|
|
|
bulletin.category = Category.find(@new_category_ids[row["Category"]])
|
|
|
|
bulletin.view_count = row["Visits"].blank? ? 0 : 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.save!
|
|
|
|
bulletin.bulletin_links << bl
|
|
|
|
end
|
|
|
|
row['files'].each do |f|
|
|
|
|
bf = BulletinFile.new
|
|
|
|
if f['url'].split('title=').size == 1
|
|
|
|
f['url'] = f['url']+"檔案下載"
|
|
|
|
end
|
|
|
|
|
|
|
|
f['title'] = "檔案下載" if f['title'].blank?
|
|
|
|
|
|
|
|
bf.remote_file_url = f['url']
|
|
|
|
bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']}
|
|
|
|
bf.save!
|
|
|
|
|
|
|
|
# Rename uploaded file
|
|
|
|
file_ext = File.extname(f['url'].split('&')[0])
|
|
|
|
file = File.new("tmp/uploads/#{bf.title}#{file_ext}","w+b")
|
|
|
|
file.write(bf.file.read)
|
|
|
|
bf.file.cache!(file)
|
|
|
|
bf.save!
|
|
|
|
|
|
|
|
bulletin.bulletin_files << bf
|
|
|
|
|
|
|
|
File.delete(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
bulletin.save!
|
|
|
|
end
|
|
|
|
|
2014-06-03 11:27:34 +00:00
|
|
|
I18n.locale = @org_locale
|
2014-05-30 11:32:51 +00:00
|
|
|
|
|
|
|
redirect_to('/admin/announcements')
|
|
|
|
end
|
2014-06-03 11:27:34 +00:00
|
|
|
|
|
|
|
def rss2_archive
|
|
|
|
unless @site_url.nil?
|
|
|
|
module_app = ModuleApp.where(:key=>"archive").first
|
|
|
|
|
|
|
|
@new_categories = {}
|
|
|
|
@categories.each do |id, category|
|
|
|
|
old_cate = Category.where(:title => category['title']["zh-tw"]).first
|
|
|
|
if old_cate.nil?
|
|
|
|
cate = Category.new
|
|
|
|
titles = {}
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
titles[locale] = category['title'][locale.to_s.gsub('_', '-')]
|
|
|
|
end
|
|
|
|
cate.title_translations = titles
|
|
|
|
cate.save!
|
|
|
|
module_app.categories << cate
|
|
|
|
@new_categories[id] = cate.id
|
|
|
|
else
|
|
|
|
@new_categories[id] = old_cate.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
I18n.locale = :en
|
|
|
|
|
|
|
|
@data.each do |id, file|
|
|
|
|
titles = {}
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
titles[locale] = file['title'][locale.to_s.gsub('_', '-')]
|
|
|
|
end
|
|
|
|
|
|
|
|
the_file = ArchiveFileMultiple.new({
|
|
|
|
:file_title_translations=>titles,
|
|
|
|
:choose_lang => @site_valid_locales,
|
|
|
|
:remote_file_url => @site_url+file['url'],
|
|
|
|
:sort_number=>file['sort_number']
|
|
|
|
})
|
|
|
|
the_file.save
|
|
|
|
|
|
|
|
archive_file = ArchiveFile.new({
|
|
|
|
:title_translations=>titles,
|
|
|
|
:sort_number=>file['sort_number']
|
|
|
|
})
|
|
|
|
archive_file.category = Category.find(@new_categories[file["category"]])
|
|
|
|
|
|
|
|
archive_file.create_user_id = current_user.id
|
|
|
|
archive_file.update_user_id = current_user.id
|
|
|
|
|
|
|
|
archive_file.save
|
|
|
|
archive_file.archive_file_multiples << the_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
I18n.locale = @org_locale
|
|
|
|
|
|
|
|
redirect_to admin_archive_files_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_data
|
|
|
|
@org_locale = I18n.locale
|
|
|
|
I18n.locale = :zh_tw
|
|
|
|
|
|
|
|
@site_url = params['site_url']
|
|
|
|
@url = params['url']
|
|
|
|
uri = URI.parse(@url)
|
|
|
|
|
|
|
|
@host = uri.host
|
|
|
|
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']
|
|
|
|
|
|
|
|
@user = User.where(:user_name => "rulingcom").first
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
end
|