Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var { cities } = require('./cities');
var fetch = require('node-fetch');
function arrayRandom(arr) {
return arr[Math.floor((Math.random() * arr.length))];
}
module.exports = async (event, context) => {
let cityToGetWeather = "";
if (event.body.city) {
cityToGetWeather = event.body.city;
} else {
cityToGetWeather = arrayRandom(cities);
}
const geocodeRes = await fetch(`https://geocode.xyz/${cityToGetWeather}?json=1`);
const { latt, longt } = await geocodeRes.json();
const weatherRes = await fetch(`http://www.7timer.info/bin/civillight.php?lon=7${longt}&lat=${latt}&unit=metric&output=json`);
const { dataseries } = await weatherRes.json();
return context.succeed(`${cityToGetWeather} is currently ${dataseries[0].weather} with a temperature between ${dataseries[0].temp2m.min}C and ${dataseries[0].temp2m.max}C`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment