From c32a5090d11d0c62e93ad2cdc3e5b12577a9c23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Starosta?= <afas15@inf.ufpr.br> Date: Tue, 7 Feb 2017 10:03:08 -0200 Subject: [PATCH] Final script almost finished --- scripts/finalScript.rb | 160 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 scripts/finalScript.rb diff --git a/scripts/finalScript.rb b/scripts/finalScript.rb new file mode 100644 index 0000000..1509f1c --- /dev/null +++ b/scripts/finalScript.rb @@ -0,0 +1,160 @@ +def createCommunities file + d = Dir[file + "*"].sort + d.each{|subfile| + meta = subfile + "/" + subfile.split('/')[-1] +".metadata" + name = getName(meta) + descript = getDescription(meta) + community = $client.communities.create( + Dspace::Community.new( + { + 'name' => name, + 'introductoryText' =>description + } + ) + ) + createSubComs(subfile,community.id) + } +end + +def createSubComs file,id + d = Dir[File.join(file , "*")] + d.each{|subfile| + if(subfile.split(".")[-1] != "metadata") + meta = File.join(subfile , "/" , subfile.split('/')[-1] + ".metadata") + name = getName(meta) + descript = getDescription(meta) + community = $client.communities.create_subcommunity( + Dspace::community.new( + { + 'name' = name, + 'introductoryText' => description + } + ), + id: id + ) + createSubComs(subfile,community.id) + end + } +end + + +def createCollections file + d = Dir[File.join(file , "*")] + d.each{|subfile| + if(subfile.split(".")[-1] != "metadata") + target = subfile + fileName = target.split('/')[-1] + target = fileName.split("_") + if(target[0] == "COL") + meta = file + "/" + file.split('/')[-1] +".metadata" + name = getName(meta) + descript = getDescription(meta) + ##PARSE COMMUNITY AND CREATE createCollections + collection = + createItem(subfile,collection.id) + end + if(target[0] == "NUCLEO") + createCollections(subfile) + end + end + + } +end + +def createItem file,id + d = Dir[File.join(file , "*")] + d.each{|subfile| + target = subfile + fileName = target.split('/')[-1] + target = fileName.split("_") + if(target[0]=="ITEM") + meta = subfile + "/" + subfile.split('/')[-1] + ".metadata" + if(File.exist?(meta)) + metarray = getItemMeta(meta) + title = metarray[0] + metarray.delete_at(0) + author = metarray[0] + metarray.delete_at(0) + description = metarray[-1] + metarray.delete_at(-1) + keywords = metarray + else + title = subfile.split('/')[-1].gsub('_',' ') + author = "" + description = "" + keywords = title.split(' ') + end + item_metadata = Dspace::Item.new( + 'metadata' => [ + {'key' => "dc.title", 'value' => title}, + {'key' => "dc.creator", 'value' => author}, + {'key' => "dc.description", 'value' => description} + ] + ) + # p id + item = $client.collections.create_item(item_metadata, id: id) + + g = Dir[subfile + "/*"].sort + g.each{|subsubfile| + if((subsubfile.split('.')[-1]=~/(?i)pdf|png/g)) + itemFile = File.new(subsubfile,'r') + $client.items.add_bitstream( + itemFile, + id:item.id, + name:subsubfile.split('/')[-1].gsub('_',' '), + description:" " + end + ) + } + end + + } +end + +def getName meta + f = File.open(meta, "r") + i = 0 + f.each_line{|line| + if(i == 1) + return(line.extractMeta) + end + i += 1 + } +end + +def getDescription meta + f = File.open(meta, "r") + i = 0 + f.each_line{|line| + if(i == 2) + return(line.extractMeta) + end + i += 1 + } +end + +################################################################################################### +################################################################################################### +$LOAD_PATH.unshift('/home/starosta/c3sl/dspace-scripts/dspace-rest-client/lib') +require 'dspace' +url = "https://seppirhomologa.c3sl.ufpr.br:8443/rest" +login = "admin@seppirhomologa.com" +passw = "adminadmin" + +#autenticacao +if(ARGV[0] == "debug") +$client = Dspace::Client.new(dspace_api: url, logger: Logger.new(STDOUT)) +else + $client = Dspace::Client.new(dspace_api: url) +end +if !$client.is_running? + raise 'Can\'t connect to DSpace API.' +end +$client.login(login, passw) +################################################################################################### +################################################################################################### +rootComunidades = "/home/starosta/c3sl/dspaceFiles/SEPPIR_SoNucleos/" +rootColecoesItens = "/home/starosta/c3sl/dspaceFiles/BANCO-SEPPIR-20160624/" +#main +createCommunities(rootComunidades) +createCollections(rootColecoesItens) -- GitLab