Created
May 7, 2019 11:52
-
-
Save pmrt/fbdbf611d0588b2a7d607816bf8c89f8 to your computer and use it in GitHub Desktop.
crawler
This file contains hidden or 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
const api = 'https://www.autodoc.es/ajax/selector/car_selector.json' | |
/* | |
error throws an error with a provided `msg` | |
*/ | |
function error(msg) { | |
console.error(`Ha ocurrido un error: ${msg}`) | |
} | |
/* | |
GetManufacturers using a hardcoded selector. | |
*/ | |
function getManufacturers() { | |
return document.querySelectorAll("optgroup[label='Los fabricantes están en orden alfabético'] > option") | |
} | |
async function fetcher(body) { | |
try { | |
const res = await fetch(api, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body, | |
}) | |
const data = await res.json() | |
return JSON.parse(data) | |
} catch (err) { | |
error(err) | |
} | |
} | |
/* | |
fetch the models for a given manufacturer ID | |
*/ | |
async function getModelsByManufacturer(id) { | |
return await fetcher(`maker_id=${id}&changed=maker`) | |
} | |
/* | |
fetch type by a given manufacturer ID and model ID | |
*/ | |
async function getType(makerId, modelId) { | |
return await fetcher(`maker_id=${makerId}&model_id=${modelId}&changed=model`) | |
} | |
async function crawl() { | |
let res = [] | |
for (let maker of getManufacturers()) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment