From be09c04987808cd7b35696ffe2378b4ec5c38c6b Mon Sep 17 00:00:00 2001 From: raphael Date: Fri, 10 Dec 2021 18:29:03 +0100 Subject: [PATCH] adds alert for new messages - messages are checked from the server every second and compared with the local log. Any message within the last 2 minutes which were pushed onto the server are shown as a notification. --- main.py | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index ea72c75..8ea41ee 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,6 @@ +import time +import ipdb +import json import random import qrcode import requests @@ -143,22 +146,42 @@ class Bump: if add_params != None: params.update(add_params) - r = requests.post(url, params) + messages = requests.post(url, params).json() - if r.json() == {} or r.json() == None: - return {} - elif type(r.json()) == list: - messages = [] - for message in r.json(): - messages.append(self.decrypt(message.get('data'))) + if messages == {} or messages == None or messages == []: + return [{}] + elif type(messages) == list: + for message in messages: + message['data'] = self.decrypt(message.get('data')) return messages else: - return self.decrypt(r.json().get('data')) + messages['data'] = self.decrypt(messages.get('data')) + return [messages] - def alert(self): + + def load_log(self): + with open('.bump_log', "r+") as f: + return f.readlines() + + def save_log(self, message): + with open('.bump_log', "a") as f: + f.write(self.to_log_line(message)) + + def to_log_line(self, message): + return json.dumps(message) + "\n" + + def alert(self, sleep_time=1): + log = self.load_log() while True: - time.sleep(1) + time.sleep(sleep_time) + messages = self.list() + for message in messages: + if self.to_log_line(message) not in log and message != {}: + print(message['data']) + self.save_log(message) + log = self.load_log() + if __name__ == '__main__':