Weather Undground API call limit per minute

时间秒杀一切 提交于 2019-12-24 09:29:50

问题


I have to limit my API request to 10 calls per minute, how can I modify the for loops to accomplish this?

I am trying to add in time.sleep(8) in the for observation loop without any luck... Any ideas?

import arrow # learn more: https://python.org/pypi/arrow
from WunderWeather import weather # learn more: https://python.org/pypi/WunderWeather
import time

api_key = ''
extractor = weather.Extract(api_key)
zip = '53711'

# get 20170101 00:00
begin_date = arrow.get("2017","YYYY")
# get 20171231 23:00
end_date = arrow.get("2018","YYYY").shift(hours=-1)
for date in arrow.Arrow.range('hour',begin_date,end_date):
  # get date object for feature
  # http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.date
  date_weather = extractor.date(zip,date.format('YYYYMMDD'))

  # use shortcut to get observations and data
  # http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.date.Observation
  for observation in date_weather.observations:
    time.sleep(8)
    print("Date:",observation.date_pretty)
    print("Temp:",observation.temp_f)

回答1:


A possible explanation of why you are still exceeding the API limit might have to do with the line on which you are adding the time wait. If the API response you are getting contains no observations, the inner loop won't execute. So first I would try to move the time wait in the outer loop right after the API call.

You might also consider using something like loopingCall from twisted to schedule your task to run every X seconds http://twistedmatrix.com/documents/9.0.0/core/howto/time.html




回答2:


Depending on how realtime you want your data or you can afford to be a day behind, you could get all observations for a date in the past which would be one API call to retrieve data for a day(or it could be an end of day summary for the current day's observations).

Alternatively, if you're trying to get the current weather every x minutes or so (under the limit) I'd use some sort of loop with a timer (or possibly twisted which seems to abstract the "loop") but make a call to one of the following (depending on what you're looking for). Your current code is looking for dates in the past but these other endpoints are for the current day.

You don't want the timer in the observations loop since, as mentioned above, there might be none.

http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.hourly_daycast

http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.today_now

which can be called similar to the following examples http://wunderweather.readthedocs.io/en/latest/index.html#additional-examples



来源:https://stackoverflow.com/questions/47874185/weather-undground-api-call-limit-per-minute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!