In: Computer Science
i want to create a weather api. which is pullling out information of all the cities(for the whole world) using their names or by their zipcode. and it should change the background image as the temperature is, cold, or hot, or mild etc. i need in help in making this weather api and also it can be in any language. please help me out
You can achieve Your goal using nodejs. With the help of nodejs we can send HTTP request to whether stack API or website which will give us wether information of city. First you need to sign in to wetherstack and mapbox website and generate your own access token.After getting access token just paste your access token to my code and run it.
Run command node app.js City_name it will give wether information of City name you have mentioned in your command
I have done this code in 3 files.please refer this files
gecode file converts name of city to cor-ordinates on map so that we can access the exact location of city with the help of mapbox api
forcast files send request to the wetherStack API to get information of wether
//app.js
const request = require('request');
//geocode and forcast files are store in utils folder
const geo = require('./utils/geocode')
const winfo = require('./utils/forcast')
//getting name of city
const address=process.argv[2]
console.log("address : "+address)
//calling geocode function to convert location name to cor-ordinates like longitude and lattitude
geo(address, (error,{longitude,lattitude,location})=>{
if(error)
{
console.log("Error "+error)
}
else
{
//calling winfo function which is forcast file to get wether information and its parameters are longitude and lattitude
winfo(longitude,lattitude , (error,response1)=>{
if(error)
{
console.log(error)
}
else
{
console.log("In the "+location+" wether is "+response1)
}
})
}
})
//forcast.js
const request = require('request');
const winfo= (co1,co2 , callback)=>{
//link on which we are sending HTTP request , please sign in to wether stack API and put ur access token in link to
//get code working
const url='http://api.weatherstack.com/current?access_key='Put_Your_AccessTocken_Here'&query='+co1+','+co2
request( {url: url , json: true}, function (error, response) {
if(error)
{
callback(error)
}
else
{
//creating output for user
const str1=" "+response.body.current.weather_descriptions+" it is "+response.body.current.temperature+" currently"
const str2=" but it feels like "+response.body.current.feelslike+" degree"
callback(undefined,str1+str2);
}
});
}
module.exports=winfo
//geocode.js
const request = require('request');
const geo= function(address , callback){
//please sign in to mapbox to generate your tocken and put the tocken here
const url1='https://api.mapbox.com/geocoding/v5/mapbox.places/'+address+'.json?access_token=' Put_Your_AccessTocken_Here'
//sending HTTP request
request({url:url1 , json: true}, (error , response)=>{
if(error)
{
callback(error)
}
else if(response.body.features.length === 0)
{
callback("Unable to find location try another search")
}
else
{
callback(undefined,{
lattitude:response.body.features[0].center[0],
longitude:response.body.features[0].center[1],
location:response.body.features[0].place_name
})
}
})
}
module.exports=geo
install node JS and NPM module before using this code