summaryrefslogtreecommitdiff
path: root/load_spreadsheet.js
blob: 347c6eb0599f07fcebd56920ab2991155399567a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * Load the No.6092 datasheet into the OKCMS JSON
 */

import { loadJSON, loadCSV, writeJSON } from "./file_utils.js";

const datasheetFile = "./data_store/tags.csv";
const dbFile = "./db.json";

var tagTypes = [
  "No6092",
  "1620s",
  "painting",
  "blunt",
  "National Gallery of Canada",
  "AGO",
  "courtauld",
  "intervensions",
  "connosieurship",
  "double agent",
  "forensics",
  "black box",
];

async function main() {
  // basically this script exists to assign the X'd fields from the spreadsheet
  // to the okcms json :)
  const data = await loadCSV(datasheetFile);
  const db = await loadJSON(dbFile);

  db.page = db.page || [];
  db.ui = db.ui || [];

  // index existing DB entries by ID in case we must merge again
  const pageById = db.page.reduce((lookup, page) => {
    const pageId = parseInt(page.id.split("_")[1]);
    lookup[pageId] = page;
    return lookup;
  }, {});

  // loop over the CSV data :)
  data.forEach((row, index) => {
    const cell = pageById[index] || {
      __index: index,
      id: "post_" + index,
      title: row.Title,
    };
    // loop over the tags...
    let tagIndex = 0;
    tagTypes.forEach((type, tagId) => {
      if (row[type] === "x") {
        cell["tag_" + tagIndex] = tagId + 1;
        tagIndex += 1;
      }
    });
    // make sure all other tags are cleared out
    for (; tagIndex < 9; tagIndex++) {
      cell["tag_" + tagIndex] = 0;
    }
    if (!pageById[index]) {
      db.page.push(cell);
    }
  });

  await writeJSON(dbFile, db);
}

main().then(() => process.exit(0));