Last active
November 10, 2019 17:19
-
-
Save hlambur/913f36ba206dd1cec75e27983fa7c196 to your computer and use it in GitHub Desktop.
Retrieves the last 1000 days of SF poop data.
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
const fetch = require("node-fetch"); | |
const getJson = async url => { | |
const response = await fetch(url); | |
const json = await response.json(); | |
if (!json) { | |
throw `Query [${url}] failed to get JSON`; | |
} | |
return json; | |
}; | |
async function getPoopPrice(d1, d2) { | |
const url = [ | |
"https://data.sfgov.org/resource/vw6y-z8j6.json?", | |
"service_subtype=Human or Animal Waste&", | |
"$where=requested_datetime between '${d1}' and '${d2}'&", | |
"$select=count(service_request_id)" | |
].join(""); | |
// console.log(`Querying SF Data`); | |
const jsonOutput = await getJson(url); | |
// console.log(`SF Data response [${JSON.stringify(jsonOutput)}]`); | |
return jsonOutput; | |
} | |
function parseDate(date) { | |
var dateStr = date.toISOString(); | |
return dateStr.substring(0, dateStr.length -14); | |
} | |
async function getPriceHistory(historyLength) { | |
var data = {}; | |
const windowSize = 30; | |
var windowEnd = new Date(); | |
var windowStart = new Date(); | |
windowStart.setDate(windowEnd.getDate() - windowSize); | |
for (i = 0; i < historyLength; i++) { | |
console.log(`Get poops between ${parseDate(windowStart)} and ${parseDate(windowEnd)}`); | |
var result = await getPoopPrice(parseDate(windowStart), parseDate(windowEnd)); | |
result = result[0].count_service_request_id; | |
console.log(`Answer: ${result}`); | |
data[parseDate(windowEnd)] = result; | |
windowStart.setDate(windowStart.getDate() - 1); | |
windowEnd.setDate(windowEnd.getDate() - 1); | |
} | |
console.log(data); | |
return data; | |
} | |
getPriceHistory(1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment