From 79d6f8678481bcfbcdc700444ad7f2c717b58d65 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Mon, 17 Jun 2019 15:46:29 -0400 Subject: [PATCH] 30 minute cache for false --- sopel_modules/SpiceBot/Sherlock.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sopel_modules/SpiceBot/Sherlock.py b/sopel_modules/SpiceBot/Sherlock.py index d8ec4ec..50193c5 100644 --- a/sopel_modules/SpiceBot/Sherlock.py +++ b/sopel_modules/SpiceBot/Sherlock.py @@ -9,6 +9,7 @@ from .Tools import read_directory_json_to_dict import os from fake_useragent import UserAgent import requests +import time class Sherlock(): @@ -27,12 +28,16 @@ class Sherlock(): for social_network in list(valid_usernames_dict.keys()): self.dict[social_network] = valid_usernames_dict[social_network] - self.dict[social_network]["cache"] = [] + self.dict[social_network]["cache"] = dict() def check_network(self, username, social_network): - if username in self.dict[social_network]["cache"]: - return True + if username in list(self.dict[social_network]["cache"].keys()): + if not self.dict[social_network]["cache"][username]["exists"]: + if time.time() - self.dict[social_network]["cache"][username]["time"] <= 1800: + return True + else: + return True url = self.dict.get(social_network).get("url").format(username) error_type = self.dict.get(social_network).get("errorType") @@ -63,8 +68,11 @@ class Sherlock(): if error in r.url: user_exists = False + if username not in list(self.dict[social_network]["cache"].keys()): + self.dict[social_network]["cache"][username] = {} + self.dict[social_network]["cache"][username]["time"] = time.time() + self.dict[social_network]["cache"][username]["exists"] = user_exists if user_exists: - self.dict[social_network]["cache"].append(username) return True else: return False