Last active
August 10, 2021 16:01
-
-
Save Rikezenho/60e6378eb56f5044b4c4c655dc253a88 to your computer and use it in GitHub Desktop.
Script para fazer polling no filômetro de SP e notificar quando tiver "FILA PEQUENA" ou "SEM FILA"
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
function eventLog() { | |
return { | |
get: () => localStorage.getItem('eventLog') || '{}', | |
set: (data) => localStorage.setItem('eventLog', data) | |
} | |
} | |
window.events = JSON.parse(eventLog().get()) | |
const doRequest = async () => { | |
if (Notification.permission !== 'granted') { | |
await Notification.requestPermission(); | |
} | |
const opcoesDePosto = ['AE DR. ALEXANDRE KALIL YASBEK - CECI', 'UBS PARQUE IMPERIAL - PROF. MANOEL A. S. SARAGOÇA'] | |
const intervaloEmMinutos = 10; | |
fetch("https://deolhonafila.prefeitura.sp.gov.br/processadores/dados.php", { | |
"headers": { | |
"content-type": "application/x-www-form-urlencoded; charset=UTF-8", | |
}, | |
"body": "dados=dados", | |
"method": "POST", | |
}).then((response) => response.json()) | |
.then((data) => data.filter(item => opcoesDePosto.includes(item.equipamento))) | |
.then((postos) => { | |
postos.forEach((posto) => { | |
const { equipamento, data_hora, status_fila } = posto; | |
window.events[equipamento] = events[equipamento] || {} | |
window.events[equipamento][data_hora] = status_fila | |
eventLog().set(JSON.stringify(window.events)) | |
console.log({ equipamento, data_hora, status_fila }) | |
if (['FILA PEQUENA', 'SEM FILA'].includes(status_fila)) { | |
new Notification(status_fila, { body: `${status_fila} - às ${data_hora} no posto ${equipamento}` }) | |
} | |
}) | |
}) | |
setTimeout(doRequest, intervaloEmMinutos * 60 * 1000) | |
} | |
doRequest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Como usar:
opcoesDePosto
Por padrão, de 10 em 10 minutos ele vai fazer o request e registrar a situação no
localStorage
, e te notificar quando estiver tranquilo, mas você pode também alterar o tempo se quiser :)PS: não abusem dos requests, não sabemos se a infraestrutura do serviço aguenta. Não seja esse cuzão que derruba serviços úteis pra população, ainda mais nesse momento.