diff --git a/Dockerfile b/Dockerfile index 5104812..b8a651c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ LABEL maintainer="BreizhHardware" ADD ntfy.py / ADD requirements.txt / ADD entrypoint.sh / +RUN apk add --no-cache sqlite-dev sqlite-libs gcc musl-dev RUN pip install -r requirements.txt # Définir les variables d'environnement pour username et password diff --git a/README.md b/README.md index 06d719c..711884d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ services: ## TODO: - [x] Dockerize the ntfy.py - [x] Add the watched repos list as a parameter -- [ ] Add the application version as a database +- [x] Add the application version as a database - [ ] Add the watched repos list as a web interface # Bash setup-notify.sh ## Description: diff --git a/ntfy.py b/ntfy.py index 0769925..46d6065 100644 --- a/ntfy.py +++ b/ntfy.py @@ -3,6 +3,7 @@ import time import os import logging import json +import sqlite3 # Configurer le logger logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @@ -15,11 +16,19 @@ if not watched_repos_list: logger.error("Aucun dépôt n'a été spécifié. Veuillez spécifier les dépôts à surveiller dans l'environnement GHREPO") exit(1) -# Dictionnaire pour stocker les versions précédentes -previous_versions = {} +# Connexion à la base de données pour stocker les versions précédentes +db_path = 'ghntfy_versions.db' +conn = sqlite3.connect(db_path) +cursor = conn.cursor() + +# Création de la table si elle n'existe pas +cursor.execute('''CREATE TABLE IF NOT EXISTS versions + (repo TEXT PRIMARY KEY, version TEXT)''') +conn.commit() logger.info("Démarrage de la surveillance des versions...") + def get_latest_releases(watched_repos): releases = [] for repo in watched_repos: @@ -45,13 +54,16 @@ def send_to_ntfy(releases, auth, url): app_url = release['html_url'] # Obtenir l'URL de l'application # Vérifier si la version a changé depuis la dernière fois - if app_name in previous_versions and previous_versions[app_name] == version_number: + cursor.execute("SELECT version FROM versions WHERE repo=?", (app_name,)) + previous_version = cursor.fetchone() + if previous_version and previous_version[0] == version_number: logger.info(f"La version de {app_name} n'a pas changé. Pas de notification envoyée.") continue # Passer à l'application suivante message = f"Nouvelle version: {version_number}\nPour: {app_name}\n{app_url}" # Mettre à jour la version précédente pour cette application - previous_versions[app_name] = version_number + cursor.execute("INSERT OR REPLACE INTO versions (repo, version) VALUES (?, ?)", (app_name, version_number)) + conn.commit() headers = {"Authorization": f"Basic {auth}", "Content-Type": "text/plain"} response = requests.post(f"{url}", headers=headers, data=message) @@ -76,5 +88,6 @@ if __name__ == "__main__": time.sleep(timeout) # Attendre une heure avant de vérifier à nouveau else: logger.error("Usage: python ntfy.py") - logger.error("auth: can be generataed by the folowing command: echo -n 'username:password' | base64 and need to be stored in a file named auth.txt") + logger.error( + "auth: can be generataed by the folowing command: echo -n 'username:password' | base64 and need to be stored in a file named auth.txt") logger.error("NTFY_URL: the url of the ntfy server need to be stored in an environment variable named NTFY_URL") diff --git a/requirements.txt b/requirements.txt index 077c95d..e8dedf8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests==2.31.0 \ No newline at end of file +requests==2.31.0 +pysqlite3==0.5.2 \ No newline at end of file