From f5581175ae9caf9a8d7d6daa413d93400071175b Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Fri, 19 May 2023 12:40:46 -0700 Subject: [PATCH] Add Discord webhook support --- adsms/__init__.py | 19 +++++++++++++------ adsms/db.py | 17 ++++++++++++++--- debian/adsms-docs.docs | 2 -- debian/adsms.doc-base.ex | 20 +++++--------------- meson.build | 1 + pyproject.toml | 2 +- requirements.txt | 1 + 7 files changed, 35 insertions(+), 27 deletions(-) delete mode 100644 debian/adsms-docs.docs diff --git a/adsms/__init__.py b/adsms/__init__.py index 511285b..f05f02f 100755 --- a/adsms/__init__.py +++ b/adsms/__init__.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 - # Partially written with ChatGPT import os @@ -8,6 +6,7 @@ import json import argparse import requests import adsms.db +import discord_webhook def load_config(config_file): @@ -22,14 +21,21 @@ def send_text_message(phone, message, key): def process_subscriptions(con, config, data): - for sub_id, phone, icao, description, last_seen in db.get_subscriptions( - con - ): + subscriptions = db.get_subscriptions(con) + print(subscriptions) + for sub_id, phone, icao, description, last_seen, platform in subscriptions: if icao in data and data[icao]["seen"] < config["max_age"]: if last_seen + config["min_disappearance"] < time.time(): message = f"{description}\n{config['tracker']}?icao={icao}" - send_text_message(phone, message, config["textbelt_key"]) + if platform == "textbelt": + send_text_message(phone, message, config["textbelt_key"]) + elif platform == "discord_webhook": + print( + discord_webhook.DiscordWebhook( + url=phone, content=message + ).execute() + ) print(f"{phone}: {message}") @@ -39,6 +45,7 @@ def process_subscriptions(con, config, data): def get_current_data(config): + # return {"78007e": {"seen": 0}} response = requests.get(config["data"]) planes = response.json()["aircraft"] return {plane["hex"]: plane for plane in planes} diff --git a/adsms/db.py b/adsms/db.py index e356ef5..1467712 100644 --- a/adsms/db.py +++ b/adsms/db.py @@ -1,4 +1,6 @@ import collections +import sqlite3 +import time Subscription = collections.namedtuple( "Subscription", @@ -8,6 +10,7 @@ Subscription = collections.namedtuple( "icao", "description", "last_seen", + "platform", ], ) @@ -15,9 +18,17 @@ Subscription = collections.namedtuple( def load_database(file_name): con = sqlite3.connect(file_name) - con.execute( + cur = con.execute( "CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)" ) + + try: + cur.execute( + "ALTER TABLE subscriptions ADD COLUMN platform DEFAULT 'textbelt'" + ) + except sqlite3.OperationalError: + pass + con.commit() return con @@ -32,6 +43,6 @@ def update_last_seen_time(con, sub_id): def get_subscriptions(con): for subscription in con.execute( - "SELECT rowid, phone, icao, description, last_seen FROM subscriptions" + "SELECT rowid, phone, icao, description, last_seen, platform FROM subscriptions" ).fetchall(): - return Subscription(*subscription) + yield Subscription(*subscription) diff --git a/debian/adsms-docs.docs b/debian/adsms-docs.docs deleted file mode 100644 index 7319041..0000000 --- a/debian/adsms-docs.docs +++ /dev/null @@ -1,2 +0,0 @@ -README.source -README.Debian diff --git a/debian/adsms.doc-base.ex b/debian/adsms.doc-base.ex index 30d1bea..7517250 100644 --- a/debian/adsms.doc-base.ex +++ b/debian/adsms.doc-base.ex @@ -1,20 +1,10 @@ Document: adsms Title: Debian adsms Manual -Author: +Author: Samuel Sloniker Abstract: This manual describes what adsms is - and how it can be used to - manage online manuals on Debian systems. + and how it can be used to send SMS notifications + based on ADS-B data. Section: unknown -Format: debiandoc-sgml -Files: /usr/share/doc/adsms/adsms.sgml.gz - -Format: postscript -Files: /usr/share/doc/adsms/adsms.ps.gz - -Format: text -Files: /usr/share/doc/adsms/adsms.text.gz - -Format: HTML -Index: /usr/share/doc/adsms/html/index.html -Files: /usr/share/doc/adsms/html/*.html +Format: Text +Files: /usr/share/doc/adsms/README.md diff --git a/meson.build b/meson.build index 48dc71e..e608576 100644 --- a/meson.build +++ b/meson.build @@ -10,3 +10,4 @@ py.install_sources( install_data('run_adsms.py', install_dir: get_option('bindir'), rename: 'adsms') install_data('adsms.service', install_dir: 'lib/systemd/system') install_data('example_config.json', install_dir: 'etc', rename: 'adsms.json') +install_data('README.md', install_dir: 'usr/share/doc/adsms/') diff --git a/pyproject.toml b/pyproject.toml index ae40e4f..2ba79ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ classifiers = [ "Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: GNU Affero General Public License v3", ] -dependencies = ["requests"] +dependencies = ["requests", "discord-webhook"] requires-python = ">=3.7" [project.urls] diff --git a/requirements.txt b/requirements.txt index f229360..0046a9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests +discord-webhook