xView Posted October 19, 2020 Share Posted October 19, 2020 (edited) const fetch = require('node-fetch'); const url = 'http://api.weatherapi.com/v1/current.json?key=c5aab2441e9248e49ee81903201910&q=bucharest'; setInterval(() => { fetch(url) .then(res => res.json()) .then(json => { let code = json.current.condition.code; switch (code) { case 1000: mp.world.weather = 'EXTRASUNNY' break; case 1003: mp.world.weather = 'CLOUDS' break; case 1006: mp.world.weather = 'CLOUDS' break; case 1066: mp.world.weather = 'SNOW' break; case 1135: mp.world.weather = 'FOGGY' break; case 1183: mp.world.weather = 'RAIN' break; case 1189: mp.world.weather = 'RAIN' break; case 1273: mp.world.weather = 'THUNDER' break; default: mp.world.weather = 'CLEAR' } console.log("[WEATHER-SYNC] Server weather is synced with Bucharest (" + json.current.condition.text + ")"); }) .catch(function(err){ console.log("There was an error running the weather.js script ! ", err) }); }, 600000) Requirements: npm i request npm i request-promise In the API call change the 'q' parameter to the city you want to sync to, and provide your API key from WeatherAPI The script syncs the weather once evry 10 minutes, you can change that by modifying the 'setInterval' loop. Edited April 19, 2021 by xView 3 Link to comment Share on other sites More sharing options...
SugarD-x Posted November 20, 2020 Share Posted November 20, 2020 (edited) Seeing how the request and request-promise packages are now deprecated, has anyone attempted using the node-fetch package with this yet? Edit: I was able to make it work with the node-fetch package using your code with slight modifications: const fetch = require('node-fetch'); const url = 'http://api.weatherapi.com/v1/current.json?key=c5aab2441e9248e49ee81903201910&q=bucharest'; setInterval(() => { fetch(url) .then(res => res.json()) .then(json => { let code = json.current.condition.code; switch (code) { case 1000: mp.world.weather = 'EXTRASUNNY' break; case 1003: mp.world.weather = 'CLOUDS' break; case 1006: mp.world.weather = 'CLOUDS' break; case 1066: mp.world.weather = 'SNOW' break; case 1135: mp.world.weather = 'FOGGY' break; case 1183: mp.world.weather = 'RAIN' break; case 1189: mp.world.weather = 'RAIN' break; case 1273: mp.world.weather = 'THUNDER' break; default: mp.world.weather = 'CLEAR' } console.log("[WEATHER-SYNC] Server weather is synced with Bucharest (" + json.current.condition.text + ")"); }) .catch(function(err){ console.log("There was an error running the weather.js script ! ", err) }); }, 600000) (I had to convert it back to your format, as I made modifications for my own usage, so if something is broken, please let me know). Additionally, instead of using the deprecated request and request-promise packages, I used the node-fetch package instead: npm i node-fetch Hopefully this helps! Edit #2: For the new people, please change the key in the URL to your own! Edited November 20, 2020 by SugarD-x 2 Link to comment Share on other sites More sharing options...
MrEngineer98 Posted February 5, 2021 Share Posted February 5, 2021 (edited) Sorry for my ignorance, but this script goes in packages right? Edited February 5, 2021 by MrEngineer98 error 1 Link to comment Share on other sites More sharing options...
SugarD-x Posted March 9, 2021 Share Posted March 9, 2021 On 2/4/2021 at 9:38 PM, MrEngineer98 said: Sorry for my ignorance, but this script goes in packages right? Yes. It is server-side. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now