diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index 4a2bbbc..ecfae1f 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -289,7 +289,17 @@ class Admin::ImportController < OrbitAdminController render :json => {"success" => false, "current_album_id" => @@import_stats["current_album_id"], "current_album_name" => @@import_stats["current_album_name"]} end end - end + when "links" + if @@thread.alive? + render :json => {"success" => true, "current_import" => @@import_stats["current_import"], "total_links" => @@import_stats["total_links"], "current_status" => @@import_stats["current_status"], "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_album_name"]} + else + if @@import_stats["current_status"] == @@import_stats["total_links"] + render :json => {"success" => true, "current_import" => @@import_stats["current_import"], "total_links" => @@import_stats["total_links"], "current_status" => @@import_stats["current_status"], "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_link_name"]} + else + render :json => {"success" => false, "current_link_id" => @@import_stats["current_link_id"], "current_link_name" => @@import_stats["current_link_name"]} + end + end + end end def rss2_galleries @@ -340,6 +350,50 @@ class Admin::ImportController < OrbitAdminController render :json => {"success" => true}.to_json end + def rss2_links + uri = URI.parse(params['url']) + @@thread = Thread.new do + 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 + 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" + 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"] + 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 + end + I18n.locale = current_locale + end + @@thread.abort_on_exception = true + render :json => {"success" => true}.to_json + end + private def page_present?(page) diff --git a/app/views/admin/import/index.html.erb b/app/views/admin/import/index.html.erb index c96d3af..ce62863 100644 --- a/app/views/admin/import/index.html.erb +++ b/app/views/admin/import/index.html.erb @@ -144,6 +144,13 @@ <%= t("gallery.gallery") %> + +