Skip to content

Commit

Permalink
Adds missing featured images checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Alfaro committed Oct 12, 2018
1 parent 31b5290 commit b289e46
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ typings/
#pdf files
*.pdf
*.png
broken_images.txt
*.txt
62 changes: 62 additions & 0 deletions featured-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require('dotenv').config()
const puppeteer = require('puppeteer');
var fs = require('fs');

async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}

async function getImages(url , index=0){
let username = process.env.HT_USER;
let password = process.env.HT_PASS;
let browser = await puppeteer.launch();
let page = await browser.newPage();
try {
if (username && password) {
await page.authenticate({username, password});
}

await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 0
});
let images_links = await page.$$eval('.grop-post_image img', image => image);
if (images_links.length == 0) {
await page.screenshot({path: `page-${index}.png`, fullPage: true });
console.log(url);
fs.appendFile('missing_featured_images.txt', url+'\n', function (err) {
if (err) throw err;
console.log('Saved!');
});
}
await browser.close();
}
catch (e){
console.error(e);
}
}

(async() => {
fs.writeFile('missing_featured_images.txt', '', function (err) {
if (err) throw err;
console.log('Replaced!');
});

const same_site_links = process.env.SAME_SITE_LINKS.split(',');
try {

await asyncForEach(same_site_links, async (link, index) => {
try {
await getImages(link, index);
}
catch (e){
console.error(e);
}
});
}
catch (e){
console.error(e);
}
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
"start": "node index.js",
"featured-image": "node featured-images.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit b289e46

Please sign in to comment.