mirror of
https://github.com/BreizhHardware/ntfy_alerts.git
synced 2026-01-18 16:37:28 +01:00
fix: use separate database connections for each function
- Refactored `send_gotify.py`, `send_ntfy.py`, and `send_discord.py` to use separate database connections for each function. - Added `get_db_connection` function to create a new database connection. - Updated `github_send_to_gotify`, `docker_send_to_gotify`, `github_send_to_ntfy`, `docker_send_to_ntfy`, `github_send_to_discord`, and `docker_send_to_discord` functions to use the new `get_db_connection` function. - Modified `ntfy.py` to use threading for calling notification functions simultaneously.
This commit is contained in:
50
ntfy.py
50
ntfy.py
@@ -5,6 +5,8 @@ import logging
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import json
|
||||
import threading
|
||||
|
||||
from send_ntfy import (
|
||||
github_send_to_ntfy,
|
||||
docker_send_to_ntfy,
|
||||
@@ -175,6 +177,34 @@ def get_changelog(repo):
|
||||
return latest_release_list["body"]
|
||||
return "Changelog not available"
|
||||
|
||||
def notify_all_services(github_latest_release, docker_latest_release, auth, ntfy_url, gotify_url, gotify_token, discord_webhook_url):
|
||||
threads = []
|
||||
|
||||
if ntfy_url:
|
||||
if github_latest_release:
|
||||
threads.append(threading.Thread(target=github_send_to_ntfy, args=(github_latest_release, auth, ntfy_url)))
|
||||
if docker_latest_release:
|
||||
threads.append(threading.Thread(target=docker_send_to_ntfy, args=(docker_latest_release, auth, ntfy_url)))
|
||||
|
||||
if gotify_url and gotify_token:
|
||||
if github_latest_release:
|
||||
threads.append(threading.Thread(target=github_send_to_gotify, args=(github_latest_release, gotify_token, gotify_url)))
|
||||
if docker_latest_release:
|
||||
threads.append(threading.Thread(target=docker_send_to_gotify, args=(docker_latest_release, gotify_token, gotify_url)))
|
||||
|
||||
if discord_webhook_url:
|
||||
if github_latest_release:
|
||||
threads.append(threading.Thread(target=github_send_to_discord, args=(github_latest_release, discord_webhook_url)))
|
||||
if docker_latest_release:
|
||||
threads.append(threading.Thread(target=docker_send_to_discord, args=(docker_latest_release, discord_webhook_url)))
|
||||
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
start_api()
|
||||
@@ -193,25 +223,9 @@ if __name__ == "__main__":
|
||||
docker_watched_repos_list = get_docker_watched_repos()
|
||||
docker_latest_release = get_latest_docker_releases(docker_watched_repos_list)
|
||||
|
||||
if ntfy_url:
|
||||
if github_latest_release:
|
||||
github_send_to_ntfy(github_latest_release, auth, ntfy_url)
|
||||
if docker_latest_release:
|
||||
docker_send_to_ntfy(docker_latest_release, auth, ntfy_url)
|
||||
notify_all_services(github_latest_release, docker_latest_release, auth, ntfy_url, gotify_url, gotify_token, discord_webhook_url)
|
||||
|
||||
if gotify_url and gotify_token:
|
||||
if github_latest_release:
|
||||
github_send_to_gotify(github_latest_release, gotify_token, gotify_url)
|
||||
if docker_latest_release:
|
||||
docker_send_to_gotify(docker_latest_release, gotify_token, gotify_url)
|
||||
|
||||
if discord_webhook_url:
|
||||
if github_latest_release:
|
||||
github_send_to_discord(github_latest_release, discord_webhook_url)
|
||||
if docker_latest_release:
|
||||
docker_send_to_discord(docker_latest_release, discord_webhook_url)
|
||||
|
||||
time.sleep(timeout) # Wait an hour before checking again
|
||||
time.sleep(timeout)
|
||||
else:
|
||||
logger.error("Usage: python ntfy.py")
|
||||
logger.error(
|
||||
|
||||
@@ -2,20 +2,18 @@ import requests
|
||||
import sqlite3
|
||||
import logging
|
||||
|
||||
conn = sqlite3.connect(
|
||||
"/github-ntfy/ghntfy_versions.db",
|
||||
check_same_thread=False,
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_db_connection():
|
||||
return sqlite3.connect("/github-ntfy/ghntfy_versions.db", check_same_thread=False)
|
||||
|
||||
def github_send_to_discord(releases, webhook_url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
for release in releases:
|
||||
app_name = release["repo"].split("/")[-1] # Getting the application name from the repo
|
||||
version_number = release["tag_name"] # Getting the version number
|
||||
@@ -34,40 +32,31 @@ def github_send_to_discord(releases, webhook_url):
|
||||
logger.info(f"The version of {app_name} has not changed. No notification sent.")
|
||||
continue # Move on to the next application
|
||||
|
||||
# Creating the embed message
|
||||
embed = {
|
||||
"title": f"New version for {app_name}",
|
||||
"url": app_url,
|
||||
"color": "#027d21",
|
||||
"fields": [
|
||||
{
|
||||
"name": "Version",
|
||||
"value": version_number,
|
||||
"inline": True
|
||||
},
|
||||
{
|
||||
"name": "Published on",
|
||||
"value": release_date,
|
||||
"inline": True
|
||||
},
|
||||
{
|
||||
"name": "Changelog",
|
||||
"value": changelog,
|
||||
"inline": False
|
||||
}
|
||||
]
|
||||
|
||||
embeds = {
|
||||
"title": f"New version for {app_name}",
|
||||
"url": app_url,
|
||||
"color": "#027d21",
|
||||
"description": f"New version: {version_number}\nPublished on: {release_date}\nChangelog:\n{changelog}"
|
||||
}
|
||||
|
||||
data = {
|
||||
"embeds": [embed]
|
||||
"content": "New version available",
|
||||
"username": "GitHub Ntfy",
|
||||
"embeds": [embeds]
|
||||
}
|
||||
response = requests.post(webhook_url, json=data)
|
||||
if response.status_code == 204:
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
response = requests.post(webhook_url, json = data, headers = headers)
|
||||
if 200 <= response.status_code < 300:
|
||||
logger.info(f"Message sent to Discord for {app_name}")
|
||||
else:
|
||||
logger.error(f"Failed to send message to Discord. Status code: {response.status_code}")
|
||||
|
||||
def docker_send_to_discord(releases, webhook_url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
for release in releases:
|
||||
app_name = release["repo"].split("/")[-1] # Getting the application name from the repo
|
||||
digest_number = release["digest"]
|
||||
|
||||
@@ -2,20 +2,18 @@ import requests
|
||||
import sqlite3
|
||||
import logging
|
||||
|
||||
conn = sqlite3.connect(
|
||||
"/github-ntfy/ghntfy_versions.db",
|
||||
check_same_thread=False,
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_db_connection():
|
||||
return sqlite3.connect("/github-ntfy/ghntfy_versions.db", check_same_thread=False)
|
||||
|
||||
def github_send_to_gotify(releases, token, url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
url = url + "/message"
|
||||
url = url + "?token=" + token
|
||||
for release in releases:
|
||||
@@ -58,6 +56,8 @@ def github_send_to_gotify(releases, token, url):
|
||||
|
||||
|
||||
def docker_send_to_gotify(releases, token, url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
url = url + "/message"
|
||||
url = url + "?token=" + token
|
||||
for release in releases:
|
||||
|
||||
12
send_ntfy.py
12
send_ntfy.py
@@ -2,20 +2,18 @@ import requests
|
||||
import sqlite3
|
||||
import logging
|
||||
|
||||
conn = sqlite3.connect(
|
||||
"/github-ntfy/ghntfy_versions.db",
|
||||
check_same_thread=False,
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_db_connection():
|
||||
return sqlite3.connect("/github-ntfy/ghntfy_versions.db", check_same_thread=False)
|
||||
|
||||
def github_send_to_ntfy(releases, auth, url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
for release in releases:
|
||||
app_name = release["repo"].split("/")[-1] # Getting the application name from the repo
|
||||
version_number = release["tag_name"] # Getting the version number
|
||||
@@ -58,6 +56,8 @@ def github_send_to_ntfy(releases, auth, url):
|
||||
|
||||
|
||||
def docker_send_to_ntfy(releases, auth, url):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
for release in releases:
|
||||
app_name = release["repo"].split("/")[-1] # Getting the application name from the repo
|
||||
digest_number = release["digest"]
|
||||
|
||||
Reference in New Issue
Block a user