Skip to content

Instantly share code, notes, and snippets.

@flavienbonvin
Created April 16, 2020 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavienbonvin/078bc85890d46202757197ed5695623d to your computer and use it in GitHub Desktop.
Save flavienbonvin/078bc85890d46202757197ed5695623d to your computer and use it in GitHub Desktop.
api
const fs = require("fs");
const Papa = require("papaparse");
const axios = require("axios");
const csvFilePath = "user.csv";
const apiKey = "";
const arrayWeatherFirstLogin = [];
const errors = [];
const readCSV = async (filePath) => {
const csvFile = fs.readFileSync(filePath);
const csvData = csvFile.toString();
return new Promise((resolve) => {
Papa.parse(csvData, {
header: true,
encoding: "UTF-8",
complete: (results) => {
console.log("Complete", results.data.length, "records.");
resolve(results.data);
},
});
});
};
const getHistoricalWeather = async (city, country, date) => {
const cityClean = city.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
const params = {
access_key: apiKey,
query: cityClean,
country,
historical_date: date,
hourly: 1,
interval: 24,
};
const res = await axios.get("https://api.weatherstack.com/historical", {
params,
});
try {
const temp = res.data.historical[date].hourly[0].weather_descriptions[0];
return temp;
} catch (e) {
errors.push({ city, country, date });
console.log(res.data.error.type);
return "Error";
}
};
const handleWeatherInsertion = (weatherArray, weather) => {
const weatherTemp = weatherArray.filter(
(item) => item.weather === weather
)[0];
if (weatherTemp) {
weatherArray[weatherArray.indexOf(weatherTemp)].count++;
} else {
weatherArray.push({
weather,
count: 1,
});
}
};
const getWeatherInformation = async () => {
const customers = await readCSV(csvFilePath);
const customersWithCity = customers.filter((item) => item.Ville);
console.log(
`Number of customers: ${customers.length}, with city: ${customersWithCity.length}`
);
for (var i = 0; i < customersWithCity.length; i++) {
if (i % 10 === 0) {
console.log(`Currently at step ${i}`);
}
const customer = customersWithCity[i];
const weatherFirstLogin = await getHistoricalWeather(
customer.Ville,
customer.Pays,
customer["First Login"].split(" ")[0]
);
handleWeatherInsertion(arrayWeatherFirstLogin, weatherFirstLogin);
}
console.log(arrayWeatherFirstLogin);
};
const testAPI = async () => {
const customers = await readCSV(csvFilePath);
const customersWithCity = customers.filter((item) => item.Ville);
const customer = customersWithCity[10];
const weatherFirstLogin = await getHistoricalWeather(
customer.Ville,
customer.Pays,
customer["First Login"].split(" ")[0]
);
handleWeatherInsertion(arrayWeatherFirstLogin, weatherFirstLogin);
console.log(arrayWeatherFirstLogin);
};
//testAPI();
getWeatherInformation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment