Skip to content

Commit

Permalink
start on script to make metadata changes from CSV, ref #34
Browse files Browse the repository at this point in the history
  • Loading branch information
phette23 committed Jul 29, 2023
1 parent 3d871f1 commit a522c8f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"@xmldom/xmldom": "^0.8.8",
"async": "^3.2.4",
"csv-reader": "^1.0.12",
"csv-stringify": "^6.4.0",
"filenamify": "~6.0.0",
"luxon": "^3.3.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions utilities/metadata-csv/modify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// usage:
// node modify --csv input.csv [--debug]
// input.csv must have a header row and the first column must be the UUID
// and the second column is the item version
import fs from 'node:fs'
import { default as fetch, Headers } from 'node-fetch'
import rc from 'rc'
import { DOMParser as xmldom } from '@xmldom/xmldom'
import xpath from 'xpath'
import { default as CSVReader } from 'csv-reader'

let options = rc('metadata-csv', {})

const headers = new Headers({
'X-Authorization': 'access_token=' + options.token,
'Accept': 'application/json',
})

// log messages only when debug=true
function debug(msg) {
if (options.debug) console.error(msg)
}

// take header row and row of values and turn them into a hash
function makeChangesHash(columns, row) {
if (columns.length === 0) {
console.error('Error: could not find header row. Are you sure the first two columns are "uuid,version" (case insensitive)?')
process.exit(1)
}
let changes = {}
columns.forEach((col, idx) => {
// lowercase the uuid and version columns for consistent access later
if (idx === 0 || idx === 1) col = col.toLowerCase()
changes[col] = row[idx]
})
return changes
}

function modifyItem(item, changes ) {
let xml = new xmldom().parseFromString(item.metadata)
let paths = Object.keys(changes)
paths.splice(0, 2)
// @TODO how to find and modify XML nodes from the paths?
// can xpath help here?
console.log(paths)
// start with for path of paths look up & print path value vs. changed value
}

function getItem(item) {
const url = `https://vault.cca.edu/api/item/${item.uuid}/${item.version}`
fetch(url, { headers: headers })
.then(r => r.json())
.then(data => {
// how EQUELLA does API errors
if (data.error) throw(data)
modifyItem(data, item)
}).catch(e => {
console.error(`Error fetching item ${url}`)
console.error('EQUELLA API Response:')
console.error(e)
})
}

let columns = []
const inputStream = fs.createReadStream(options.csv, 'utf8')

inputStream.pipe(new CSVReader({ trim: true }))
.on('data', (row)=> {
if (row[0].toLowerCase() === 'uuid' && row[1].toLowerCase() === 'version') {
columns = row
debug(`CSV columns are ${columns.join(', ')}`)
return
} else {
let changes = makeChangesHash(columns, row)
getItem(changes)
}
})
.on('end', () => {
debug('Finished reading rows from CSV')
})

0 comments on commit a522c8f

Please sign in to comment.