orbit4-5/lib/tasks/sync_personal_plugins.rake

329 lines
12 KiB
Ruby
Raw Normal View History

2015-06-05 06:21:34 +00:00
require "uri"
require "net/http"
namespace :sync_personal_plugins do
task :sync,[:arg] => :environment do |task,args|
MemberProfile.each do |mp|
staff_id = mp.sid rescue nil
if !staff_id.nil? && staff_id != ""
sync_journal_papers(staff_id,mp)
sync_conference_papers(staff_id,mp)
sync_research_papers(staff_id,mp)
sync_books(staff_id,mp)
sync_projects(staff_id,mp)
sync_honors(staff_id,mp)
sync_diplomas(staff_id,mp)
sync_experiences(staff_id,mp)
end
end
end
end
def sync_journal_papers(staff_id,mp)
params_to_send = {"plugin" => "writing","type" => "journal", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Jounral Paper for staff #{staff_id}."
total = data["journals"].count
data["journals"].each_with_index do |journal,index|
jp = JournalPaper.where(:rss2_id => journal["rss_id"]).first rescue nil
if jp.nil?
jp = JournalPaper.new
jp.vol_no = journal["number"]
jp.publication_date = "#{journal["publication_year"]}/#{journal["publication_month"]}/01"
jp.rss2_id = journal["rss_id"]
jp.year = "#{journal["publication_year"]}"
jp.url = journal["url"]
jp.paper_title_translations = journal["title"]
jp.member_profile = mp
jp.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
jp.vol_no = journal["number"]
jp.year = "#{journal["publication_year"]}"
jp.publication_date = "#{journal["publication_year"]}/#{journal["publication_month"]}/01"
jp.url = journal["url"]
jp.paper_title_translations = journal["title"]
jp.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Jounral Paper for staff #{staff_id} completed."
end
def sync_conference_papers(staff_id,mp)
params_to_send = {"plugin" => "writing","type" => "conference", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Conference Paper for staff #{staff_id}."
total = data["conferences"].count
data["conferences"].each_with_index do |conference,index|
wc = WritingConference.where(:rss2_id => conference["rss_id"]).first rescue nil
if wc.nil?
wc = WritingConference.new
wc.isi_number = conference["number"]
wc.publication_date = "#{conference["publication_year"]}/#{conference["publication_month"]}/01"
wc.rss2_id = conference["rss_id"]
wc.year = "#{conference["publication_year"]}"
2015-06-05 06:21:34 +00:00
wc.url = conference["url"]
wc.paper_title_translations = conference["title"]
wc.member_profile = mp
wc.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
wc.isi_number = conference["number"]
wc.publication_date = "#{conference["publication_year"]}/#{conference["publication_month"]}/01"
wc.year = "#{conference["publication_year"]}"
wc.url = conference["url"]
wc.paper_title_translations = conference["title"]
wc.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Conference Paper for staff #{staff_id} completed."
end
def sync_research_papers(staff_id,mp)
params_to_send = {"plugin" => "writing","type" => "research", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Research Paper for staff #{staff_id}."
total = data["researches"].count
data["researches"].each_with_index do |research,index|
res = Research.where(:rss2_id => research["rss_id"]).first rescue nil
if res.nil?
res = Research.new
res.publish_date = "#{research["publication_year"]}/#{research["publication_month"]}/01"
res.rss2_id = research["rss_id"]
res.url = research["url"]
res.year = research["publication_year"]
res.research_title_translations = research["title"]
res.member_profile = mp
res.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
res.publish_date = "#{research["publication_year"]}/#{research["publication_month"]}/01"
res.url = research["url"]
res.year = research["publication_year"]
res.research_title_translations = research["title"]
res.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Research Paper for staff #{staff_id} completed."
end
def sync_books(staff_id,mp)
params_to_send = {"plugin" => "writing","type" => "book", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Books for staff #{staff_id}."
total = data["books"].count
data["books"].each_with_index do |book,index|
boo = Book.where(:rss2_id => book["rss_id"]).first rescue nil
if boo.nil?
boo = Book.new
boo.publish_date = "#{book["publication_year"]}/#{book["publication_month"]}/01"
boo.rss2_id = book["rss_id"]
boo.url = book["url"]
boo.year = book["publication_year"]
boo.book_title_translations = book["title"]
boo.member_profile = mp
boo.isbn = book["number"]
boo.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
boo.publish_date = "#{book["publication_year"]}/#{book["publication_month"]}/01"
boo.url = book["url"]
boo.year = book["publication_year"]
boo.book_title_translations = book["title"]
boo.isbn = book["number"]
boo.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Books for staff #{staff_id} completed."
end
def sync_projects(staff_id,mp)
params_to_send = {"plugin" => "project","type" => "xx", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Projects for staff #{staff_id}."
total = data["projects"].count
data["projects"].each_with_index do |project,index|
proj = Project.where(:rss2_id => project["rss_id"]).first rescue nil
if proj.nil?
proj = Project.new
proj.period_start_date = project["start_date"]
proj.period_end_date = project["end_date"]
proj.rss2_id = project["rss_id"]
proj.url = project["url"]
proj.year = project["year"]
proj.project_title_translations = project["title"]
proj.job_title_translations = project["job"]
proj.participator_translations = project["participator"]
proj.unit_translations = project["unit"]
proj.member_profile = mp
proj.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
proj.period_start_date = project["start_date"]
proj.period_end_date = project["end_date"]
proj.url = project["url"]
proj.year = project["year"]
proj.project_title_translations = project["title"]
proj.job_title_translations = project["job"]
proj.participator_translations = project["participator"]
proj.unit_translations = project["unit"]
proj.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Projects for staff #{staff_id} completed."
end
def sync_honors(staff_id,mp)
params_to_send = {"plugin" => "honor","type" => "xx", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Honors for staff #{staff_id}."
total = data["honors"].count
data["honors"].each_with_index do |honor,index|
hon = Honor.where(:rss2_id => honor["rss_id"]).first rescue nil
ht = HonorType.where(:title => honor["category"]["zh_tw"]).first rescue nil
ht = HonorType.where(:title => honor["category"]["en"]).first rescue nil
if ht.nil?
ht = HonorType.new
ht.title_translations = honor["category"]
ht.save
end
if hon.nil?
hon = Honor.new
hon.rss2_id = honor["rss_id"]
hon.award_name_translations = honor["title"]
hon.awarding_unit_translations = honor["unit"]
hon.year = honor["year"]
hon.honor_type = ht
hon.member_profile = mp
hon.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
hon.award_name_translations = honor["title"]
hon.awarding_unit_translations = honor["unit"]
hon.year = honor["year"]
hon.honor_type = ht
hon.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Honors for staff #{staff_id} completed."
end
def sync_diplomas(staff_id,mp)
params_to_send = {"plugin" => "diploma","type" => "xx", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Diplomas for staff #{staff_id}."
total = data["diplomas"].count
data["diplomas"].each_with_index do |diploma,index|
dip = Diploma.where(:rss2_id => diploma["rss_id"]).first rescue nil
if dip.nil?
dip = Diploma.new
dip.rss2_id = diploma["rss_id"]
dip.country_translations = diploma["country"]
dip.degree_translations = diploma["degree"]
dip.department_translations = diploma["dept"]
dip.end_date = diploma["end_date"].gsub("-","/").gsub("00","02")
dip.start_date = diploma["start_date"].gsub("-","/").gsub("00","02")
dip.school_name_translations = diploma["school"]
dip.member_profile = mp
dip.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
dip.country_translations = diploma["country"]
dip.degree_translations = diploma["degree"]
dip.department_translations = diploma["dept"]
dip.end_date = diploma["end_date"].gsub("-","/").gsub("00","02")
dip.start_date = diploma["start_date"].gsub("-","/").gsub("00","02")
dip.school_name_translations = diploma["school"]
dip.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Diplomas for staff #{staff_id} completed."
end
def sync_experiences(staff_id,mp)
params_to_send = {"plugin" => "experience","type" => "xx", "staff" => staff_id}
data = get_data(params_to_send)
return if !data["success"].nil?
puts "Starting Experiences for staff #{staff_id}."
total = data["experiences"].count
data["experiences"].each_with_index do |experience,index|
exp = Experience.where(:rss2_id => experience["rss_id"]).first rescue nil
et = ExperienceType.where(:title => experience["type"]["zh_tw"]).first rescue nil
et = ExperienceType.where(:title => experience["type"]["en"]).first rescue nil
if et.nil?
et = ExperienceType.new
et.title_translations = experience["type"]
et.save
end
if exp.nil?
exp = Experience.new
exp.rss2_id = experience["rss_id"]
exp.department_translations = experience["dept"]
exp.organizationt_title_translations = experience["title"]
exp.end_date = experience["end_date"]
exp.start_date = experience["start_date"]
exp.experience_type = et
exp.member_profile = mp
exp.save
puts "Syncing new : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
else
exp.department_translations = experience["dept"]
exp.organizationt_title_translations = experience["title"]
exp.end_date = experience["end_date"]
exp.start_date = experience["start_date"]
exp.experience_type = et
exp.save
puts "Updating old : " + ((100 * (index +1)) / total).to_s + "%"
sleep 0.5
end
end
puts "Experiences for staff #{staff_id} completed."
end
def get_data(params_to_send)
uri = URI.parse("http://140.119.221.39/sync/?plugin=#{params_to_send["plugin"]}&type=#{params_to_send["type"]}&staff=#{params_to_send["staff"]}")
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
return data
end