Jump to content
RAGE Multiplayer Community

[Node JS] Real Life Weather In RageMP


Recommended Posts

const req = require('request-promise');
const url = 'http://api.weatherapi.com/v1/current.json?key=c5aab2441e9248e49ee81903201910&q=bucharest';

setInterval(() => { 
  req(url)
  .then(function(data) {
    let jsonData = JSON.parse(data)
    let code = jsonData.current.condition.code

    switch (code) {
        case 1000:
          mp.world.weather = 0
          break;
        case 1003:
          mp.world.weather = 2
          break;
        case 1006:
          mp.world.weather = 2
          break;
        case 1066:
          mp.world.weather = 10
          break;
        case 1135:
          mp.world.weather = 4
          break;
        case 1183:
          mp.world.weather = 6
          break;
        case 1189:
          mp.world.weather = 6
          break;
        case 1273:
          mp.world.weather = 7
          break;
        default:
          mp.world.weather = 1
      }

    console.log("[WEATHER-SYNC] Server weather is synced with Bucharest (" + jsonData.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.

  • Like 1
Link to post
Share on other sites
  • 1 month later...

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 by SugarD-x
Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...