orbit-4-2/app/controllers/admin/import_controller.rb

126 lines
3.3 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2014-02-26 12:30:43 +00:00
require "net/http"
require "uri"
require 'json'
class Admin::ImportController < OrbitBackendController
open_for_admin
2014-02-26 12:30:43 +00:00
def rss2_news
2014-02-27 09:49:22 +00:00
@url = params['url']
2014-02-26 12:30:43 +00:00
uri = URI.parse(@url)
2014-02-27 09:49:22 +00:00
@host = uri.host
2014-02-26 12:30:43 +00:00
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
@user = User.where(:user_id => "rulingcom").first
@data.each do |row|
# next if row['files'].size == 0
2014-02-26 12:30:43 +00:00
bulletin = Bulletin.new
bulletin.title_translations = row['Title']
bulletin.subtitle_translations = row['Summary']
next if row['Title']==[] and row['Summary']==[] and row['Content']==[]
2014-02-26 12:30:43 +00:00
@site_valid_locales.each do |locale|
if row['Content'][locale]=="" and row['Summary'][locale]!=""
row['Content'][locale] = row['Summary'][locale]
row['Summary'][locale] = " "
end
temp = row['Content'][locale]
2014-02-27 09:49:22 +00:00
urls = Nokogiri::HTML(temp).css("img").map do |link|
if URI.parse(link.attr("src")).host == @host
link.attr("src")
end
2014-02-27 09:49:22 +00:00
end
urls.each do |url|
next if url.nil?
a = Asset.new
a.remote_data_url = url
a.assetable_type = "User"
a.assetable_id = @user.id
a.title = {"en" => a["data"], "zh_tw" => a["data"]}
a.save!
temp.sub!(url,a.data.to_s)
end
row['Content'][locale] = temp
end
2014-02-26 12:30:43 +00:00
bulletin.text_translations = row['Content']
2014-02-26 12:30:43 +00:00
bulletin.category_id = @new_category_ids[row["Category"]]
bulletin.view_count = row["Visits"].blank? ? 0 : row["Visits"]
2014-02-26 12:30:43 +00:00
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
if f['url'].split('title=').size == 1
f['url'] = f['url']+"檔案下載"
end
f['title'] = "檔案下載" if f['title'].blank?
2014-02-26 12:30:43 +00:00
bf.remote_file_url = f['url']
bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']}
bf.bulletin_id = bulletin.id
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!
File.delete(file)
2014-02-26 12:30:43 +00:00
end
bulletin.tagged_ids = []
bulletin.save!
end
I18n.locale = l
redirect_to(panel_announcement_back_end_bulletins_url)
2014-02-26 12:30:43 +00:00
end
end