Update import new from Rss2

This commit is contained in:
Manson Wang 2014-02-27 17:49:22 +08:00
parent d853cb9f63
commit befaae9b13
1 changed files with 11 additions and 7 deletions

View File

@ -5,8 +5,9 @@ require 'json'
class Admin::ImportController < ApplicationController class Admin::ImportController < ApplicationController
def rss2_news def rss2_news
@url = "http://192.168.1.10/alreadly_removed/NKUT/NKUT_01/remote_annoucement/get_news.php" @url = params['url']
uri = URI.parse(@url) uri = URI.parse(@url)
@host = uri.host
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri) request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request) response = http.request(request)
@ -38,7 +39,6 @@ class Admin::ImportController < ApplicationController
end end
end end
@regexp = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/
@user = User.where(:user_id => "rulingcom").first @user = User.where(:user_id => "rulingcom").first
@data.each do |row| @data.each do |row|
@ -46,19 +46,23 @@ class Admin::ImportController < ApplicationController
bulletin.title_translations = row['Title'] bulletin.title_translations = row['Title']
bulletin.subtitle_translations = row['Summary'] bulletin.subtitle_translations = row['Summary']
@site_valid_locales.each do |locale| @site_valid_locales.each do |locale|
temp = row['Content'][locale] temp = row['Content'][locale]
urls = temp.scan(@regexp)
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| urls.each do |url|
u = url[0]
a = Asset.new a = Asset.new
a.remote_data_url = u a.remote_data_url = url
a.assetable_type = "User" a.assetable_type = "User"
a.assetable_id = @user.id a.assetable_id = @user.id
a.title = {"en" => a["data"], "zh_tw" => a["data"]} a.title = {"en" => a["data"], "zh_tw" => a["data"]}
a.save! a.save!
temp.sub!(u,a.data.to_s) temp.sub!(url,a.data.to_s)
end end
row['Content'][locale] = temp row['Content'][locale] = temp
end end