Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mongo.connect(url, {useNewUrlParser: true}, (err, db) => {
if(err) {
console.log(err);
process.exit(0);
}
var dbo = db.db('codeforgeek');
console.log('database connected!');
var collection = dbo.collection('posts');
// grab posts
request('https://codeforgeek.com/wp-json/wp/v2/posts?per_page=100&page=3',(err, response, body) => {
let data = JSON.parse(body);
//console.log(data);
let userData = [];
data.forEach(singleData => {
async.waterfall([
(callback) => {
// get the featured image
request(`https://codeforgeek.com/wp-json/wp/v2/media/${singleData.featured_media}`,(err, res, body) => {
if(err) {
console.log(err);
return callback(null, null);
}
let data = JSON.parse(body);
console.log('featued image = ', data.source_url);
return callback(null, data.source_url);
});
},
(featueredImage, callback) => {
// get categories name
dbo.collection('categories').find({id: { $in: singleData.categories}}).toArray((err, result) => {
let catname = [];
result.forEach((singleResult) => {
catname.push({
name: singleResult.name,
slug: singleResult.slug,
});
});
console.log('categories = ', catname);
return callback(null, featueredImage, catname);
});
},
(featueredImage, catNames, callback) => {
// get tags name
// get categories name
dbo.collection('tags').find({id: { $in: singleData.tags}}).toArray((err, result) => {
let tagname = [];
result.forEach((singleResult) => {
tagname.push({
name: singleResult.name,
slug: singleResult.slug,
});
});
console.log('tags = ', tagname);
return callback(null, featueredImage, catNames, tagname);
});
}
], (err, image, cats, tags) => {
console.log(image);
console.log(cats);
console.log(tags);
let parseContent = JSON.parse(JSON.stringify(singleData.content));
let formatContent = parseContent.rendered.split("\n").join('');
let formatContentspaces = formatContent.split("\t").join('');
let postData = {
id: singleData.id,
title: singleData.title.rendered,
date: singleData.date,
url: singleData.link,
slug: singleData.slug,
status: singleData.status,
type: singleData.type,
excerpt: singleData.excerpt.rendered,
content: formatContentspaces,
author: singleData.author,
categories: cats,
tags: tags,
featured_image: image
};
console.log(postData);
collection.insert(postData, (err, result) => {
if(err) {
console.log(err);
process.exit(0);
}
console.log(result);
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.