Skip to content

Commit ea4b109

Browse files
committed
Added script to populate site with random posts
1 parent b74ba38 commit ea4b109

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

add-random-posts.js

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@tryghost/admin-api": "^1.0.0",
2525
"bluebird": "^3.7.1",
2626
"ghost-ignition": "^3.1.0",
27-
"lodash": "^4.17.15"
27+
"lodash": "^4.17.15",
28+
"lorem-ipsum": "2.0.3"
2829
}
2930
}

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
326326
dependencies:
327327
delayed-stream "~1.0.0"
328328

329+
commander@^2.17.1:
330+
version "2.20.3"
331+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
332+
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
333+
329334
330335
version "0.0.1"
331336
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -1260,6 +1265,13 @@ lolex@^4.1.0, lolex@^4.2.0:
12601265
resolved "https://registry.yarnpkg.com/lolex/-/lolex-4.2.0.tgz#ddbd7f6213ca1ea5826901ab1222b65d714b3cd7"
12611266
integrity sha512-gKO5uExCXvSm6zbF562EvM+rd1kQDnB9AZBbiQVzf1ZmdDpxUSvpnAaVOP83N/31mRK8Ml8/VE8DMvsAZQ+7wg==
12621267

1268+
1269+
version "2.0.3"
1270+
resolved "https://registry.yarnpkg.com/lorem-ipsum/-/lorem-ipsum-2.0.3.tgz#9f1fa634780c9f58a349d4e091c3ba74f733164e"
1271+
integrity sha512-CX2r84DMWjW/DWiuzicTI9aRaJPAw2cvAGMJYZh/nx12OkTGqloj8y8FU0S8ZkKwOdqhfxEA6Ly8CW2P6Yxjwg==
1272+
dependencies:
1273+
commander "^2.17.1"
1274+
12631275
lower-case@^1.1.1:
12641276
version "1.1.4"
12651277
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"

0 commit comments

Comments
 (0)