def rate_limiter(rl_params):
''' A very basic rate limiter which limits to 1 request per X seconds to the api'''
# Please respect the parties providing these free api's to us and do not modify this code.
# If I suspect any abuse I will revoke all api keys and require all users
# to have a personal api key for all services.
# Thank you
if not rl_params:
return
monitor = xbmc.Monitor()
win = xbmcgui.Window(10000)
rl_name = rl_params[0]
rl_delay = rl_params[1]
cur_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
prev_timestamp = try_parse_int(win.getProperty("ratelimiter.%s" % rl_name))
if (prev_timestamp + rl_delay) > cur_timestamp:
sec_to_wait = (prev_timestamp + rl_delay) - cur_timestamp
log_msg(
"Rate limiter active for %s - delaying request with %s seconds - "
"Configure a personal API key in the settings to get rid of this message and the delay." %
(rl_name, sec_to_wait), xbmc.LOGNOTICE)
while sec_to_wait and not monitor.abortRequested():
monitor.waitForAbort(1)
# keep setting the timestamp to create some sort of queue
cur_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
win.setProperty("ratelimiter.%s" % rl_name, "%s" % cur_timestamp)
sec_to_wait -= 1
# always set the timestamp
cur_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
win.setProperty("ratelimiter.%s" % rl_name, "%s" % cur_timestamp)
del monitor
del win
评论列表
文章目录