From 077e98a6d45d020be2332b71c1f8dac4d7e893da Mon Sep 17 00:00:00 2001 From: manson666 Date: Tue, 31 Dec 2013 11:46:12 +0800 Subject: [PATCH] Fix Sync auto rescue timeout and filter user email for ntu only --- .../admin/import_data_controller.rb | 97 +++++++++---------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/app/controllers/admin/import_data_controller.rb b/app/controllers/admin/import_data_controller.rb index e4dffb2b..d9c75d16 100644 --- a/app/controllers/admin/import_data_controller.rb +++ b/app/controllers/admin/import_data_controller.rb @@ -2,6 +2,7 @@ require 'net/http' require 'open-uri' +require 'timeout' class Admin::ImportDataController < OrbitBackendController # helper Admin::PagePartsHelper @@ -483,19 +484,19 @@ end def sync_conference_data_auto @user = User.all @user.each do |user| - WritingConference.delete_all(conditions:{create_user_id: "#{user.id}"}) - # @user = User.find("#{params[:user_id]}") - - # teachers.each do |hash| - # if user.sid.present? - if user.user_id.present? - # ntu_seq = user.sid + if user.user_id.present? + next if !user.email.include? "ntu.edu.tw" u_account = user.user_id - conference_xml = Nokogiri::XML( \ - # open("http://ann.cc.ntu.edu.tw/Achv/xmlPaper.asp?Seq=#{ntu_seq}&type=C")) - open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=paper&account=#{u_account}&type=C")) - #open("http://versatile.management.ntu.edu.tw/publication1/conference/#{ntuseq}.xml")) - @conference_papers = conference_xml.xpath("//Paper").map do |cp_node| + @conference_xml = nil + begin + timeout(60) do + @conference_xml = Nokogiri::XML( \ + open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=paper&account=#{u_account}&type=C")) + end + rescue Timeout::Error + next + end + @conference_papers = @conference_xml.xpath("//Paper").map do |cp_node| { author: (cp_node>"Authors").text, year: (cp_node>"PublishYear").text, @@ -508,6 +509,8 @@ end } end if @conference_papers.present? + WritingConference.delete_all(conditions:{create_user_id: user.id}) + @conference_papers.each do |b| @conference_paper = WritingConference.new I18n.locale = :zh_tw @@ -545,16 +548,8 @@ end @conference_paper.save! end else - puts "No conference by Teacher" end - end - # respond_to do |format| - - # format.html - # format.json { render json: {"success"=>true}.to_json} - - # end end end @@ -562,20 +557,19 @@ end def sync_journal_paper_data_auto @user = User.all @user.each do |user| - WritingJournal.delete_all(conditions:{create_user_id: "#{user.id}"}) - # @user = User.find("#{params[:user_id]}") - - # teachers.each do |hash| - # if user.sid.present? if user.user_id.present? - next if user.user_id=="chenlin" - # ntu_seq = user.sid + next if !user.email.include? "ntu.edu.tw" u_account = user.user_id - paper_xml = Nokogiri::XML( \ - #open("http://versatile.management.ntu.edu.tw/publication1/journal/#{ntuseq}.xml")) - # open("http://ann.cc.ntu.edu.tw/Achv/xmlPaper.asp?Seq=#{ntu_seq}&type=J")) - open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=paper&account=#{u_account}&type=J")) - @journal_papers = paper_xml.xpath("//Paper").map do |paper_node| + @paper_xml = nil + begin + timeout(60) do + @paper_xml = Nokogiri::XML( \ + open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=paper&account=#{u_account}&type=J")) + end + rescue Timeout::Error + next + end + @journal_papers = @paper_xml.xpath("//Paper").map do |paper_node| { author: (paper_node>"Authors").text, year: (paper_node>"PublishYear").text, @@ -591,6 +585,9 @@ def sync_journal_paper_data_auto } end if @journal_papers.present? + + WritingJournal.delete_all(conditions:{create_user_id: user.id}) + @journal_papers.each do |b| @journal_paper = WritingJournal.new I18n.locale = :zh_tw @@ -639,18 +636,20 @@ end def sync_book_data_auto @user = User.all @user.each do |user| - WritingBook.delete_all(conditions:{create_user_id: "#{user.id}"}) - # @user = User.find("#{params[:user_id]}") - - # teachers.each do |hash| - # if user.sid.present? - if user.user_id.present? - # ntu_seq = user.sid - u_account = user.user_id - books_xml = Nokogiri::XML( \ - # open("http://ann.cc.ntu.edu.tw/Achv/xmlBook.asp?Seq=#{ntu_seq}")) - open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=Book&account=#{u_account}")) - @books = books_xml.xpath("//Book").map do |book_node| + if user.user_id.present? + next if !user.email.include? "ntu.edu.tw" + u_account = user.user_id + @books_xml = nil + begin + timeout(60) do + @books_xml = Nokogiri::XML( \ + open("http://ann.cc.ntu.edu.tw/Achv/xmlData_useMail.asp?item=Book&account=#{u_account}")) + end + rescue Timeout::Error + next + end + + @books = @books_xml.xpath("//Book").map do |book_node| { author: (book_node>"Authors").text, year: (book_node>"PublishYear").text, @@ -660,7 +659,11 @@ def sync_book_data_auto book_title: (book_node>"BookTitle").text } end + if @books.present? + + WritingBook.delete_all(conditions:{create_user_id: user.id}) + @books.each do |b| @book = WritingBook.new I18n.locale = :zh_tw @@ -681,12 +684,6 @@ def sync_book_data_auto end end end - # respond_to do |format| - - # format.html - # format.json { render json: {"success"=>true}.to_json} - - # end end end