Orbit/app/controllers/admin/import_data_controller.rb

95 lines
3.1 KiB
Ruby

# encoding: utf-8
require 'net/http'
require 'open-uri'
class Admin::ImportDataController < OrbitBackendController
def get_teacher_data
uri = URI("https://15fc4b.ngrok.com/teachers.xml")
#params = {"UnitName" => "管理學院".encode('big5-uao'), "account" => "manage"}
#uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
@teacher_data = Hash.from_xml(res.body)
@teachers = @teacher_data["objects"]
@i = 2000
@teachers.each do |hash|
@roles = Role.all
@teacher = User.new
I18n.locale = :zh_tw
if hash['teacher_zh_tw'].blank?
@teacher.first_name = hash['teacher_en']
elsif hash['teacher_zh_tw'].blank? && hash['teacher_en'].blank?
@teacher.first_name = "Please Fill the Name"
else
@teacher.first_name = hash['teacher_zh_tw']
end
I18n.locale = :en
if hash['teacher_en'].blank?
@teacher.first_name = hash['teacher_zh_tw']
elsif hash['teacher_zh_tw'].blank? && hash['teacher_en'].blank?
@teacher.first_name = "Please Fill the Name"
else
@teacher.first_name = hash['teacher_en']
end
#@teacher.first_name = hash['teacher_en']
@teacher.ntu_seq = hash['ntu_seq']
@teacher.role_ids = ["#{@roles.first.id}"]
@teacher.password = "testpass"
@i += 10
if hash['email'].blank?
@teacher.email = "#{@i}@ntu.edu.tw"
Rails.logger.info"@@@@@@@@@@@@@@@@@@@@@"+@teacher.email.inspect
else
@teacher.email = hash['email']
end
@teacher.user_id = hash['teacher_en']
@teacher.save!
end
end
# def get_book_data
# uri = URI("http://ann.cc.ntu.edu.tw/Achv/xmlTeacherData.asp")
# params = {"UnitName" => "管理學院".encode('big5-uao'), "account" => "manage"}
# uri.query = URI.encode_www_form(params)
# res = Net::HTTP.get_response(uri)
# teachers = User.all
# teachers.each do |hash|
# if hash.ntu_seq.present?
# ntu_seq = hash.ntu_seq
# books_xml = Nokogiri::XML( \
# open("http://ann.cc.ntu.edu.tw/Achv/xmlBook.asp?Seq=#{ntu_seq}"))
# @books = books_xml.xpath("//Book").map do |book_node|
# {
# author: (book_node>"Authors").text,
# year: (book_node>"PublishYear").text,
# title: (book_node>"DocTitle").text,
# remarks: (book_node>"Remarks").text,
# publisher: (book_node>"Publisher").text,
# book_title: (book_node>"BookTitle").text
# }
# end
# if @books.present?
# @books.each do |b|
# @book = WritingBook.new
# @book.authors = b[:author]
# @book.paper_title = b[:title]
# @book.book_title = b[:book_title]
# @book.year = b[:year]
# @book.note = b[:remarks]
# @book.publisher = b[:publisher]
# @book.create_user_id = hash.id
# @book.save
# end
# else
# puts "No books by Teacher"
# end
# end
# end
#end
end