Fix Sync auto rescue timeout and filter user email for ntu only

This commit is contained in:
manson666 2013-12-31 11:46:12 +08:00 committed by Manson Wang
parent 52f8e9f1b8
commit 077e98a6d4
1 changed files with 47 additions and 50 deletions

View File

@ -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]}")
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
# 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|
@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