Created
April 30, 2023 23:57
-
-
Save maxandron/58a7dc878e1e38ed9ea42e355c3135cf to your computer and use it in GitHub Desktop.
Script to fetch all of the dog friendly tracks in New Zealand from the DOC website
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 ProgressBar = require("progress"); | |
const fs = require("fs"); | |
const DOG_ACCESS_FILTER = { | |
epiFindPropertyName: "DogAccess", | |
selectedValue: "true", | |
fieldId: 1153495591, | |
}; | |
const NZ_REGIONS = [ | |
"Nelson/Tasman", | |
"Marlborough", | |
"West Coast", | |
"Canterbury", | |
"Otago", | |
"Southland", | |
"Fiordland", | |
]; | |
const commonHeaders = { | |
accept: "application/json, text/plain, */*", | |
"accept-language": "en-US,en;q=0.9,he-IL;q=0.8,he;q=0.7", | |
"content-type": "application/json;charset=UTF-8", | |
"sec-ch-ua": | |
'"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', | |
"sec-ch-ua-mobile": "?0", | |
"sec-ch-ua-platform": '"macOS"', | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-origin", | |
"Referrer-Policy": "strict-origin-when-cross-origin", | |
}; | |
async function fetchDogTracks(region, pageNumber = 0) { | |
const regionFilter = { | |
epiFindPropertyName: "Location.List", | |
selectedValue: region, | |
}; | |
const body = JSON.stringify({ | |
searchBlockId: 165894, | |
pageNumber, | |
lastPark: "", | |
sortBy: "Popular", | |
selectedFilters: [DOG_ACCESS_FILTER, regionFilter], | |
}); | |
const response = await fetch("https://www.doc.govt.nz/api/search", { | |
headers: commonHeaders, | |
body, | |
method: "POST", | |
}); | |
return response.json(); | |
} | |
async function getAllDogTracks() { | |
const allResults = []; | |
for (const region of NZ_REGIONS) { | |
let pageNumber = -1; | |
let totalResults = 0; | |
let progressBar; | |
let results = []; | |
do { | |
const data = await fetchDogTracks(region, pageNumber); | |
if (!data.totalResults) { | |
console.error("Error fetching dog-friendly tracks for region", region); | |
break; | |
} | |
// Initialize the progress bar | |
if (!progressBar) { | |
totalResults = data.totalResults; | |
progressBar = new ProgressBar( | |
`Fetching dog-friendly tracks in ${region} [:bar] :percent :etas`, | |
{ | |
complete: "=", | |
incomplete: " ", | |
width: 30, | |
total: totalResults, | |
} | |
); | |
} | |
results = results.concat(data.sections[0].foundPages); | |
pageNumber += 1; | |
// Update the progress bar | |
progressBar.tick(data.sections[0].foundPages.length); | |
} while (results.length < totalResults); | |
allResults.push(...results); | |
} | |
return allResults; | |
} | |
(async () => { | |
const dogTracks = await getAllDogTracks(); | |
console.log( | |
`\n${dogTracks.length} dog-friendly tracks fetched successfully.` | |
); | |
// Write the data to tracks.json | |
fs.writeFileSync("tracks.json", JSON.stringify(dogTracks, null, 2)); | |
console.log("Dog-friendly tracks saved to tracks.json"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment