-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgetImg.js
More file actions
63 lines (56 loc) · 1.39 KB
/
getImg.js
File metadata and controls
63 lines (56 loc) · 1.39 KB
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
require('dotenv').config()
const db = require('monk')(process.env.MONGO_DB)
const posts = db.get('posts')
const getUrls = require('get-urls');
const Bluebird = require('bluebird');
const rp = require('request-promise');
const async = require('async');
const CONCURRENCY = 5;
const q = async.queue(function(post, callback) {
console.log('starting', post.id)
rp(post._links['wp:featuredmedia'][0].href)
.then((result) => {
const json = JSON.parse(result);
if (!json.guid.rendered) {
return callback();
}
const featuredImage = json.guid.rendered.replace('http://', 'https://');
console.log(featuredImage)
posts.update({ id: post.id }, {
$set: {
featuredImage,
},
})
.then(() => {
callback();
})
})
.catch((error) => {
callback(error);
})
}, CONCURRENCY);
q.drain = function() {
console.log('all items have been processed');
db.close();
};
let promises = [];
posts
.find({
$or: [
{ featuredImage: { $exists: false } },
{ featuredImage: { $regex: 'http://' } },
]
})
.each((post) => {
if (!post._links['wp:featuredmedia']) {
console.log('no featuredmedia', post.id)
return;
}
q.push(post, function (err) {
if (err) {
console.log(err.message);
} else {
console.log('finished processing', post.id);
}
});
})