ssummer3
9/27/2017 - 1:40 PM

Contentful Stuffs

Contentful Stuffs

---
- name: update contentful for hours
  hosts: library-web-campus-1
  strategy: free

  tasks:

    - name: update contentful
      uri:
        url: 'https://api.contentful.com/spaces/{{ space }}/entries?content_type=servicePoint&select=fields,sys.id,sys.version,sys.publishedVersion&limit=200'
        headers:
          Authorization: 'Bearer {{ contentful_key }}'
        return_content: true
      register: contentful
      delegate_to: localhost
    - set_fact:
        has_hours: >
          {{
             (contentful.content | from_json)
             | json_query("
                 items[?sys.publishedVersion && fields.hoursCode].{
                   id: sys.id,
                   version: sys.version,
                   data: { fields: fields }
               }")
          }}
    - command: >
        jq --argjson convert {{ convert | to_json | quote }} '.[]|.data.fields.hoursCode["en-US"]=$convert[.data.fields.hoursCode["en-US"]]'
      args:
        stdin: '{{ has_hours | to_json }}'
      register: jq_out
      delegate_to: localhost
...
#! /bin/bash

## set these
SPACE=
CONTENTFUL_KEY=

curl -s \
 -H "Authorization: Bearer ${CONTENTFUL_KEY}" \
 "https://api.contentful.com/spaces/${SPACE}/entries?content_type=servicePoint&select=fields,sys.id,sys.version,sys.publishedVersion&limit=200" \
 | jq '
     .items[]
     | select(.sys.publishedVersion and .fields.hoursCode)
     | { id: .sys.id,
         version: .sys.version,
         data: {fields: .fields},
       }
   ' \
 | jq --slurpfile convert convert.json -c '
     .data.fields.hoursCode["en-US"]=$convert[0][.data.fields.hoursCode["en-US"]]
     | select(.data.fields.hoursCode["en-US"])
   ' \
 | jq -r '"\(.id) \(.version) \(.data | tojson | @sh)"' \
 | while read id version data; do
    curl -X PUT \
      -H "Authorization: Bearer ${CONTENTFUL_KEY}" \
      -H "Content-Type: application/vnd.contentful.management.v1+json" \
      -H "X-Contentful-Version: ${version}" \
      -d "${data}" \
      "https://api.contentful.com/spaces/${SPACE}/entries/${id}" #\
    | jq -r '"\(.sys.version)"' \
    | while read newversion; do
        curl -X PUT
          -H "Authorization: Bearer ${CONTENTFUL_KEY}" \
          -H "Content-Type: application/vnd.contentful.management.v1+json" \
          -H "X-Contentful-Version: ${newversion}" \
          "https://api.contentful.com/spaces/${SPACE}/entries/${id}/published"
      done
   done 
{
  "universityarchives": "5561",
  "reservesmicrotextandmediadesk": "5557",
  "medievalinstitutelibrary": "5558",
  "circulationservicedesk": "1483",
  "engineeringlibrary": "5566",
  "architecturelibrary": "5563",
  "kelloggkroclibrary": "5567",
  "mahaffeybusinesslibrary": "5564",
  "chemistryphysicslibrary": "5565",
  "centerfordigitalscholarship": "5560",
  "rarebooksspecialcollections": "5556",
  "omearamathematicslibrary": "5569",
  "askusdesk": "5540",
  "hesburghlibrary": "426",
  "radiationchemistryreadingroom": "5570",
  "visualresourcescenter": "5571",
  "musiclibrary": "6092",
  "oitoutpost": "6091"
}