Skip to content

Commit

Permalink
rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
jurialmunkey committed Aug 5, 2019
1 parent 58a0ba4 commit 3006eab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@
import xbmc
import datetime
import simplecache
import time
import xml.etree.ElementTree as ET
from globals import TMDB_API, _tmdb_apikey, _language, OMDB_API, _omdb_apikey, OMDB_ARG, _addonlogname, _addonname
_cache = simplecache.SimpleCache()
_waittime = 1


def cache_last_used_time(func):
"""
Simple rate limiter
"""
def decorated(*args, **kwargs):
cache_name = _addonname + '.last_used_time'
cached_time = _cache.get(cache_name)
current_time = time.time()
time_diff = cached_time - current_time
if time_diff < 0:
cached_time = current_time + _waittime
_cache.set(cache_name, cached_time, expiration=datetime.timedelta(days=14))
return func(*args, **kwargs)
else:
xbmc.log(_addonlogname + 'Run after ' + str(cached_time - current_time) + ' secs', level=xbmc.LOGNOTICE)
time.sleep(time_diff)
return func(*args, **kwargs)
return decorated


def use_mycache(cache_days=14, suffix=''):
Expand Down
8 changes: 7 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import xbmcplugin
import lib.utils
import lib.apis
from lib.apis import cache_last_used_time
from urllib import urlencode
from urlparse import parse_qsl
from lib.globals import _url, _handle, _addonpath, _addonlogname, CATEGORIES, MAINFOLDER, IMAGEPATH, _omdb_apikey, GENRE_IDS
Expand Down Expand Up @@ -442,5 +443,10 @@ def router(self):
self.list_categories()


if __name__ == '__main__':
@cache_last_used_time
def run_plugin():
Plugin()


if __name__ == '__main__':
run_plugin()

0 comments on commit 3006eab

Please sign in to comment.