From 013ca966efe1df1e8111c1017ff197ec1f02ff84 Mon Sep 17 00:00:00 2001 From: Nick Stokoe Date: Tue, 27 Dec 2022 18:08:44 +0000 Subject: [PATCH] unpack-whenwe-json.js - unpacks a drupal node-export of whenwe's nodes --- unpack-whenwe-json.js | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 unpack-whenwe-json.js diff --git a/unpack-whenwe-json.js b/unpack-whenwe-json.js new file mode 100644 index 0000000..68d6d7d --- /dev/null +++ b/unpack-whenwe-json.js @@ -0,0 +1,67 @@ +const data = require('./whenwe.json'); +const fs = require('fs'); +const path = require('path'); + +function toYaml(data, body) { + const frontmatter = Object.keys(data).sort().map(key => key+': '+(data[key] ?? '')).join("\n"); + return frontmatter + "\n---\n" + body; +} + +function date(datestr) { + if (datestr) { + const [Y,M,D,h,m,s] = datestr.split(/[^0-9]/) + return new Date(Date.UTC.call(null, Y,M-1,D,h,m,s)).toISOString(); + } + else + return ''; +} + + +data.forEach((node, ix) => { + const lang = 'und'; + const filepath = path.join('out', 'node.type'); + const body = node.body.und[0].value; + const filename = `${node.uuid}.yml`; + + fs.mkdirSync(path.join(__dirname, filepath), { recursive: true }); + const item = { + ix: ix, + nid: Number(node.nid), + type: node.type, + title: node.title, + uuid: node.uuid, + created: new Date(Number(node.created)*1000).toISOString(), + changed: new Date(Number(node.changed)*1000).toISOString(), + path: node.path.alias, + comment_count: node.comment_count, + }; + switch(node.type) { + case 'article': + Object.assign(item, { + original_author: node.field_original_author?.und?.[0]?.value, + featured_image: node.field_featured_image?.und?.[0]?.filename, + images: node.field_basic_image_image?.und?.map(item => item.filename), + category: node.field_category?.und?.[0]?.tid, + }); + break; + + case 'person': + Object.assign(item, { + forename_at_birth: node.field_forename_at_birth?.und?.[0]?.value, + surname_at_birth: node.field_surname_at_birth?.und?.[0]?.value, + other_surnames: node.field_other_surnames?.und?.[0]?.value, + other_forenames: node.field_other_forenames?.und?.[0]?.value, + title: node.field_title?.und?.[0]?.value, + date_of_birth: date(node.field_date_of_birth?.und?.[0]?.value), + date_of_death: date(node.field_date_of_death?.und?.[0]?.value), + parent_of: node.field_parent_of?.und?.[0]?.value, + child_of: node.field_child_of?.und?.[0]?.value, + partner_of: node.field_partner_of?.und?.[0]?.value, +// lifetime: node.field_lifetime?.und?.[0]?.value, + featured_image: node.field_featured_image?.und?.[0]?.filename, + }); + break; + } + fs.writeFileSync(path.join(filepath, filename), toYaml(item, body)); + console.log(item.title); +});