Skip to content

Instantly share code, notes, and snippets.

@paazmaya
Created March 19, 2021 22:57
Show Gist options
  • Save paazmaya/be31ac3c8f2677e6dc00b081f52c6d87 to your computer and use it in GitHub Desktop.
Save paazmaya/be31ac3c8f2677e6dc00b081f52c6d87 to your computer and use it in GitHub Desktop.
Get all Fred's ImageMagick Scripts with Playwright
const fs = require('fs');
// https://www.npmjs.com/package/playwright
const {
firefox
} = require('playwright');
const FILE_DIR = __dirname + '/freds-scripts';
// Fred's ImageMagick Scripts
const URL = 'http://www.fmwconcepts.com/imagemagick/index.php';
const getLinks = (list) => {
return Promise.all(list.map((item) => item.getAttribute('href')));
};
const getScript = async (page, name) => {
const link = `http://www.fmwconcepts.com/imagemagick/downloadcounter.php?scriptname=${name}&dirname=${name}`;
const [ download ] = await Promise.all([
page.waitForEvent('download'),
page.goto(link)
]);
return download.saveAs(FILE_DIR + '/' + name + '.sh');
};
(async () => {
const browser = await firefox.launch({
headless: false,
downloadsPath: FILE_DIR
});
const context = await browser.newContext({
acceptDownloads: true
});
const page = await context.newPage();
// The initial page container could skip images for saving bandwidth
await page.route(/\.(png|jpg|gif|svg)\?/, route => route.abort());
await page.goto(URL, {
waitUntil: 'domcontentloaded',
timeout: 0 // wait forever
});
const list = await page.$$('#nav li a');
const links = await getLinks(list);
console.log('Found links', links.length);
const downloads = links
.map((item) => {
return item.replace('http://www.fmwconcepts.com/imagemagick/', '').replace('/index.php', '');
})
.filter((item) => {
return item.length > 4 && item !== 'index.php';
});
for (let i = 0; i < downloads.length; ++i) {
const name = downloads[i];
console.log('Getting item', name);
await getScript(page, name);
}
await context.close();
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment