diff --git a/README.md b/README.md index 571be4f..81f3649 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,2 @@ -A fork of +this is a repository of useful python scripts -https://github.com/ReneCareenium/rengobot - -A discord bot for playing rengo games! - -# Dependencies -- sgf-render -- python-discord -- python-sgfmill - -Make sure to run the bot in an environment with read/write permissions \ No newline at end of file diff --git a/python_website_monitor/credentials.txt b/python_website_monitor/credentials.txt new file mode 100644 index 0000000..c732ee9 --- /dev/null +++ b/python_website_monitor/credentials.txt @@ -0,0 +1,2 @@ +email +password \ No newline at end of file diff --git a/python_website_monitor/server.py b/python_website_monitor/server.py new file mode 100644 index 0000000..389adb5 --- /dev/null +++ b/python_website_monitor/server.py @@ -0,0 +1,120 @@ +import requests +import time +import smtplib +from email.mime.text import MIMEText +import sqlite3 +from datetime import datetime, timedelta + +def read_credentials(file_path): + with open(file_path, 'r') as file: + lines = file.readlines() + email = lines[0].strip() + password = lines[1].strip() + return email, password + +def check_website(url, timeout=5): + try: + response = requests.get(url, timeout=timeout) + return response.status_code == 200 + except requests.RequestException: + return False + +def send_email(subject, body, to_email, from_email, from_password, smtp_server): + msg = MIMEText(body) + msg['Subject'] = subject + msg['From'] = from_email + msg['To'] = to_email + + with smtplib.SMTP_SSL(smtp_server, 465) as server: + server.login(from_email, from_password) + server.sendmail(from_email, to_email, msg.as_string()) + +def create_database(db_path): + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS website_status ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp DATETIME NOT NULL, + status INTEGER NOT NULL + ) + ''') + conn.commit() + conn.close() + +def log_website_status(db_path, status): + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + cursor.execute(''' + INSERT INTO website_status (timestamp, status) + VALUES (?, ?) + ''', (timestamp, 1 if status else 0)) + conn.commit() + conn.close() + +def get_last_status(db_path): + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute(''' + SELECT status FROM website_status + ORDER BY id DESC LIMIT 1 + ''') + result = cursor.fetchone() + conn.close() + return result[0] if result else None + +def print_email_sent(status_str, timestamp): + print(f"[{timestamp}] Email sent: Website is now {status_str}") + +def cleanup_old_logs(db_path, days_to_keep=10): + cutoff_date = (datetime.now() - timedelta(days=days_to_keep)).strftime('%Y-%m-%d %H:%M:%S') + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + cursor.execute(''' + DELETE FROM website_status + WHERE timestamp < ? + ''', (cutoff_date,)) + deleted_rows = cursor.rowcount + conn.commit() + conn.close() + return deleted_rows + +def main(): + url = "http://petrovv.com" + to_email = "nikola@petrovv.com" + check_interval = 600 # 10 minutes in seconds + credentials_file = "credentials.txt" + db_path = "website_status.db" + smtp_server = "mail.petrovv.com" + + create_database(db_path) + from_email, from_password = read_credentials(credentials_file) + + last_status = None + last_cleanup = datetime.now() + + while True: + current_time = datetime.now() + current_status = check_website(url) + log_website_status(db_path, current_status) + + if last_status is None or current_status != (last_status == 1): + status_str = "up" if current_status else "down" + subject = f"Website Status Change: {status_str}" + body = f"The website {url} is now {status_str} at {current_time.strftime('%Y-%m-%d %H:%M:%S')}." + send_email(subject, body, to_email, from_email, from_password, smtp_server) + print_email_sent(status_str, current_time.strftime('%Y-%m-%d %H:%M:%S')) + last_status = 1 if current_status else 0 + + # Perform cleanup once per day + if (current_time - last_cleanup).days >= 1: + deleted = cleanup_old_logs(db_path, 10) + if deleted > 0: + print(f"[{current_time.strftime('%Y-%m-%d %H:%M:%S')}] Cleanup: Deleted {deleted} logs older than 10 days") + last_cleanup = current_time + + time.sleep(check_interval) + +if __name__ == "__main__": + main() diff --git a/.gitignore b/rengobot/.gitignore similarity index 100% rename from .gitignore rename to rengobot/.gitignore diff --git a/rengobot/README.md b/rengobot/README.md new file mode 100644 index 0000000..571be4f --- /dev/null +++ b/rengobot/README.md @@ -0,0 +1,12 @@ +A fork of + +https://github.com/ReneCareenium/rengobot + +A discord bot for playing rengo games! + +# Dependencies +- sgf-render +- python-discord +- python-sgfmill + +Make sure to run the bot in an environment with read/write permissions \ No newline at end of file diff --git a/rengobot.py b/rengobot/rengobot.py similarity index 100% rename from rengobot.py rename to rengobot/rengobot.py diff --git a/sgfengine.py b/rengobot/sgfengine.py similarity index 100% rename from sgfengine.py rename to rengobot/sgfengine.py