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.
This commit is contained in:
Raphael Maenle 2021-12-10 18:29:03 +01:00
parent 36d5bd42ff
commit be09c04987

43
main.py
View File

@ -1,3 +1,6 @@
import time
import ipdb
import json
import random import random
import qrcode import qrcode
import requests import requests
@ -143,22 +146,42 @@ class Bump:
if add_params != None: if add_params != None:
params.update(add_params) params.update(add_params)
r = requests.post(url, params) messages = requests.post(url, params).json()
if r.json() == {} or r.json() == None: if messages == {} or messages == None or messages == []:
return {} return [{}]
elif type(r.json()) == list: elif type(messages) == list:
messages = [] for message in messages:
for message in r.json(): message['data'] = self.decrypt(message.get('data'))
messages.append(self.decrypt(message.get('data')))
return messages return messages
else: 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: 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__': if __name__ == '__main__':