diff --git a/sopel_modules/SpiceBot/Sherlock.py b/sopel_modules/SpiceBot/Sherlock.py index 05df6b4..9ff385a 100644 --- a/sopel_modules/SpiceBot/Sherlock.py +++ b/sopel_modules/SpiceBot/Sherlock.py @@ -29,6 +29,40 @@ class Sherlock(): self.dict[sherlockdict] = valid_usernames_dict[sherlockdict] self.dict[sherlockdict]["cache"] = dict() + def check_network(self, username, social_network): + url = self.dict.get(social_network).get("url").format(username) + error_type = self.dict.get(social_network).get("errorType") + cant_have_period = self.dict.get(social_network).get("noPeriod") + + if ("." in username) and (cant_have_period == "True"): + while ("." in username): + username = username.replace(".", '') + + r, error_type = self.make_request(url=url, error_type=error_type, social_network=social_network) + + if error_type == "message": + error = self.dict.get(social_network).get("errorMsg") + # Checks if the error message is in the HTML + if error not in r.text: + return True + else: + return False + + elif error_type == "status_code": + # Checks if the status code of the repsonse is 404 + if not r.status_code == 404: + return True + else: + return False + + elif error_type == "response_url": + error = self.dict.get(social_network).get("errorUrl") + # Checks if the redirect url is the same as the one defined in data.json + if error not in r.url: + return True + else: + return False + def make_request(self, url, error_type, social_network): try: r = requests.get(url, headers=self.header) diff --git a/sopel_modules/SpiceBot_Core_Commands/Sherlock.py b/sopel_modules/SpiceBot_Core_Commands/Sherlock.py index 6c58d55..452c6c4 100644 --- a/sopel_modules/SpiceBot_Core_Commands/Sherlock.py +++ b/sopel_modules/SpiceBot_Core_Commands/Sherlock.py @@ -13,7 +13,6 @@ import sopel_modules.SpiceBot as SpiceBot @sopel.module.commands('sherlock', 'username') def bot_command_sherlock(bot, trigger): - data = SpiceBot.sherlock.dict netlist = list(SpiceBot.sherlock.dict.keys()) username = spicemanip.main(trigger.sb["args"], 1) or trigger.nick @@ -28,43 +27,15 @@ def bot_command_sherlock(bot, trigger): bot.osd("Checking username " + username + " in " + checklistname + " network.") - inlist = [] - notinlist = [] + inlist, notinlist = [], [] for social_network in checklist: - url = data.get(social_network).get("url").format(username) - error_type = data.get(social_network).get("errorType") - cant_have_period = data.get(social_network).get("noPeriod") - - if ("." in username) and (cant_have_period == "True"): - while ("." in username): - username = username.replace(".", '') - - r, error_type = SpiceBot.sherlock.make_request(url=url, error_type=error_type, social_network=social_network) - - if error_type == "message": - error = data.get(social_network).get("errorMsg") - # Checks if the error message is in the HTML - if error not in r.text: - inlist.append(social_network) - else: - notinlist.append(social_network) - - elif error_type == "status_code": - # Checks if the status code of the repsonse is 404 - if not r.status_code == 404: - inlist.append(social_network) - else: - notinlist.append(social_network) - - elif error_type == "response_url": - error = data.get(social_network).get("errorUrl") - # Checks if the redirect url is the same as the one defined in data.json - if error not in r.url: - inlist.append(social_network) - else: - notinlist.append(social_network) + in_social = SpiceBot.sherlock.check_network(username, social_network) + if in_social: + inlist.append(social_network) + else: + notinlist.append(social_network) if inlist != []: if checklistname != 'all':