Compare commits
2 Commits
5748696970
...
f5581175ae
Author | SHA1 | Date | |
---|---|---|---|
f5581175ae | |||
422f987888 |
|
@ -1,13 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# Partially written with ChatGPT
|
||||
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import sqlite3
|
||||
import argparse
|
||||
import requests
|
||||
import adsms.db
|
||||
import discord_webhook
|
||||
|
||||
|
||||
def load_config(config_file):
|
||||
|
@ -15,24 +14,6 @@ def load_config(config_file):
|
|||
return json.load(f)
|
||||
|
||||
|
||||
def load_database(config):
|
||||
con = sqlite3.connect(config["database"])
|
||||
|
||||
con.execute(
|
||||
"CREATE TABLE IF NOT EXISTS subscriptions(phone VARCHAR, icao VARCHAR, description VARCHAR, last_seen INTEGER)"
|
||||
)
|
||||
con.commit()
|
||||
|
||||
return con
|
||||
|
||||
|
||||
def update_last_seen_time(con, sub_id):
|
||||
con.execute(
|
||||
"UPDATE subscriptions SET last_seen = ? WHERE rowid = ?",
|
||||
(time.time(), sub_id),
|
||||
)
|
||||
|
||||
|
||||
def send_text_message(phone, message, key):
|
||||
request = {"phone": phone, "message": message, "key": key}
|
||||
resp = requests.post("https://textbelt.com/text", request)
|
||||
|
@ -40,32 +21,38 @@ def send_text_message(phone, message, key):
|
|||
|
||||
|
||||
def process_subscriptions(con, config, data):
|
||||
cur = con.execute(
|
||||
"SELECT rowid, phone, icao, description, last_seen FROM subscriptions"
|
||||
)
|
||||
|
||||
for sub_id, phone, icao, description, last_seen in cur.fetchall():
|
||||
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}")
|
||||
|
||||
update_last_seen_time(con, sub_id)
|
||||
db.update_last_seen_time(con, sub_id)
|
||||
|
||||
con.commit()
|
||||
|
||||
|
||||
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}
|
||||
|
||||
|
||||
def run(config):
|
||||
con = load_database(config)
|
||||
con = adsms.db.load_database(config["database"])
|
||||
|
||||
while True:
|
||||
data = get_current_data(config)
|
||||
|
|
48
adsms/db.py
Normal file
48
adsms/db.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import collections
|
||||
import sqlite3
|
||||
import time
|
||||
|
||||
Subscription = collections.namedtuple(
|
||||
"Subscription",
|
||||
[
|
||||
"id",
|
||||
"phone",
|
||||
"icao",
|
||||
"description",
|
||||
"last_seen",
|
||||
"platform",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def load_database(file_name):
|
||||
con = sqlite3.connect(file_name)
|
||||
|
||||
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
|
||||
|
||||
|
||||
def update_last_seen_time(con, sub_id):
|
||||
con.execute(
|
||||
"UPDATE subscriptions SET last_seen = ? WHERE rowid = ?",
|
||||
(time.time(), sub_id),
|
||||
)
|
||||
|
||||
|
||||
def get_subscriptions(con):
|
||||
for subscription in con.execute(
|
||||
"SELECT rowid, phone, icao, description, last_seen, platform FROM subscriptions"
|
||||
).fetchall():
|
||||
yield Subscription(*subscription)
|
2
debian/adsms-docs.docs
vendored
2
debian/adsms-docs.docs
vendored
|
@ -1,2 +0,0 @@
|
|||
README.source
|
||||
README.Debian
|
20
debian/adsms.doc-base.ex
vendored
20
debian/adsms.doc-base.ex
vendored
|
@ -1,20 +1,10 @@
|
|||
Document: adsms
|
||||
Title: Debian adsms Manual
|
||||
Author: <insert document author here>
|
||||
Author: Samuel Sloniker <sam@kj7rrv.com>
|
||||
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
|
||||
|
|
|
@ -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/')
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
requests
|
||||
discord-webhook
|
||||
|
|
Loading…
Reference in New Issue
Block a user