Last active
October 9, 2019 08:49
-
-
Save shaikh-shahid/6baaa02ceea5de2f024cbed89a7012cf to your computer and use it in GitHub Desktop.
This file contains 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
const mongo = require('mongodb'); | |
const chalk = require('chalk'); | |
const url = "mongodb://localhost:27017/codeforgeek"; | |
const async = require('async'); | |
const request = require('request'); | |
// start the work | |
mongo.connect(url, {useNewUrlParser: true}, (err, db) => { | |
if(err) { | |
console.log(err); | |
process.exit(0); | |
} | |
var dbo = db.db('codeforgeek'); | |
var collection = dbo.collection('posts'); | |
// grab posts | |
request('https://codeforgeek.com/wp-json/wp/v2/posts?per_page=100&page=1',(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://yourdomain.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 | |
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) => { | |
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 | |
}; | |
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