Skip to content

Instantly share code, notes, and snippets.

@eric-sproles-volusion
Last active March 11, 2020 18:47
Show Gist options
  • Save eric-sproles-volusion/1a6ee5011ae71d2311bb1161d8794fc6 to your computer and use it in GitHub Desktop.
Save eric-sproles-volusion/1a6ee5011ae71d2311bb1161d8794fc6 to your computer and use it in GitHub Desktop.
Write 404 images
const express = require('express');
const router = express.Router();
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) => {
const params = {
pageSize: 100,
startIndex: 0
};
let counter = 0;
try {
const response = await axios.get(url, { headers, params });
const images = response.data;
fs.writeFileSync('404s.txt', '');
images.map(async image => {
await axios
.head(image.fullUri)
.then(res => {
// console.log(`${image.id} ${res.status}`);
})
.catch(error => {
if (error.response.status === 404) {
const stream = fs.createWriteStream('404s.txt', { flags: 'a' });
stream.write(`${image.id}\n`);
} else {
fs.writeFileSync('other-errors.txt', '');
const otherStream = fs.createWriteStream('other-errors.txt', {
flags: 'a'
});
otherStream.write(`${image.id} ${res.status} \n`);
}
});
// console.log(counter);
params.startIndex = ++counter * params.pageSize;
// console.log('params.startIndex', params.startIndex);
});
} catch (error) {}
};
getImages(materialApi, headers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment