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 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__':