-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathadd-random-posts.js
87 lines (76 loc) · 2.78 KB
/
add-random-posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Adds N number of posts (10 by default)
*
* Usage:
*
* node add-random-posts.js https://blah.ghost.io ADMIN_API_KEY number_of_posts
*/
if (process.argv.length < 5) {
console.error('Missing an argument');
process.exit(1);
}
const url = process.argv[2];
const key = process.argv[3];
const count = process.argv[4] || 10;
const Promise = require('bluebird');
const loremIpsum = require('lorem-ipsum').loremIpsum;
const _ = require('lodash');
const GhostAdminAPI = require('@tryghost/admin-api');
console.log(url);
const api = new GhostAdminAPI({
url,
key,
version: 'v2'
});
(async function main() {
try {
const posts = [];
_.times(count, () => {
let post = {
status: 'published',
title: loremIpsum({
count: 2,
units: 'words'
}),
excerpt: loremIpsum({
count: 2,
units: 'sentences'
}),
html: loremIpsum({
count: 400, // Number of "words", "sentences", or "paragraphs"
format: 'html', // "plain" or "html"
paragraphLowerBound: 3, // Min. number of sentences per paragraph.
paragraphUpperBound: 7, // Max. number of sentences per paragarph.
random: Math.random, // A PRNG function
sentenceLowerBound: 3, // Min. number of words per sentence.
sentenceUpperBound: 15, // Max. number of words per sentence.
suffix: '\n', // Line ending, defaults to "\n" or "\r\n" (win32)
units: 'paragraphs', // paragraph(s), "sentence(s)", or "word(s)"
words: undefined // Array of words to draw from
}),
meta_title: loremIpsum({
count: 4,
units: 'words'
}),
meta_description: loremIpsum({
count: 2,
units: 'sentences'
})
};
posts.push(post);
});
console.log(`Adding ${posts.length} posts to ${url}`);
// convert our list of posts, to a list of promises for requests to the api
const result = await Promise.mapSeries(posts, async (post) => {
console.log('Adding', post.title);
// Call the API
let result = await api.posts.add(post, {source: 'html'});
// Add a delay but return the original result
return Promise.delay(5).return(result);
});
console.log(`Added ${result.length} posts`);
} catch (err) {
console.error('There was an error', require('util').inspect(err, false, null));
process.exit(1);
}
}());