Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm

tmtmtmtm/women-of-scotland.md Secret

Last active Aug 1, 2020
Embed
What would you like to do?
Using wikibase-cli to add "Women of Scotland" items

We have a Google Spreadsheet where we've been manually collecting data of Scottish memorials to women. We reconciled the women being commemorated to Wikidata, but that left quite a few for whom there didn't seem to be a Wikidata item yet.

I added lots of those using wikibase-cli:

First I created a template for what an initial item for the woman would contain:

add-woman.js:

module.exports = (label, id) => {
  return {
    type: 'item',
    labels: {
      en: label
    },
    claims: {
      P31: { value: 'Q5' },
      P21: { value: 'Q6581072' },
      P8050: { value: id }
    }
  }
}

In each case I would pass the "label" to be set in Wikidata (i.e. the woman's name), and the "id" of the woman from the Women of Scotland site, and would always add "instance of: human" and "sex/gender: female" (we also have other fields, but we'll add those separately later: this was just to quickly get IDs for everyone so we could set those as the 'commemorates' field on the memorial items being created in parallel with this).

For a single item this would be something like:

  wd create-entity add-woman.js "Billie Arthur" billie-arthur

If the list of these had been large, I would have fully automated creating all of them in one go, but because there was a relatively small number, and I wanted to paste the new item IDs back into the spreadsheet, I decided to just do them one at a time.

As well as creating the item, the wb command also returns what it did as a big chunk of JSON, from which I needed to extract the ID of the item. Using jq that can be pulled out as as .entity.id — i.e.:

   wd create-entity add-woman.js "Billie Arthur" billie-arthur | jq -r .entity.id

but I also wanted that straight into my paste buffer to put back into the spreadsheet, so:

   wd create-entity add-woman.js "Billie Arthur" billie-arthur | jq -r .entity.id | pbcopy

I also wanted to be able to grab the cells directly from the spreadsheet for the name and the label, but I needed to do a little bit of processing of them before sending them to this script:

pbpaste | awk -F"\t" '{ print "\""$1"\"", $2 }' 

(i.e. grab fields 1 and 2 from tab-separated output, and put quotes around the first one)

This let me chain the whole thing for each row as:

pbpaste  | awk -F"\t" '{ print "\""$1"\"", $2 }' | xargs wd ce add-woman.js | jq -r .entity.id | pbcopy

I was then able to do the same sort of process for the memorials themselves using a very similar script:

add-memorial.js:

module.exports = (label, id) => {
  return {
    type: 'item',
    labels: {
      en: label
    },
    claims: {
      P17: { value: 'Q145' },
      P131: { value: 'Q62274582' },
      P8048: { value: id }
    }
  }
}

Again this takes the label and ID, and adds a 'country: UK', and 'located in: Aberdeen'. The cells in the spreadsheet were a bit further apart this time, but the same approach works to select them:

pbpaste  | awk -F"\t" '{ print "\""$1"\"", $3 }' | xargs wd ce add-memorial.js | jq -r .entity.id | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.