Skip to content

Instantly share code, notes, and snippets.

@studiotomi
Forked from eric-sproles-volusion/images.js
Last active April 24, 2020 13:17
Show Gist options
  • Save studiotomi/6c1d19b85137c204bf7ad0052d5b8cc1 to your computer and use it in GitHub Desktop.
Save studiotomi/6c1d19b85137c204bf7ad0052d5b8cc1 to your computer and use it in GitHub Desktop.
Write 404 images
const axios = require('axios');
const fs = require('fs');
const headers = { 'x-vol-tenant': '5c49cda1ddca5800122fad4e' };
const materialApi = 'https://api.material.com/images';
const getImages = async (url, headers) => {
if (!fs.existsSync('404s.txt')) {
fs.writeFileSync('404s.txt', '');
}
const params = {
pageSize: 100,
startIndex: 0,
};
let counter = 0;
try {
fs.open('404s.txt', 'a', async (err, fd) => {
if (err) throw err;
let response;
do {
response = await axios.get(url, { headers, params });
const {data} = response;
data.map(async image => {
axios
.head(image.fullUri)
.then(res => {
console.log(`${image.id} ${res.statusText}`);
})
.catch(error => {
fs.appendFile(fd, `${image.id}\n`, err => { });
console.log(`${image.id} returned 404`);
});
});
params.startIndex = (++counter) * params.pageSize;
} while (response.data.length > 0);
});
} catch (error) { console.log(error) }
};
getImages(materialApi, headers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment