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 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 newWebpage(webpageName, webAddress, contactEmail, contactNumber, sent_notif_on_day): logging.info("Adding a new webpage to the program.") webPages = get_webpage_database() webPage = { webpageName: { "webAddress": webAddress, "contactEmail": contactEmail, "contactNumber": contactNumber, "notification_day": sent_notif_on_day } } now = datetime.datetime.now() new_insertion = {} new_insertion["status"] = True new_insertion["date"] = now.__str__() all_insertions = {0: new_insertion} location = f"data/databases" filename = f"{pylavor.get_valid_filename(webpageName)}.json" pylavor.json_write(location, filename, all_insertions) logging.debug("First event inserted correctly.") webPages.update(webPage) save_webpage_database(webPages) logging.debug(f"Successfully added the webpage: {webpageName}")
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)
def save_webpage_database(webpages_to_save): pylavor.json_write("data", "webpages_to_check.json", webpages_to_save) logging.debug("Webpages saved to json correctly.")
"data/email_data.json"): logging.debug( "All needed files were found to exists, the program can run as expected." ) powerUp() #newWebpage("Motion Olmo", "https://motion.razor.si", "*****@*****.**", "031310333") else: logging.info( "The main webpages db or the db with the email credentials were not found. Will go trough and create the ones that are not found." ) if not pylavor.check_file_exists("data/webpages_to_check.json"): logging.info("The webpages DB file was not found. Creating one.") newWebpage("Internet Holidays", "http://ih.razor.si", "*****@*****.**", "031301330", "") if not pylavor.check_file_exists("data/email_data.json"): logging.info( "The email credentials file was not found. Creating one.") email_credentials = { "smtp_server": "smtp.emailserver.com", "smtp_port": "465", "from_address": "*****@*****.**", "pass": "******" } pylavor.json_write("data", "email_data.json", email_credentials) logging.info( "---------------------------------------------------------------------------------------------" )