def add_main_dir(m_dir): #check if given directory exists if not os.path.isdir(m_dir): return False #if it exists else: json_maste_file = "master_dirs.json" master_directories = {} if not os.path.isfile("data/" + json_maste_file): master_directories = {} pylavor.json_write("data", json_maste_file, master_directories) else: master_directories = pylavor.json_read("data", json_maste_file) #check if the directory is already on the list for i, b in master_directories.items(): if i == m_dir: return False #check all files in the master folder all_directories_in_master = get_dir_in_path(m_dir) master_directories[m_dir] = all_directories_in_master pylavor.json_write("data", json_maste_file, master_directories)
def add_insertion(self): logging.info("Logging the event in to the webpage database.") filename = f"{pylavor.get_valid_filename(self.name)}.json" location = f"data/databases" all_insertions = pylavor.json_read(location, filename) now = datetime.datetime.now() self.last_contact_time = now.__str__() new_insertion = {} if all_insertions[list(all_insertions.keys())[-1]]["status"] == True: new_insertion["status"] = False new_insertion["date"] = now.__str__() else: new_insertion["status"] = True new_insertion["date"] = now.__str__() all_insertions[len(all_insertions)] = new_insertion pylavor.json_write(location, filename, all_insertions) logging.info("Event inserted correctly.")
def previous_insertion(self): logging.debug( "Getting data from the database to check the previous insertion.") all_insertions = pylavor.json_read( f"data/databases", f"{pylavor.get_valid_filename(self.name)}.json") logging.debug(list(all_insertions.keys())) return all_insertions[list(all_insertions.keys())[-1]]["status"]
def get_webpage_database(): try: webpages_to_check = pylavor.json_read("data", "webpages_to_check.json") logging.debug("Webpages loaded from json correctly.") except: webpages_to_check = {} pylavor.json_write("data", "webpages_to_check.json", webpages_to_check) return webpages_to_check
def send_alert_email(self, status, timedate): logging.info( f"Starting the process of sending an email alert for {self.name}.") if status == True: status = "ONLINE" else: status = "OFFLINE" location = "data" filename = "email_data.json" logging.info("Loading email credentials.") try: email_credentials = pylavor.json_read(location, filename) except: logging.debug( "Could not locate the .json file with the email credentials, creating one." ) email_credentials = { "smtp_server": "smtp.emailserver.com", "smtp_port": "465", "from_address": "*****@*****.**", "pass": "******" } pylavor.json_write(location, filename, email_credentials) email_data = { "smtp_server": email_credentials["smtp_server"], "smtp_port": email_credentials["smtp_port"], "from_address": email_credentials["from_address"], "pass": email_credentials["pass"], "to_address": self.contactEmail, "subject": f"Webpage {self.name} is {status}", "body": f"The webpage {self.name}, on the URL {self.webAddress}, went {status} at {timedate}." } logging.info("Sending alert email.") email_sender.sendEmail(email_data)