def __init__(self,config_path): self.config_path = config_path # Reading in our configuration file try: with open(config_path,"r") as f: # Check the first byte to see if theres anything in there at all if not f.seek(0): self.config = {} else: self.config = json.load(f) except ValueError: print("There was an error parsing your json file.") self.checker = LinkChecker(self.config)
def add_links(self,category,links,delim=","): link_entries = self.split_input(links,delim) links = self.config[str.lower(category)]["links"] for link_entry in link_entries: link_hash = LinkChecker.get_link_hash(link_entry) # First we check for duplicate pages, we don't want to send multiple alerts # for the same page. We also assume (As much as I hate assumtions) the user is # smart enough to not add the same link for different categories. for link in links: if link_hash == link["hash"]: print("The link: \"" + repr(link) + "\" is already being checked for... skipping...") break else: links.append({"url": link_entry, "hash": link_hash}) return True
class Service: def __init__(self,config_path): self.config_path = config_path # Reading in our configuration file try: with open(config_path,"r") as f: # Check the first byte to see if theres anything in there at all if not f.seek(0): self.config = {} else: self.config = json.load(f) except ValueError: print("There was an error parsing your json file.") self.checker = LinkChecker(self.config) # Adds a new blank category to the current config def add_category(self,category,sender,recipients=None,links=None,delim=","): category = str.lower(category) config = self.config if category not in config: config[category] = { "status": "True", "sender": sender, "recipients": [], "links":[] } if recipients: self.add_recipients(category,recipients) if links: self.add_links(category,links) # "Normalizes" and splits input def split_input(self,input,delim): return map(str.lower,input.split(delim)) def add_recipients(self,category,recipients,delim=","): try: recipients = self.split_input(recipients,delim) category = self.config[category] # Preventing duplicates, it's easy for recipients because email addresses # are unique and won't have some similarity metric to watch out for like pages have category["recipients"] = list(set(recipients).union(category["recipients"])) return True except KeyError: print("Category: " + repr(category) + " does not exist") return False def add_links(self,category,links,delim=","): link_entries = self.split_input(links,delim) links = self.config[str.lower(category)]["links"] for link_entry in link_entries: link_hash = LinkChecker.get_link_hash(link_entry) # First we check for duplicate pages, we don't want to send multiple alerts # for the same page. We also assume (As much as I hate assumtions) the user is # smart enough to not add the same link for different categories. for link in links: if link_hash == link["hash"]: print("The link: \"" + repr(link) + "\" is already being checked for... skipping...") break else: links.append({"url": link_entry, "hash": link_hash}) return True def check(self): return self.checker.check_categories() def update(self,path=None): with open(path or self.config_path,'w+') as f: f.write(json.dumps(self.config))