Last active
June 25, 2017 13:52
-
-
Save michaelnagy/2e46afad16ae4dc55ba0ce6f3eb644b0 to your computer and use it in GitHub Desktop.
A simple node.js script to crawl and download information and images from pokeapi and pokemondb.net
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
// Nodejs > 7 or some babel is required cause we use ES7 syntax here (arrow functions and async await) | |
// This script requires node-fetch and image-downloader packages in order to work | |
// get it in npm: | |
// npm install --save image-downloader node-fetch | |
const download = require('image-downloader') | |
const fetch = require('node-fetch') | |
// We are doing a for loop to get info for the first 152 pokemons | |
for (var i = 1; i < 153; i++){ | |
const asyncDownload = async () => { | |
try { | |
// download JSON information needed to crawl pokemondb | |
let json = await (await fetch('https://pokeapi.co/api/v2/pokemon/'+i+'/')).json() | |
console.log(json.name); | |
// download the pokemon images | |
let result = await download.image({url: 'https://img.pokemondb.net/artwork/'+json.name+'.jpg', dest: __dirname+'/pokemons'}) | |
console.log('File saved to', result) | |
} | |
catch (error) { | |
console.log('request failed', error) | |
} | |
} | |
asyncDownload() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment