2014-05-30 11:32:51 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require "net/http"
|
|
|
|
require "uri"
|
|
|
|
require 'json'
|
2014-06-30 08:40:31 +00:00
|
|
|
require 'yaml'
|
2014-05-30 11:32:51 +00:00
|
|
|
|
|
|
|
class Admin::ImportController < OrbitAdminController
|
2014-06-06 04:22:55 +00:00
|
|
|
layout "structure"
|
2014-06-11 07:16:32 +00:00
|
|
|
before_filter :get_user
|
2014-06-06 04:22:55 +00:00
|
|
|
|
2014-06-11 07:16:32 +00:00
|
|
|
@@import_stats = {}
|
|
|
|
@@thread = nil
|
2014-06-06 04:22:55 +00:00
|
|
|
def check_url
|
|
|
|
params['url'] = params['url']+"/" if params['url'].last!="/"
|
|
|
|
uri = URI.parse(params['url'])
|
|
|
|
|
|
|
|
begin
|
|
|
|
@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)
|
|
|
|
msg = data['status']
|
|
|
|
rescue
|
|
|
|
msg = "Failed to connect to RSS2 (#{uri.to_s})"
|
|
|
|
end
|
|
|
|
|
|
|
|
render :json=>{"status"=>msg}
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
def module_categories
|
|
|
|
module_app = ModuleApp.find_by(:key=>params['module'])
|
|
|
|
uri = URI.parse(params['url'])
|
|
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
request = Net::HTTP::Get.new(uri.request_uri)
|
|
|
|
response = http.request(request)
|
|
|
|
data = response.body
|
|
|
|
categories = JSON.parse(data)
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
I18n.locale = :zh_tw
|
|
|
|
categories.each do |id, category|
|
|
|
|
old_cate = module_app.categories.where(:title => category["zh_tw"]).first
|
|
|
|
if old_cate.nil?
|
2014-05-30 11:32:51 +00:00
|
|
|
cat = Category.new
|
2014-06-06 04:22:55 +00:00
|
|
|
cat.title_translations = category
|
|
|
|
cat.save!
|
|
|
|
module_app.categories << cat
|
|
|
|
categories[id]['id'] = cat.id.to_s
|
2014-05-30 11:32:51 +00:00
|
|
|
else
|
2014-06-06 04:22:55 +00:00
|
|
|
categories[id]['id'] = old_cate.id.to_s
|
2014-05-30 11:32:51 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-06 04:22:55 +00:00
|
|
|
render :json=>categories
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
def module_data_list
|
|
|
|
uri = URI.parse(params['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)
|
|
|
|
render :json=>data
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
def module_data
|
|
|
|
uri = URI.parse(params['url'])
|
2014-06-19 08:50:11 +00:00
|
|
|
@host = uri.host
|
2014-06-06 04:22:55 +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)
|
|
|
|
|
|
|
|
case params['module']
|
|
|
|
when 'archive'
|
|
|
|
import_archive(data)
|
|
|
|
when 'announcement'
|
|
|
|
import_announcement(data)
|
|
|
|
end
|
|
|
|
render :json=>['status'=>'ok']
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
def import_archive(file)
|
|
|
|
archive_file = ArchiveFile.where(:rss2_sn=>file['Sn']).first
|
|
|
|
if archive_file.nil?
|
|
|
|
archive_file = ArchiveFile.new
|
|
|
|
archive_file.rss2_sn = file['Sn']
|
2014-06-17 03:45:08 +00:00
|
|
|
else
|
|
|
|
archive_file.archive_file_multiples.destroy_all
|
2014-06-06 04:22:55 +00:00
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
archive_file.title_translations = file['title']
|
|
|
|
archive_file.sort_number = file['sort_number']
|
|
|
|
archive_file.category = Category.find(params["category"])
|
|
|
|
archive_file.create_user_id = current_user.id
|
|
|
|
archive_file.update_user_id = current_user.id
|
|
|
|
archive_file.save
|
|
|
|
|
|
|
|
if(file['url'])
|
|
|
|
archive = ArchiveFileMultiple.new({
|
|
|
|
:file_title_translations=>file['title'],
|
|
|
|
:choose_lang => @site_valid_locales,
|
|
|
|
:remote_file_url => file['url'],
|
|
|
|
:sort_number=>file['sort_number']
|
|
|
|
})
|
|
|
|
archive.save
|
|
|
|
archive_file.archive_file_multiples << archive
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
if(file['url2'])
|
|
|
|
archive = ArchiveFileMultiple.new({
|
|
|
|
:file_title_translations=>file['title'],
|
|
|
|
:choose_lang => @site_valid_locales,
|
|
|
|
:remote_file_url => file['url2'],
|
|
|
|
:sort_number=>file['sort_number']
|
|
|
|
})
|
|
|
|
archive.save
|
|
|
|
archive_file.archive_file_multiples << archive
|
|
|
|
end
|
|
|
|
end
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
def import_announcement(news)
|
|
|
|
bulletin = Bulletin.where(:rss2_sn=>news['Sn']).first
|
|
|
|
if bulletin.nil?
|
|
|
|
bulletin = Bulletin.new
|
|
|
|
bulletin.rss2_sn = news['Sn']
|
2014-06-17 03:45:08 +00:00
|
|
|
else
|
|
|
|
bulletin.bulletin_links.destroy_all
|
|
|
|
bulletin.bulletin_files.destroy_all
|
2014-06-06 04:22:55 +00:00
|
|
|
end
|
|
|
|
bulletin.update_user = current_user
|
|
|
|
bulletin.title_translations = news['Title']
|
|
|
|
bulletin.subtitle_translations = news['Summary']
|
|
|
|
|
|
|
|
@site_valid_locales.each do |locale|
|
|
|
|
locale = locale.to_s
|
|
|
|
if news['Content'][locale]=="" and news['Summary'][locale]!=""
|
|
|
|
news['Content'][locale] = news['Summary'][locale]
|
|
|
|
news['Summary'][locale] = " "
|
2014-05-30 11:32:51 +00:00
|
|
|
end
|
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
temp = news['Content'][locale]
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
urls = Nokogiri::HTML(temp).css("img").map do |link|
|
2014-05-30 11:32:51 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
if URI.parse(link.attr("src")).host == @host
|
|
|
|
link.attr("src")
|
2014-05-30 11:32:51 +00:00
|
|
|
end
|
2014-06-03 11:27:34 +00:00
|
|
|
end
|
2014-06-06 04:22:55 +00:00
|
|
|
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)
|
2014-06-03 11:27:34 +00:00
|
|
|
end
|
2014-06-06 04:22:55 +00:00
|
|
|
news['Content'][locale] = temp
|
|
|
|
end
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
bulletin.text_translations = news['Content']
|
|
|
|
bulletin.category = Category.find(params["category"])
|
|
|
|
bulletin.view_count = news["Visits"].blank? ? 0 : news["Visits"]
|
|
|
|
bulletin.postdate = news["PostDate"]
|
|
|
|
bulletin.deadline = news['Deadline']
|
|
|
|
bulletin.remote_image_url = news["Pic"] if news["Pic"]
|
|
|
|
|
|
|
|
if news["URL"] && news['URL'] != ""
|
|
|
|
bl = BulletinLink.new
|
|
|
|
bl.url = news["URL"]
|
|
|
|
bl.title_translations = {"en" => "Link", "zh_tw" => "Link"}
|
|
|
|
bl.save!
|
|
|
|
bulletin.bulletin_links << bl
|
2014-06-03 11:27:34 +00:00
|
|
|
end
|
2014-06-06 04:22:55 +00:00
|
|
|
news['files'].each do |f|
|
|
|
|
bf = BulletinFile.new
|
|
|
|
if f['url'].split('title=').size == 1
|
|
|
|
f['url'] = f['url']+"檔案下載"
|
|
|
|
end
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
f['title'] = "檔案下載" if f['title'].blank?
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
bf.remote_file_url = f['url']
|
|
|
|
bf.title_translations = {"en" => f['title'], "zh_tw" => f['title']}
|
|
|
|
bf.save!
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
# 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!
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
bulletin.bulletin_files << bf
|
|
|
|
|
|
|
|
File.delete(file)
|
|
|
|
end
|
2014-06-03 11:27:34 +00:00
|
|
|
|
2014-06-06 04:22:55 +00:00
|
|
|
bulletin.save!
|
2014-06-03 11:27:34 +00:00
|
|
|
end
|
2014-06-11 07:16:32 +00:00
|
|
|
|
|
|
|
def rss2_pages
|
2014-06-30 08:40:31 +00:00
|
|
|
delete_import_file
|
2014-06-11 07:16:32 +00:00
|
|
|
uri = URI.parse(params['url'])
|
2014-06-30 08:40:31 +00:00
|
|
|
# @@thread = Thread.new do
|
2014-06-11 07:16:32 +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)
|
|
|
|
pages = data["pages"]
|
2014-06-30 08:40:31 +00:00
|
|
|
@@import_stats["total_pages"] = data["total_pages"].to_i
|
|
|
|
@@import_stats["current_status"] = 0
|
|
|
|
@@import_stats["success"] = true
|
|
|
|
write_to_file
|
2014-06-11 07:16:32 +00:00
|
|
|
pages.each do |page|
|
|
|
|
@@import_stats["current_page"] = page["page_id"]
|
|
|
|
@@import_stats["current_page_name"] = page["title"]["zh_tw"]
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-11 07:16:32 +00:00
|
|
|
if Page.where(:rss2_id => page["page_id"]).count == 0
|
|
|
|
page_id = page["page_id"]
|
|
|
|
number = 0
|
|
|
|
while page_present?page_id
|
|
|
|
page_id = page_id + "_" + (number + 1).to_s
|
|
|
|
end
|
|
|
|
p = Page.new(:page_id => page_id, :parent_page_id => Page.root.id, :page_type => "page", :url => "/#{page_id}", :module => "page_content", :enabled_for => ["en", "zh_tw"], :menu_enabled_for => ["en", "zh_tw"], :enabled_for_sitemap => ["en","zh_tw"], :name_translations => {"en"=>page["title"]["en"], "zh_tw" => page["title"]["zh_tw"]},:rss2_id => page["page_id"])
|
|
|
|
p.save
|
|
|
|
en = smart_downloader(page["content"]["en"],data["site_url"])
|
|
|
|
zh_tw = smart_downloader(page["content"]["zh_tw"],data["site_url"])
|
|
|
|
page_context = PageContext.new(:content_translations => {"en" => en,"zh_tw" => zh_tw}, :version => 1, :update_user_id => current_user.id, :page_id => p.id)
|
|
|
|
page_context.save
|
|
|
|
else
|
|
|
|
p = Page.find_by(:rss2_id => page["page_id"])
|
|
|
|
page_id = p.page_id
|
|
|
|
end
|
2014-06-30 08:40:31 +00:00
|
|
|
@@import_stats["current_status"] = @@import_stats["current_status"] + 1
|
|
|
|
write_to_file
|
2014-06-11 07:16:32 +00:00
|
|
|
if !page["childpages"].blank?
|
|
|
|
childpages = page["childpages"]
|
|
|
|
childpages.each_with_index do |childpage,i|
|
|
|
|
@@import_stats["current_page"] = childpage["page_id"]
|
|
|
|
@@import_stats["current_page_name"] = page["title"]["zh_tw"]
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-11 07:16:32 +00:00
|
|
|
if Page.where(:rss2_id => childpage["page_id"]).count == 0
|
|
|
|
p_id = page_id + "_" + i.to_s
|
|
|
|
case childpage["type"]
|
|
|
|
when "page"
|
2014-06-27 09:08:03 +00:00
|
|
|
cp = Page.new(:page_id => p_id, :parent_page_id => p.id, :page_type => "page", :url => "/#{p.page_id}/#{p_id}", :module => "page_content", "enabled_for" => ["en", "zh_tw"], :menu_enabled_for => ["en", "zh_tw"], :enabled_for_sitemap => ["en","zh_tw"], :name_translations => {"en"=>childpage["title"]["en"], "zh_tw" => childpage["title"]["zh_tw"]},:rss2_id => childpage["page_id"])
|
2014-06-11 07:16:32 +00:00
|
|
|
cp.save
|
|
|
|
en = smart_downloader(childpage["content"]["en"],data["site_url"])
|
|
|
|
zh_tw = smart_downloader(childpage["content"]["zh_tw"],data["site_url"])
|
|
|
|
page_context = PageContext.new(:content_translations => {"en" => en,"zh_tw" => zh_tw}, :version => 1, :update_user_id => current_user.id, :page_id => cp.id)
|
|
|
|
page_context.save
|
|
|
|
when "exturl"
|
2014-06-27 09:08:03 +00:00
|
|
|
cp = Page.new(:page_id => p_id, :parent_page_id => p.id, :page_type => "link", "url" => "/#{p.page_id}/#{p_id}", :enabled_for => ["en", "zh_tw"], :menu_enabled_for => ["en", "zh_tw"], :enabled_for_sitemap => ["en","zh_tw"], :name_translations => {"en"=>childpage["title"]["en"], "zh_tw" => childpage["title"]["zh_tw"]}, :external_url_translations => {"en" => childpage["url"], "zh_tw" => childpage["url"]},:rss2_id => childpage["page_id"])
|
2014-06-11 07:16:32 +00:00
|
|
|
cp.save
|
|
|
|
when "file"
|
|
|
|
asset = Asset.new
|
|
|
|
asset.remote_data_url = childpage["filename"]
|
|
|
|
asset.title_translations = {"en" => childpage["file_title"], "zh_tw" =>childpage["file_title"]}
|
|
|
|
asset.save!
|
|
|
|
@user.assets << asset
|
|
|
|
@user.save
|
|
|
|
external_url = asset.data.url
|
2014-06-27 09:08:03 +00:00
|
|
|
cp = Page.new(:page_id => p_id, :parent_page_id => p.id, :page_type => "link", "url" => "/#{p.page_id}/#{p_id}", :enabled_for => ["en", "zh_tw"], :menu_enabled_for => ["en", "zh_tw"], :enabled_for_sitemap => ["en","zh_tw"], :name_translations => {"en"=>childpage["title"]["en"], "zh_tw" => childpage["title"]["zh_tw"]}, :external_url_translations => {"en" => external_url, "zh_tw" => external_url},:rss2_id => childpage["page_id"])
|
2014-06-11 07:16:32 +00:00
|
|
|
cp.save
|
|
|
|
end
|
|
|
|
end
|
2014-06-30 08:40:31 +00:00
|
|
|
@@import_stats["current_status"] = @@import_stats["current_status"] + 1
|
|
|
|
write_to_file
|
2014-06-11 07:16:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-30 08:40:31 +00:00
|
|
|
# end
|
|
|
|
# @@thread.join
|
|
|
|
# @@thread.abort_on_exception = true
|
2014-06-11 07:16:32 +00:00
|
|
|
render :json => {"success"=>true}.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_import_status
|
2014-06-30 08:40:31 +00:00
|
|
|
settings = get_settings_from_import_file
|
|
|
|
render :json => settings.to_json
|
2014-06-13 08:16:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rss2_galleries
|
2014-06-30 08:40:31 +00:00
|
|
|
delete_import_file
|
2014-06-13 08:16:07 +00:00
|
|
|
uri = URI.parse(params['url'])
|
2014-06-26 11:14:46 +00:00
|
|
|
# @@thread = Thread.new do
|
2014-06-13 08:16:07 +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)
|
|
|
|
@@import_stats["total_images"] = data["total_images"].to_i
|
|
|
|
@@import_stats["current_status"] = 0
|
2014-06-30 08:40:31 +00:00
|
|
|
@@import_stats["success"] = true
|
|
|
|
write_to_file
|
2014-06-13 08:16:07 +00:00
|
|
|
albums = data["albums"]
|
|
|
|
current_locale = I18n.locale
|
|
|
|
I18n.locale = :en
|
|
|
|
category = Category.where(:title => "RSS2 Gallery").first
|
|
|
|
I18n.locale = current_locale
|
|
|
|
if category.nil?
|
|
|
|
module_app = ModuleApp.find_by_key("gallery")
|
|
|
|
category = Category.new
|
|
|
|
category.title_translations = {"en" => "RSS2 Gallery", "zh_tw" => "RSS2畫廊"}
|
|
|
|
category.save
|
|
|
|
module_app.categories << category
|
|
|
|
end
|
|
|
|
albums.each do |album|
|
|
|
|
@@import_stats["current_album_id"] = album["albumid"]
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-13 08:16:07 +00:00
|
|
|
if Album.where(:rss2_id => album["albumid"]).count == 0
|
|
|
|
@@import_stats["current_album_name"] = album["name"]["zh_tw"]
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-13 08:16:07 +00:00
|
|
|
newalbum = Album.new(:name_translations => {"en" => album["name"]["en"], "zh_tw" => album["name"]["zh_tw"]}, :description_translations => {"en" => album["description"]["en"], "zh_tw" => album["description"]["zh_tw"]},"rss2_id" => album["albumid"])
|
|
|
|
newalbum.category_id = category.id
|
|
|
|
newalbum.save
|
|
|
|
images = album["images"]
|
|
|
|
if !images.blank?
|
|
|
|
images.each do |image|
|
|
|
|
if AlbumImage.where(:rss2_id => image["photoid"]).count == 0
|
|
|
|
img = newalbum.album_images.new
|
|
|
|
img.description_translations = {"en" => image["description"]["en"], "zh_tw" => image["description"]["zh_tw"]}
|
|
|
|
img.remote_file_url = image["image"]
|
|
|
|
img.save
|
|
|
|
end
|
|
|
|
@@import_stats["current_status"] = @@import_stats["current_status"] + 1
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-13 08:16:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 11:14:46 +00:00
|
|
|
# end
|
|
|
|
# @@thread.join
|
|
|
|
# @@thread.abort_on_exception = true
|
2014-06-13 08:16:07 +00:00
|
|
|
render :json => {"success" => true}.to_json
|
2014-06-11 07:16:32 +00:00
|
|
|
end
|
|
|
|
|
2014-06-16 09:16:45 +00:00
|
|
|
def rss2_links
|
2014-06-30 08:40:31 +00:00
|
|
|
delete_import_file
|
2014-06-16 09:16:45 +00:00
|
|
|
uri = URI.parse(params['url'])
|
2014-06-30 08:40:31 +00:00
|
|
|
# @@thread = Thread.new do
|
2014-06-16 09:16:45 +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)
|
|
|
|
@@import_stats["total_links"] = data["total_links"].to_i
|
|
|
|
@@import_stats["current_import"] = "categories"
|
|
|
|
@@import_stats["current_status"] = 0
|
2014-06-30 08:40:31 +00:00
|
|
|
@@import_stats["success"] = true
|
|
|
|
write_to_file
|
2014-06-16 09:16:45 +00:00
|
|
|
categories = data["categories"]
|
|
|
|
current_locale = I18n.locale
|
|
|
|
I18n.locale = :zh_tw
|
|
|
|
module_app = ModuleApp.find_by_key("web_resource")
|
|
|
|
categories.each do |category|
|
|
|
|
cat = Category.where(:title => category[1]["zh_tw"], :module_app_id => module_app.id).first
|
|
|
|
if cat.nil?
|
|
|
|
cat = Category.new
|
|
|
|
cat.title_translations = {"en" => category[1]["en"], "zh_tw" => category[1]["zh_tw"]}
|
|
|
|
cat.save
|
|
|
|
module_app.categories << cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@@import_stats["current_import"] = "links"
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-16 09:16:45 +00:00
|
|
|
links = data["links"]
|
|
|
|
links.each do |link|
|
|
|
|
l = WebLink.where(:rss2_id => link["link_id"]).first
|
|
|
|
if l.nil?
|
|
|
|
@@import_stats["current_link_id"] = link["link_id"]
|
|
|
|
@@import_stats["current_link_name"] = link["title"]["zh_tw"]
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-16 09:16:45 +00:00
|
|
|
weblink = WebLink.new(:title_translations => {"en" => link["title"]["en"], "zh_tw" => link["title"]["zh_tw"]}, :context_translations => {:en => link["description"]["en"], :zh_tw => link["description"]["zh_tw"]}, :url_translations => {"en" => link["url"], "zh_tw" => link["url"]}, :rss2_id => link["link_id"])
|
|
|
|
category = Category.where(:title => link["category_name"], :module_app_id => module_app).first
|
|
|
|
weblink.category_id = category.id
|
|
|
|
weblink.save
|
|
|
|
end
|
|
|
|
@@import_stats["current_status"] = @@import_stats["current_status"] + 1
|
2014-06-30 08:40:31 +00:00
|
|
|
write_to_file
|
2014-06-16 09:16:45 +00:00
|
|
|
end
|
|
|
|
I18n.locale = current_locale
|
2014-06-30 08:40:31 +00:00
|
|
|
# end
|
|
|
|
# @@thread.join
|
|
|
|
# @@thread.abort_on_exception = true
|
2014-06-16 09:16:45 +00:00
|
|
|
render :json => {"success" => true}.to_json
|
|
|
|
end
|
|
|
|
|
2014-06-11 07:16:32 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def page_present?(page)
|
|
|
|
Page.where(:page_id => page).count > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def smart_downloader(data,url)
|
|
|
|
@data = data
|
|
|
|
excluded_extensions = ["php","/"]
|
|
|
|
@links = URI.extract(@data)
|
|
|
|
@links.each do |link|
|
|
|
|
if link.include?url
|
|
|
|
temp = link.gsub(url,"")
|
|
|
|
extension = temp.split(".").last rescue nil
|
|
|
|
if extension.nil? || excluded_extensions.include?(extension)
|
|
|
|
@data = @data.sub(link,"#")
|
|
|
|
else
|
|
|
|
a = Asset.new
|
|
|
|
a.remote_data_url = link
|
|
|
|
a.title_translations = {"en" => a.data.filename, "zh_tw" => a.data.filename}
|
|
|
|
a.save!
|
|
|
|
@user.assets << a
|
|
|
|
@user.save
|
|
|
|
@data = @data.sub(link, a.data.url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@data
|
|
|
|
end
|
|
|
|
|
2014-06-30 08:40:31 +00:00
|
|
|
def write_to_file
|
|
|
|
File.open("#{Rails.root.to_s}/public/import_status.yml", "w") do |file|
|
|
|
|
file.write @@import_stats.to_yaml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_import_file
|
|
|
|
@@import_stats = {}
|
|
|
|
f = File.join(Rails.root,"public","import_status")
|
|
|
|
if File.exists?f
|
|
|
|
File.delete(f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_settings_from_import_file
|
|
|
|
YAML::load_file "#{Rails.root.to_s}/public/import_status.yml"
|
|
|
|
end
|
|
|
|
|
2014-06-11 07:16:32 +00:00
|
|
|
def get_user
|
|
|
|
@user = User.find(current_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|