使用 Node js,如何使用 yahoo weather 在单个请求中获取多个城市的天气信息 api

Using Node js, how to get weather feeds for multiple cities in single request using yahoo weather api

我正在使用 yahoo weather api 获取单个城市的天气信息,现在我想在单个请求中获取多个城市的天气信息,我该如何使用 yahoo api。我也想知道,有没有api yahoo 提供获取任何国家/地区的城市列表。

我的weather.js

 import OAuth from 'oauth';

    const header = {
      "X-Yahoo-App-Id": "myappid"
    };

   const request = new OAuth.OAuth(
         null,
         null,
         'myconsumerkey',
         'myconsumersecret',
         '1.0',
         null,
         'HMAC-SHA1',
         null,
         header
        );

request.get('https://weather-ydn-yql.media.yahoo.com/forecastrss?w=713169&format=json', null,null, function (err, data, result) {
       if (err) {
         console.log(err);
       } else {
         console.log(data)
       }
     });

使用此代码我只能获取一个城市的天气详细信息我想一次获取多个城市的天气详细信息。

提前致谢!!

因此阅读文档似乎无法将一批位置发送到 Yahoo Weather API。但是您可以做的是 .map() 遍历一组位置并发出多个请求。

https://developer.yahoo.com/weather/documentation.html#params

由于 OAuth 1.0 是一个回调,我用 new Promise() 包装了它,这将为我们提供一系列未实现的承诺。最后,Promise.all() 方法 returns 一个单一的 Promise,当所有作为可迭代传递的 promise 都已实现时,它就会实现。

const OAuth = require('oauth')

const header = {
    'X-Yahoo-App-Id': 'your-app-id',
}

const request = new OAuth.OAuth(null, null, 'your-consumer-key', 'your-consumer-secret', '1.0', null, 'HMAC-SHA1', null, header)

const locations = ['pittsburgh,pa', 'london']

const getWeatherData = () =>
    Promise.all(
        locations.map(
            location =>
                new Promise((resolve, reject) =>
                    request.get(`https://weather-ydn-yql.media.yahoo.com/forecastrss?location=${location}&format=json`, null, null, (err, data) => {
                        if (err) return reject(err)
                        return resolve(data)
                    })
                )
        )
    )

const main = async () => {
    const test = await getWeatherData()

    console.log(test)
}

main()

我已经用 API 对此进行了测试,这里是上面代码的示例响应。

[
    '{"location":{"city":"Pittsburgh","region":" PA","woeid":2473224,"country":"United States","lat":40.431301,"long":-79.980698,"timezone_id":"America/New_York"},"current_observation":{"wind":{"chill":32,"direction":280,"speed":5.59},"atmosphere":{"humidity":70,"visibility":10.0,"pressure":29.03,"rising":0},"astronomy":{"sunrise":"6:42 am","sunset":"7:59 pm"},"condition":{"text":"Partly Cloudy","code":30,"temperature":37},"pubDate":1586862000},"forecasts":[{"day":"Tue","date":1586836800,"low":38,"high":45,"text":"Mostly Cloudy","code":28},{"day":"Wed","date":1586923200,"low":32,"high":47,"text":"Partly Cloudy","code":30},{"day":"Thu","date":1587009600,"low":31,"high":45,"text":"Partly Cloudy","code":30},{"day":"Fri","date":1587096000,"low":35,"high":42,"text":"Rain And Snow","code":5},{"day":"Sat","date":1587182400,"low":35,"high":51,"text":"Scattered Showers","code":39},{"day":"Sun","date":1587268800,"low":42,"high":59,"text":"Rain","code":12},{"day":"Mon","date":1587355200,"low":43,"high":55,"text":"Mostly Cloudy","code":28},{"day":"Tue","date":1587441600,"low":37,"high":58,"text":"Partly Cloudy","code":30},{"day":"Wed","date":1587528000,"low":44,"high":61,"text":"Partly Cloudy","code":30},{"day":"Thu","date":1587614400,"low":50,"high":59,"text":"Mostly Cloudy","code":28}]}',
    '{"location":{"city":"London","region":" England","woeid":44418,"country":"United Kingdom","lat":51.506401,"long":-0.12721,"timezone_id":"Europe/London"},"current_observation":{"wind":{"chill":46,"direction":70,"speed":6.84},"atmosphere":{"humidity":50,"visibility":10.0,"pressure":30.27,"rising":0},"astronomy":{"sunrise":"6:04 am","sunset":"7:58 pm"},"condition":{"text":"Mostly Sunny","code":34,"temperature":49},"pubDate":1586862000},"forecasts":[{"day":"Tue","date":1586818800,"low":38,"high":54,"text":"Partly Cloudy","code":30},{"day":"Wed","date":1586905200,"low":34,"high":62,"text":"Mostly Sunny","code":34},{"day":"Thu","date":1586991600,"low":38,"high":68,"text":"Partly Cloudy","code":30},{"day":"Fri","date":1587078000,"low":45,"high":62,"text":"Rain","code":12},{"day":"Sat","date":1587164400,"low":45,"high":60,"text":"Rain","code":12},{"day":"Sun","date":1587250800,"low":42,"high":63,"text":"Partly Cloudy","code":30},{"day":"Mon","date":1587337200,"low":44,"high":64,"text":"Scattered Showers","code":39},{"day":"Tue","date":1587423600,"low":44,"high":66,"text":"Partly Cloudy","code":30},{"day":"Wed","date":1587510000,"low":45,"high":67,"text":"Mostly Cloudy","code":28},{"day":"Thu","date":1587596400,"low":44,"high":65,"text":"Mostly Cloudy","code":28}]}',
]