mirror of
https://github.com/BreizhHardware/ntfy_alerts.git
synced 2026-03-18 21:40:38 +01:00
Add github token authentification
This commit is contained in:
12
README.md
12
README.md
@@ -7,8 +7,11 @@ Personal ntfy alerts system
|
||||
This script is used to watch the github repos and send a notification to the ntfy server when a new release is published.
|
||||
## Utilisation:
|
||||
auth and ntfy_url are required to be set as environment variables.
|
||||
|
||||
auth: can be generataed by the folowing command: echo -n 'username:password' | base64
|
||||
|
||||
ntfy_url: the url of the ntfy server including the topic
|
||||
|
||||
````python
|
||||
python ntfy.py
|
||||
````
|
||||
@@ -25,7 +28,8 @@ services:
|
||||
- PASSWORD=password # Required
|
||||
- NTFY_URL=ntfy_url # Required
|
||||
- GHNTFY_TIMEOUT=timeout # Default is 3600 (1 hour)
|
||||
- GHNTFY_WATCHED_REPOS=["username/repo1", "username/repo2"] # Default is empty
|
||||
- GHREPO=["username/repo1", "username/repo2"] # Default is empty
|
||||
- GHNTFY_TOKEN= # Default is empty (Github token)
|
||||
restart: unless-stopped
|
||||
````
|
||||
## TODO:
|
||||
@@ -41,8 +45,11 @@ This script is used to setup the ntfy notification system on ssh login for a new
|
||||
bash setup-notify.sh <ntfy_url> <username> <password> <topic>
|
||||
````
|
||||
ntfy_url: the url of the ntfy server
|
||||
|
||||
username: the username of the user
|
||||
|
||||
password: the password of the user
|
||||
|
||||
topic: the topic of the notification
|
||||
|
||||
This script will create a send-notify.sh in the root of your disk and add the login-notify.sh to the /etc/profile.d/ folder.
|
||||
@@ -54,7 +61,10 @@ This script is used to send a notification to the ntfy server.
|
||||
bash send-notify.sh <ntfy_url> <basic_auth> <topic> <message>
|
||||
````
|
||||
ntfy_url: the url of the ntfy server
|
||||
|
||||
basic_auth: the basic auth of the user
|
||||
|
||||
topic: the topic of the notification
|
||||
|
||||
message: the message of the notification
|
||||
|
||||
|
||||
36
ntfy.py
36
ntfy.py
@@ -6,9 +6,14 @@ import json
|
||||
import sqlite3
|
||||
|
||||
# Configurer le logger
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
github_token = os.environ.get('GHNTFY_TOKEN')
|
||||
github_headers = {}
|
||||
if github_token:
|
||||
github_headers['Authorization'] = f"token {github_token}"
|
||||
|
||||
repo_list_env = os.environ.get('GHREPO')
|
||||
watched_repos_list = json.loads(repo_list_env) if repo_list_env else []
|
||||
|
||||
@@ -23,7 +28,7 @@ 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)''')
|
||||
(repo TEXT PRIMARY KEY, version TEXT, changelog TEXT)''')
|
||||
conn.commit()
|
||||
|
||||
logger.info("Démarrage de la surveillance des versions...")
|
||||
@@ -33,25 +38,40 @@ def get_latest_releases(watched_repos):
|
||||
releases = []
|
||||
for repo in watched_repos:
|
||||
url = f"https://api.github.com/repos/{repo}/releases/latest"
|
||||
response = requests.get(url)
|
||||
response = requests.get(url, headers=github_headers)
|
||||
if response.status_code == 200:
|
||||
release_info = response.json()
|
||||
changelog = get_changelog(repo)
|
||||
releases.append({
|
||||
"repo": repo,
|
||||
"name": release_info["name"],
|
||||
"tag_name": release_info["tag_name"],
|
||||
"html_url": release_info["html_url"]
|
||||
"html_url": release_info["html_url"],
|
||||
"changelog": changelog
|
||||
})
|
||||
else:
|
||||
logger.error(f"Failed to fetch release info for {repo}")
|
||||
return releases
|
||||
|
||||
|
||||
def get_changelog(repo):
|
||||
url = f"https://api.github.com/repos/{repo}/releases"
|
||||
response = requests.get(url, headers=github_headers)
|
||||
if response.status_code == 200:
|
||||
releases = response.json()
|
||||
if releases:
|
||||
latest_release = releases[0]
|
||||
if 'body' in latest_release:
|
||||
return latest_release['body']
|
||||
return "Changelog non disponible"
|
||||
|
||||
|
||||
def send_to_ntfy(releases, auth, url):
|
||||
for release in releases:
|
||||
app_name = release['repo'].split('/')[-1] # Obtenir le nom de l'application à partir du repo
|
||||
version_number = release['tag_name'] # Obtenir le numéro de version
|
||||
app_url = release['html_url'] # Obtenir l'URL de l'application
|
||||
changelog = release['changelog'] # Obtenir le changelog
|
||||
|
||||
# Vérifier si la version a changé depuis la dernière fois
|
||||
cursor.execute("SELECT version FROM versions WHERE repo=?", (app_name,))
|
||||
@@ -60,9 +80,10 @@ def send_to_ntfy(releases, auth, url):
|
||||
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}"
|
||||
message = f"Nouvelle version: {version_number}\nPour: {app_name}\nChangelog:\n{changelog}\n{app_url}"
|
||||
# Mettre à jour la version précédente pour cette application
|
||||
cursor.execute("INSERT OR REPLACE INTO versions (repo, version) VALUES (?, ?)", (app_name, version_number))
|
||||
cursor.execute("INSERT OR REPLACE INTO versions (repo, version, changelog) VALUES (?, ?, ?)",
|
||||
(app_name, version_number, changelog))
|
||||
conn.commit()
|
||||
|
||||
headers = {"Authorization": f"Basic {auth}", "Content-Type": "text/plain"}
|
||||
@@ -88,6 +109,5 @@ 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")
|
||||
|
||||
Reference in New Issue
Block a user