jagreygoose

Google Books Library XML Output to CSV & SQL

Jun 17th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Output Google Books Library XML Files to CSV & SQL
  2. # For creating labels for new library books & SQL INSERT statements
  3. set-executionpolicy unrestricted
  4.  
  5. $googleXML =  Read-Host "Enter the path to the Google books xml file ( D:\AR_Middle_years.xml )"#"D:\AR_Middle_years.xml" "Enter the path to the Google books xml file ( D:\AR_Middle_years.xml )"
  6. $configDir = Read-Host "Enter the path to the configuration file ( D:\config.xml )"
  7. $outputDir =  "D:\" #"Enter root directory for output ( D:\ )"
  8.  
  9. #Load XML file exported from Google Books
  10. [xml]$books = Get-Content $googleXML -ErrorAction Stop
  11. #Load configuration file
  12. [xml]$config = Get-Content $configDir -ErrorAction Stop
  13.  
  14.  
  15.  #Get Unique ID set as integer
  16.  [int]$UID = $config.Configuration.UID
  17.  #Set input file name without extension or path
  18.  $fileNameWE = [io.path]::GetFileNameWithoutExtension($googleXML)
  19.  
  20.  #Create a Powershell Object from imported XML
  21.  #Add the auto incrementing UID value from the configuration file
  22.  $bookObj = $books.library.books.book | ForEach-Object {
  23.       New-Object PSCustomObject -Property @{
  24.         "ISBN" = $_.identifier.value
  25.         "Title" = $_.title
  26.         "Author" = $_.contributor
  27.         "Barcode" = 'e' + $UID++
  28.       }
  29.  }
  30.      
  31.  
  32. #
  33. #concatenate SQL INSERT Statement
  34. #
  35.  
  36.      $biblioQuery = 'INSERT IGNORE INTO biblio ' #SQL Command, table name, ' ' blank space at the end.
  37.      $biblio_copyQuery = 'INSERT IGNORE INTO biblio_copy '
  38.      $biblio_fieldQuery = 'INSERT IGNORE INTO biblio_field '
  39.      $sqlArray = @() #Initialize an empty array
  40.  
  41. #
  42. #Set database column names
  43. #
  44.  
  45. #biblio table
  46. $biblioID = 'bibid';
  47. $b_COL2 = 'create_dt'; $b_COL3 = 'last_change_dt'; $b_COL4 = 'last_change_userid'; $b_COL5 = 'material_cd'; $b_COL6 = 'collection_cd'; $b_COL7 = 'call_nmbr1'; $b_COL10 = 'title'; $b_COL13 = 'author'; $b_COL19 = 'opac_flg';    
  48. $b_VAL2 = 'now()';     $b_VAL3 = 'now()';          $b_VAL4 = '1';                  $b_VAL5 = '2';           $b_VAL6 = '1';             $b_VAL7 = '';           $b_VAL10 = '';      $b_VAL13 = '';       $b_VAL19 = 'Y';
  49.  
  50.  
  51. #biblio_copy table
  52. $b_c_COL2 = 'copyid'; $b_c_COL3 = 'create_dt'; $b_c_COL4 = 'copy_desc'; $b_c_COL5 = 'barcode_nmbr'; $b_c_COL6 = 'status_cd'; $b_c_COL7 = 'status_begin_dt'; $b_c_COL10 = 'renewal_count';
  53. $b_c_VAL2 = '';      $b_c_VAL3 = 'now()';     $b_c_VAL4 = '';          $b_c_VAL5 = '';             $b_c_VAL6 = 'in';        $b_c_VAL7 = 'now()';           $b_c_VAL10 = '0';
  54.  
  55. #biblio_field table
  56. $b_f_COL2 = 'fieldid'; $b_f_COL3 = 'tag'; $b_f_COL4 = 'ind1_cd'; $b_f_COL5 = 'ind2_cd'; $b_f_COL6 = 'subfield_cd'; $b_f_COL7 = 'field_data'
  57. $b_f_VAL2 = '1';       $b_f_VAL3 = '20';  $b_f_VAL4 = 'N';       $b_f_VAL5 = 'N';       $b_f_VAL6 = 'a';            
  58.      
  59.      
  60.  #Populate $sqlArray
  61.  $bookObj | ForEach-Object{
  62.      $VAL1 =  $_.Barcode
  63.      $VAL3 =  $_.Title.replace("'","")
  64.      $VAL4 =  $_.Author.replace("'","")
  65.      $VAL2 =  $_.ISBN
  66.      $sqlArray += Write-Output "/* $VAL3 by $VAL4 (Barcode: $VAL1, ISBN: $VAL2) */"
  67.      $sqlArray += Write-Output $biblioQuery"("$b_COL2","$b_COL3","$b_COL4","$b_COL5","$b_COL6","$b_COL7","$b_COL10","$b_COL13","$b_COL19") VALUES ("$b_VAL2","$b_VAL3",'"$b_VAL4"','"$b_VAL5"','"$b_VAL6"','"$VAL2"','"$VAL3"','"$VAL4"','"$b_VAL19"');"
  68.      $sqlArray += Write-Output $biblio_copyQuery"("$biblioID","$b_c_COL2","$b_c_COL3","$b_c_COL5","$b_c_COL6","$b_c_COL7","$b_c_COL10") SELECT biblio.bibid,'"$b_c_VAL2"',biblio.create_dt,'"$VAL1"','"$b_c_VAL6"',biblio.create_dt,'"$b_c_VAL10"' FROM biblio WHERE biblio.title = '"$VAL3"' AND biblio.author = '"$VAL4"';"
  69.      $sqlArray += Write-Output $biblio_fieldQuery"("$biblioID","$b_f_COL2","$b_f_COL3","$b_f_COL4","$b_f_COL5","$b_f_COL6","$b_f_COL7") SELECT biblio.bibid,'"$b_f_VAL2"','"$b_f_VAL3"','"$b_f_VAL4"','"$b_f_VAL5"','"$b_f_VAL6"', biblio.call_nmbr1 FROM biblio WHERE biblio.title = '"$VAL3"' AND biblio.author = '"$VAL4"';"
  70.      $sqlArray += Write-Output ""
  71.  }
  72.  
  73.  
  74.  #
  75.  #Create Output Files
  76.  #
  77.  
  78.  
  79. if (!(Test-Path -path $outputDir"LibraryLabels")) {
  80.  
  81. New-Item $outputDir"LibraryLabels\SQL" -type directory
  82. New-Item $outputDir"LibraryLabels\CSV" -type directory
  83. Write-Host "New Directories Created:"
  84. Write-Host $outputDir"LibraryLabels\SQL"
  85. Write-Host $outputDir"LibraryLabels\CSV"
  86.  
  87. }
  88.  
  89.  
  90.  #Set the format to export the SQL Statments
  91.  $sqlArray | out-file  -force -Encoding "UTF8" -filepath $outputDir"LibraryLabels\SQL\"$fileNameWE"_sqlOutput_"$((Get-Date).ToString("yyyy-MM-dd")).sql
  92.  
  93. #Use select to order columns and export to csv
  94.  $bookObj | Select Title, Author, ISBN, Barcode | Export-Csv -NoTypeInformation -Path $outputDir"LibraryLabels\CSV\"$fileNameWE"_labelOutput_"$((Get-Date).ToString("yyyy-MM-dd")).csv
  95.  
  96.  
  97.  #
  98.  #Update Configuration File
  99.  #
  100.  
  101.  $config.Configuration | ForEach-Object { $_.UID = $UID.ToString() } #convert from int to string
  102.  $config.Save($configDir) #save xml
  103.  
  104.  
  105. #
  106. #Provide 15 seconds of feedback before script end
  107. #
  108.  
  109. #If the path to the files the script creates exist show a success message, otherwise throw error
  110. if (!(Test-Path -path $outputDir"LibraryLabels\SQL\"$fileNameWE"_sqlOutput_"$((Get-Date).ToString("yyyy-MM-dd")).sql)-and (Test-Path -path $outputDir"LibraryLabels\CSV\"$fileNameWE"_labelOutput_"$((Get-Date).ToString("yyyy-MM-dd")).csv)) {
  111.  
  112. Write-Host "Warning! Files could not be created."
  113.  
  114. }
  115. ELSE {
  116.  
  117. Write-Host "Success! Files Created:"
  118. Write-Host $outputDir"LibraryLabels\SQL\"$fileNameWE"_sqlOutput_"$((Get-Date).ToString("yyyy-MM-dd")).txt
  119. Write-Host $outputDir"LibraryLabels\CSV\"$fileNameWE"_labelOutput_"$((Get-Date).ToString("yyyy-MM-dd")).csv
  120. }
  121.  
  122. #Confirm that UID has been correctly incremented and is written to the configuration file
  123. Write-Host UID in configuration file updated to: $config.Configuration.UID
  124.  
  125. #After messages are written to the console, sleep for 15 seconds then -END
  126. Start-Sleep -s 15
  127.  
  128. #
  129. #Example Configuration File
  130. #
  131.  
  132. #<?xml version="1.0" encoding="utf-8"?>
  133. #<Configuration>
  134. #    <UID>8231</UID>
  135. #</Configuration>
Add Comment
Please, Sign In to add comment