From 082983806392b9858f335f267253977b6ed89918 Mon Sep 17 00:00:00 2001 From: pluja Date: Tue, 6 Oct 2020 18:11:28 +0200 Subject: [PATCH] Limit petitions to Nitter on 60s interval Default: use cache if cache is not older than 1 minute --- app/routes.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 3ced2ca..1e6151d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -75,14 +75,19 @@ def twitter(page=0): avatarPath = "img/avatars/1.png" posts = [] - if page == 0: - cache_file = glob.glob("app/cache/{}*".format(current_user.username)) + cache_file = glob.glob("app/cache/{}_*".format(current_user.username)) + + time_diff = round(time.time()-os.path.getmtime(cache_file[0])) + # If cache file is more than 1 minute old + if page == 0 and time_diff > 60: if cache_file: - os.remove(cache_file[0]) + for f in cache_file: + os.remove(f) feed = getFeed(followingList) cache_file = "{u}_{d}.json".format(u=current_user.username, d=time.strftime("%Y%m%d-%H%M%S")) with open("app/cache/{}".format(cache_file), 'w') as fp: json.dump(feed, fp) + # Else, refresh feed else: try: cache_file = glob.glob("app/cache/{}*".format(current_user.username))[0]