Created
March 15, 2018 15:14
-
-
Save nickwesselman/9c420e1d35b916d59e112b9f723d3aba to your computer and use it in GitHub Desktop.
Sitecore Content Import via JSS Manifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function(manifest) { | |
manifest.addTemplate({ | |
name: 'Setlist', | |
icon: '/~/icon/office/32x32/earth_music.png', | |
fields: [ | |
{ name: 'eventDate', type: 'Date' }, | |
{ name: 'artist', type: manifest.fieldTypes.singleLineText }, | |
{ name: 'venue', type: manifest.fieldTypes.singleLineText }, | |
{ name: 'location', type: manifest.fieldTypes.singleLineText }, | |
{ name: 'description', type: manifest.fieldTypes.richText }, | |
] | |
}); | |
manifest.addTemplate({ | |
name: 'Song', | |
icon: '/~/icon/office/32x32/document_music.png', | |
fields: [ | |
{ name: 'name', type: manifest.fieldTypes.singleLineText }, | |
{ name: 'isCover', type: 'Checkbox' }, | |
{ name: 'coverArtist', type: manifest.fieldTypes.singleLineText }, | |
{ name: 'description', type: manifest.fieldTypes.richText } | |
] | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import moment from 'moment'; | |
import leftpad from 'left-pad'; // yup, I used it | |
const http = axios.create({ | |
baseURL: 'https://api.setlist.fm/rest/1.0', | |
headers: { | |
'Accept': 'application/json', | |
'x-api-key': '' // Get an API Key: https://www.setlist.fm/settings/api | |
} | |
}); | |
const getSetlists = (manifest, artistGuid, numPages) => { | |
const requests = [...Array(numPages).keys()].map((index) => getSetlistRequest(artistGuid, index+1)); | |
return Promise.all(requests) | |
.then((responses) => { | |
const setlists = responses.reduce((accumulator, response) => { | |
return accumulator.concat(response.data.setlist.map(setlist => parseSetlist(setlist))); | |
}, []); | |
manifest.addContent({ | |
path: '/Setlists', | |
name: 'Setlists', | |
displayName: 'Setlists', | |
template: 'Folder', | |
children: setlists | |
}); | |
}); | |
} | |
const getSetlistRequest = (artistGuid, pageNumber) => { | |
return http(`/artist/${artistGuid}/setlists?p=${pageNumber}`) | |
} | |
const parseSetlist = (setlist) => { | |
const eventDate = moment(setlist.eventDate, "DD-MM-YYYY"); | |
const path = `/Setlists/${eventDate}`; | |
return { | |
path: path, | |
name: eventDate.format("YYYY-MM-DD"), | |
displayName: `${eventDate.format("YYYY-MM-DD")} - ${setlist.venue.name}`, | |
template: 'Setlist', | |
fields: { | |
artist: { | |
value: setlist.artist.name | |
}, | |
eventDate: { | |
value: eventDate.format() | |
}, | |
venue: { | |
value: setlist.venue.name, | |
}, | |
location: { | |
value: `${setlist.venue.city.name}, ${setlist.venue.city.stateCode}` | |
}, | |
description: { | |
value: setlist.info ? `<p>${setlist.info}</p>` : null | |
} | |
}, | |
children: setlist.sets.set && setlist.sets.set.reduce((songs, set) => | |
songs.concat(set.song), []).map((song, index) => parseSong(song, path, index)), | |
} | |
} | |
const parseSong = (song, path, index) => { | |
const songIndex = index + 1; | |
const name = leftpad(songIndex, 3, '0'); | |
return { | |
path: `${path}/${name}`, | |
name: name, | |
displayName: `${songIndex} - ${song.name}`, | |
template: 'Song', | |
fields: { | |
name: { | |
value: song.name | |
}, | |
isCover: { | |
value: typeof song.cover !== 'undefined' | |
}, | |
coverArtist: { | |
value: song.cover ? song.cover.name : null | |
}, | |
description: { | |
value: song.info ? `<p>${song.info}</p>` : null | |
} | |
} | |
}; | |
} | |
export default function(manifest) { | |
return getSetlists(manifest, "83b9cbe7-9857-49e2-ab8e-b57b01038103", 25); // Pearl Jam! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment