-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from tblanarik/cleanup
Major cleanup and refactoring
- Loading branch information
Showing
6 changed files
with
168 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import requests | ||
import os | ||
|
||
class DiscordHttp: | ||
def __init__(self): | ||
self.session = requests.Session() | ||
|
||
def post_message(self, content, messageId=None): | ||
# flags = 4 means it will suppress embeds: https://discord.com/developers/docs/resources/message#message-object-message-flags | ||
content_payload = {"content": content, "flags": 4} | ||
target_url = os.getenv('TARGET_URL') | ||
verb = "POST" | ||
if messageId is not None: | ||
target_url = target_url + f"/messages/{messageId}" | ||
verb = "PATCH" | ||
response = self.session.request(verb, url=target_url, params={"wait": "true"}, json=content_payload) | ||
return response.json()['id'] | ||
|
||
def get_message_from_id(self, messageId): | ||
target_url = os.getenv('TARGET_URL') | ||
verb = "GET" | ||
target_url = target_url + f"/messages/{messageId}" | ||
response = self.session.request(verb, url=target_url) | ||
return response.json()['content'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import datetime | ||
from pytz import timezone | ||
|
||
class HamAlertMessage: | ||
def __init__(self, req_body): | ||
self.callsign = req_body.get('callsign', 'Unknown') | ||
self.source = req_body.get('source', 'Unknown') | ||
self.frequency = req_body.get('frequency', 'Unknown') | ||
self.mode = req_body.get('mode', 'Unknown') | ||
self.summitRef = req_body.get('summitRef', '') | ||
self.wwffRef = req_body.get('wwffRef', '') | ||
self.received_time_pt = datetime.datetime.now(timezone('US/Pacific')) | ||
|
||
def spot_deeplink(self): | ||
match self.source: | ||
case "sotawatch": | ||
return f"[{self.source}](https://sotl.as/activators/{self.callsign})" | ||
case "pota": | ||
return f"[{self.source}](https://api.pota.app/spot/comments/{self.callsign}/{self.wwffRef})" | ||
case _: | ||
return "" | ||
|
||
def __str__(self): | ||
return f'{self.received_time_pt.strftime("%H:%M")} | {self.callsign} | {self.spot_deeplink()} | freq: {self.frequency} | mode: {self.mode} | loc: {self.summitRef}{self.wwffRef}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,44 @@ | ||
import logging | ||
import os | ||
import datetime | ||
from pytz import timezone | ||
import requests | ||
import tables | ||
|
||
|
||
def run(req): | ||
logging.info('Python HTTP trigger function processed a request.') | ||
dd = datetime.datetime.now(timezone('US/Pacific')) | ||
|
||
req_body = req.get_json() | ||
logging.info(f"Received JSON: {req_body}") | ||
|
||
callsign = req_body.get('callsign') | ||
|
||
content = create_content(req_body, dd) | ||
|
||
table = tables.get_table() | ||
entity = tables.query_for_entity(table, callsign) | ||
messageId = None | ||
existingMessage = None | ||
|
||
if is_entity_recent(entity): | ||
messageId = entity['MessageId'] | ||
existingMessage = get_previous_message(messageId).replace("~", "") | ||
content = "~~" + existingMessage + "~~\n" + content | ||
|
||
# flags = 4 means it will suppress embeds: https://discord.com/developers/docs/resources/message#message-object-message-flags | ||
content_payload = {"content":content, "flags": 4} | ||
|
||
messageId = post_message(content_payload, messageId) | ||
tables.upsert_entity(table, callsign, messageId) | ||
|
||
def create_content(req_body, dd): | ||
callsign = req_body.get('callsign', 'Unknown') | ||
source = req_body.get('source', 'Unknown') | ||
frequency = req_body.get('frequency', 'Unknown') | ||
mode = req_body.get('mode', 'Unknown') | ||
summitRef = req_body.get('summitRef', '') | ||
wwffRef = req_body.get('wwffRef', '') | ||
|
||
spot_deeplink = create_spot_deeplink(source, callsign, wwffRef) | ||
formatted_time = dd.strftime("%H:%M") | ||
|
||
content = f"{formatted_time} | {callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef}" | ||
return content | ||
|
||
def create_spot_deeplink(source, callsign, wwffRef): | ||
match source: | ||
case "sotawatch": | ||
return f"[{source}](https://sotl.as/activators/{callsign})" | ||
case "pota": | ||
return f"[{source}](https://api.pota.app/spot/comments/{callsign}/{wwffRef})" | ||
case _: | ||
return "" | ||
|
||
def is_entity_recent(entity): | ||
if entity is None: | ||
return False | ||
ent_time = entity.metadata['timestamp'] | ||
cur_time = datetime.datetime.now(datetime.timezone.utc) | ||
lookback_seconds = int(os.getenv('LOOKBACK_SECONDS', 7200)) | ||
return (cur_time - ent_time).total_seconds() < lookback_seconds | ||
|
||
def post_message(content, messageId=None): | ||
target_url = os.getenv('TARGET_URL') | ||
verb = "POST" | ||
if messageId is not None: | ||
target_url = target_url + f"/messages/{messageId}" | ||
verb = "PATCH" | ||
response = requests.request(verb, url=target_url, params={"wait": "true"}, json=content) | ||
return extract_message_id(response) | ||
|
||
def get_previous_message(messageId): | ||
target_url = os.getenv('TARGET_URL') | ||
verb = "GET" | ||
target_url = target_url + f"/messages/{messageId}" | ||
response = requests.request(verb, url=target_url) | ||
return response.json()['content'] | ||
|
||
def extract_message_id(response): | ||
return response.json()['id'] | ||
from hamalertmessage import HamAlertMessage | ||
|
||
class SpotBot: | ||
def __init__(self, http_req, table, discord_http): | ||
self.http_req = http_req | ||
self.ham = HamAlertMessage(http_req.get_json()) | ||
self.table = table | ||
self.discord_http = discord_http | ||
|
||
def process(self): | ||
logging.info('Processing HamAlert message') | ||
previous_message, message_id = self.get_last_message() | ||
if previous_message: | ||
previous_message = self.strikethrough_mesage(previous_message) | ||
content = self.combine_messages(previous_message, self.ham) | ||
else: | ||
content = str(self.ham) | ||
message_id = self.discord_http.post_message(content, message_id) | ||
self.table.upsert_entity(self.ham.callsign, message_id) | ||
|
||
def strikethrough_mesage(self, message): | ||
return f"~~{message}~~" | ||
|
||
def combine_messages(self, m1, m2): | ||
return f"{m1}\n{m2}" | ||
|
||
def get_last_message(self): | ||
last_message_entity = self.table.query_for_entity(self.ham.callsign) | ||
if self.is_entity_recent(last_message_entity): | ||
messageId = last_message_entity['MessageId'] | ||
existing_message = self.discord_http.get_message_from_id(messageId) | ||
return existing_message.replace("~", ""), messageId | ||
return "", None | ||
|
||
def is_entity_recent(self, entity): | ||
if entity is None: | ||
return False | ||
ent_time = entity.metadata['timestamp'] | ||
cur_time = datetime.datetime.now(datetime.timezone.utc) | ||
lookback_seconds = int(os.getenv('LOOKBACK_SECONDS', 7200)) | ||
return (cur_time - ent_time).total_seconds() < lookback_seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters