Last active
September 28, 2018 08:02
-
-
Save onefriendaday/42dc1dae46ad557f4ef878d5fbb3ecad to your computer and use it in GitHub Desktop.
XML export of stories
This file contains hidden or 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
| // About | |
| // This script exports all stories as XML in a specific folder | |
| // ------- | |
| // How to install? | |
| // 1. Create a folder for the project and inside a "data" folder | |
| // 2. $ npm init | |
| // 3. $ npm install storyblok-js-client --save | |
| // 4. Exchange SPACE_ID and YOUR_OAUTH2_TOKEN and optionally startWith to export only one specific folder | |
| // 5. $ node xml-export.js | |
| const StoryblokClient = require('storyblok-js-client') | |
| const fs = require('fs') | |
| const startWith = '' | |
| const spaceId = 'SPACE_ID' | |
| const oAuthToken = 'YOUR_OAUTH2_TOKEN' | |
| let Storyblok = new StoryblokClient({ | |
| oauthToken: oAuthToken | |
| }) | |
| var StoryblokExporter = { | |
| getAll(page) { | |
| return Storyblok.get('spaces/' + spaceId + '/stories', { | |
| per_page: 25, | |
| search: startWith, | |
| page: page | |
| }) | |
| }, | |
| export(id) { | |
| return Storyblok.get('spaces/' + spaceId + '/stories/' + id + '/export.xml') | |
| } | |
| } | |
| async function getAllStories(){ | |
| var page = 1 | |
| var res = await StoryblokExporter.getAll(page) | |
| var all = res.data.stories | |
| var total = res.total | |
| var lastPage = Math.ceil((res.total / 25)) | |
| while (page < lastPage){ | |
| page++ | |
| res = await StoryblokExporter.getAll(page) | |
| res.data.stories.forEach((story) => { | |
| all.push(story) | |
| }) | |
| } | |
| for (var i = 0; i < all.length; i++) { | |
| exportResult = await StoryblokExporter.export(all[i].id) | |
| console.log('Saving ' + all[i].id) | |
| fs.writeFile('./data/' + all[i].id + '.xml', exportResult.data, (err) => { | |
| if (err) throw err | |
| }) | |
| } | |
| return all | |
| } | |
| getAllStories().then((result) => { | |
| console.log('Finished') | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment