Skip to content

Instantly share code, notes, and snippets.

@ejfox
Created September 22, 2022 16:31
Show Gist options
  • Save ejfox/ad555d7d8fca5cf5f35db1c3e3486a72 to your computer and use it in GitHub Desktop.
Save ejfox/ad555d7d8fca5cf5f35db1c3e3486a72 to your computer and use it in GitHub Desktop.
A GPT-3 code-davinci-002 written script to scrape the top 40 worst polluters for all available industries
/* Our API takes queries like https://api.dev.climatetrace.org/v0/assets?sectors=steel&limit=40&year=2020 and returns geojson with point features */
/* We need to loop through every available sector and concatenate all of the features together into one large GeoJSON file called top40_allsectors.geojson */
/* Available sectors are: [{"SectorId":1,"SectorName":"solid-waste-disposal"},{"SectorId":2,"SectorName":"manure-management"},{"SectorId":3,"SectorName":"oil-and-gas-production-and-transport"},{"SectorId":4,"SectorName":"coal-mining"},{"SectorId":5,"SectorName":"cement"},{"SectorId":6,"SectorName":"electricity-generation"},{"SectorId":7,"SectorName":"oil-and-gas-refining"},{"SectorId":8,"SectorName":"aviation"},{"SectorId":9,"SectorName":"international-aviation"},{"SectorId":10,"SectorName":"copper-mining"},{"SectorId":11,"SectorName":"shipping"},{"SectorId":12,"SectorName":"iron-mining"},{"SectorId":13,"SectorName":"bauxite-mining"},{"SectorId":14,"SectorName":"domestic-aviation"},{"SectorId":15,"SectorName":"road-transportation"},{"SectorId":16,"SectorName":"enteric-fermentation"},{"SectorId":17,"SectorName":"steel"}] */
/* Use vanilla ES6 JS, and turf.js if necessary */
const sectors = [
"solid-waste-disposal",
"manure-management",
"oil-and-gas-production-and-transport",
"coal-mining",
"cement",
"electricity-generation",
"oil-and-gas-refining",
"aviation",
"international-aviation",
"copper-mining",
"shipping",
"iron-mining",
"bauxite-mining",
"domestic-aviation",
"road-transportation",
"enteric-fermentation",
"steel",
];
const year = 2020;
const limit = 40;
const fetch = require("node-fetch");
const fs = require("fs");
const getSectorData = async (sector) => {
const url = `https://api.dev.climatetrace.org/v0/assets?sectors=${sector}&limit=${limit}&year=${year}`;
const response = await fetch(url);
const json = await response.json();
return json;
};
const getAllSectorData = async () => {
const sectorData = await Promise.all(
sectors.map((sector) => getSectorData(sector))
);
return sectorData;
};
const writeToFile = (data) => {
fs.writeFile("top40_allsectors.geojson", JSON.stringify(data), (err) => {
if (err) throw err;
console.log("The file has been saved!");
});
};
const main = async () => {
const sectorData = await getAllSectorData();
const features = sectorData.flatMap((sector) => sector.features);
const geojson = {
type: "FeatureCollection",
features: features,
};
writeToFile(geojson);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment