From f98dc053f09d530b601164a6ea2f48e18621e6c7 Mon Sep 17 00:00:00 2001 From: manson Date: Fri, 30 May 2014 19:32:51 +0800 Subject: [PATCH] Import Announcement from RSS2 --- app/controllers/admin/import_controller.rb | 126 +++++++++++++++++++++ app/models/concerns/slug.rb | 2 +- config/routes.rb | 2 + 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/import_controller.rb diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb new file mode 100644 index 0000000..72b9770 --- /dev/null +++ b/app/controllers/admin/import_controller.rb @@ -0,0 +1,126 @@ +# encoding: utf-8 +require "net/http" +require "uri" +require 'json' + +class Admin::ImportController < OrbitAdminController + + def rss2_news + @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'] + @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.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 + + @user = User.where(:user_name => "rulingcom").first + + num = 0 + @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 + + I18n.locale = l + + redirect_to('/admin/announcements') + end +end \ No newline at end of file diff --git a/app/models/concerns/slug.rb b/app/models/concerns/slug.rb index 367d07d..6258faa 100644 --- a/app/models/concerns/slug.rb +++ b/app/models/concerns/slug.rb @@ -12,7 +12,7 @@ end def to_param - (self.slug_title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!:,、。!?;「」〈〉【】/]/,'-')+"-"+self.uid).gsub(/-{2,}/,'-') rescue "-"+self.uid + (self.slug_title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'-')+"-"+self.uid).gsub(/-{2,}/,'-') rescue "-"+self.uid end private diff --git a/config/routes.rb b/config/routes.rb index fe25239..07c1ae6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -195,6 +195,8 @@ Orbit::Application.routes.draw do end get 'design_list' => 'designs#design_list' get 'module_store' => 'module_store#index' + + get "import/rss2_news" => "import#rss2_news" end get 'mobile', to: 'pages#home'