Created
February 8, 2016 16:24
-
-
Save hijonathan/310bc74d3c79aa9929eb to your computer and use it in GitHub Desktop.
Simple node script to import blog articles into HubSpot.
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
| { | |
| "count": "2", | |
| "results": [{ | |
| "name": "Post 1", | |
| "meta_description": "A post worth reading", | |
| "meta_keywords": "articles, tests, things", | |
| "post_body": "<h1>Hey!</h1>\n<p>Some words and stuff!</p>", | |
| "blog_author_id": 40000001, | |
| "publish_date": 1454948561, | |
| "slug": "/one", | |
| "content_group_id": 3600000 | |
| }, { | |
| "name": "Post 2", | |
| "meta_description": "A post worth reading", | |
| "meta_keywords": "articles, tests, things", | |
| "post_body": "<h1>Hey!</h1>\n<p>Some words and stuff!</p>", | |
| "blog_author_id": 40000003, | |
| "publish_date": 1454948562, | |
| "slug": "/two", | |
| "content_group_id": 3600001 | |
| } | |
| ] | |
| } |
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
| var data = require('./articles.json').results, | |
| req = require('superagent'), | |
| API_KEY = process.env.API_KEY; | |
| function run(index) { | |
| index = index != null ? index : 0; | |
| req.post('https://api.hubapi.com/content/api/v2/blog-posts?hapikey=' + API_KEY) | |
| .send(data[index]) | |
| .end(function(err, res) { | |
| if (err && err.status !== 409) { | |
| console.log(err); | |
| } else { | |
| if (err && err.status === 409) { | |
| console.log('Duplicate entry. Skipped.'); | |
| } else { | |
| console.log('Added new post.'); | |
| } | |
| if (data[index+1] != null) { | |
| run(index+1); | |
| } | |
| } | |
| }) | |
| } | |
| run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment