orbit4-5/lib/tasks/upgrade2.rake

140 lines
5.1 KiB
Ruby
Raw Normal View History

2014-11-07 08:06:50 +00:00
require "net/http"
require "uri"
require 'json'
namespace :upgrade2 do
desc "Imports news from orbit 4.2"
task :import_news,[:url] => :environment do |task,args|
puts "Starting to import news from #{args.url}"
# u get total number of pages
total_pages = 0
uri = URI.parse("#{args.url}/panel/upgrade/module?module=news_count")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = JSON.parse(response.body) rescue nil
if !data.nil? && data["success"] == true
puts "Importing categories...."
categories = data["categories"]
tags = data["tags"]
module_app = ModuleApp.find_by_key("news")
I18n.locale = :zh_tw
categories.each do |category|
c = Category.where(:title => category["title"]["zh_tw"], :module_app_id => module_app.id).first
c = Category.where(:title => category["title"]["en"], :module_app_id => module_app.id).first if c.nil?
if c.nil?
c = Category.new
c.title_translations = category["title"]
c.disable = category["disable"]
c.module_app_id = module_app.id
c.save
end
end
puts "Categories imported."
puts "Importing tags..."
tags.each do |tag|
t = module_app.tags.where(:name => tag["name"]["zh_tw"]).first
t = module_app.tags.where(:name => tag["name"]["en"]).first
if t.nil?
t = Tag.new
t.name_translations = tag["name"]
t.is_default = tag["is_default"]
t.module_app_ids << module_app.id
t.save
end
end
puts "Importing tags completed."
puts data["count"]
total_pages = data["count"]/150
total_pages = total_pages.ceil + 1
(1..total_pages).each do |page_number|
uri = URI.parse("#{args.url}/panel/upgrade/module?module=news&page=#{page_number}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = JSON.parse(response.body) rescue nil
news = data["news"]
if !data.nil? && data["success"] == true
puts "Importing news..."
news.each do |news|
bulletin = NewsBulletin.new
bulletin.email_sent = news["email_sent"]
bulletin.email_sentdate = news["email_sentdate"]
bulletin.other_mailaddress = news["other_mailaddress"]
bulletin.public = news["public"]
bulletin.postdate = news["postdate"]
bulletin.deadline = news["deadline"]
bulletin.department_id = news["department"]
bulletin.unit_id = news["unit"]
bulletin.subtitle_translations = news["subtitle"]
news["text"]["en"] = (news["text"]["en"].nil? ? " " : smart_downloader(news["text"]["en"], args.url.chomp("/")))
news["text"]["zh_tw"] = (news["text"]["zh_tw"].nil? ? " " : smart_downloader(news["text"]["zh_tw"], args.url.chomp("/")))
bulletin.text_translations = news["text"]
bulletin.title_translations = news["title"]
bulletin.remote_image_url = news["image"]
news["links"].each do |link|
l = NewsBulletinLink.new
l.title_translations = link["title"]
l.url = link["url"]
l.news_bulletin = bulletin
l.save
end
news["files"].each do |file|
f = NewsBulletinFile.new
f.title_translations = file["title"]
f.description_translations = file["description"]
f.remote_file_url = file["file"]
f.news_bulletin = bulletin
f.save
end
category = module_app.categories.where(:title => news["category"]["zh_tw"]).first
category = module_app.categories.where(:title => news["category"]["en"]).first if category.nil?
bulletin.category = category
news["tags"].each do |tag|
t = module_app.tags.where(:name => tag["name"]["zh_tw"]).first
t = module_app.tags.where(:name => tag["name"]["en"]).first if t.nil?
bulletin.tags << t
end
bulletin.approved = true
bulletin.save
end
puts "News imported successfully for page number #{page_number}."
else
puts "Import has some problem."
end
end
end
end
end
def smart_downloader(data,url)
@data = data
@user = User.where(:user_name => "rulingcom").first
excluded_extensions = ["php","/"]
regex = /https?:\/\/[\S]+/
# @links = URI.extract(@data) rescue []
@links = @data.scan(regex) rescue []
@links = @links.map{|link| link.chomp("\"") if (link.include?url rescue false)}.reject{|link| link.nil?}.uniq
@links.each do |link|
link = link.gsub("\&amp\;","&")
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}
if (a.save! rescue false)
@user.assets << a
@user.save
link = link.gsub("&","\&amp\;")
@data = @data.sub(link, a.data.url)
end
end
end
@data
end