-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rafael Alfaro
committed
Oct 12, 2018
1 parent
b289e46
commit 6f78b16
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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(); | ||
// let old_url = process.env.OLD_DOMAIN.replace(/\/$/, '') + '/' +url; | ||
let new_url = process.env.NEW_DOMAIN.replace(/\/$/, '') + '/' +url; | ||
try { | ||
if (username && password) { | ||
await page.authenticate({username, password}); | ||
} | ||
|
||
await page.goto(new_url, { | ||
waitUntil: 'networkidle2', | ||
timeout: 0 | ||
}); | ||
let images_links = await page.$$eval('body.error404', image => image); | ||
if (images_links.length > 0) { | ||
// await page.screenshot({path: `page-${index}.png`, fullPage: true }); | ||
console.log(new_url); | ||
|
||
fs.appendFile('broken-links.txt', new_url+'\n', function (err) { | ||
if (err) throw err; | ||
console.log('Saved!'); | ||
}); | ||
} | ||
await browser.close(); | ||
} | ||
catch (e){ | ||
console.error(e); | ||
} | ||
} | ||
|
||
(async() => { | ||
fs.writeFile('broken-links.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); | ||
} | ||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
HT_USER=your_user | ||
HT_PASS=your_password | ||
SAME_SITE_LINKS=https://your_link.com,https://your_link_2.com | ||
NEW_DOMAIN=https://google.com |
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