wardyns-feditools/notification_sound.py

30 lines
1.0 KiB
Python
Raw Normal View History

2023-07-30 19:18:11 -07:00
#!/usr/bin/env python
2023-01-26 19:40:26 -08:00
import websocket
import os
from playsound import playsound
from generate_config import generate_config
import json
parent = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(os.path.join(parent, 'config.json')):
generate_config("Wardyn's feditools", "read write follow push")
with open(os.path.join(parent, 'config.json'), 'r') as config_file:
config = json.load(config_file)
sound = os.path.join(parent, 'notification.mp3')
def on_message(ws, message):
print('Received notification')
playsound(sound)
def on_open(ws):
print('Websocket opened')
def on_close(ws, *error):
2023-01-26 19:40:26 -08:00
print('Websocket closed, attempting to reconnect')
def connect_websocket():
ws = websocket.WebSocketApp("wss://" + config['instance'].split('/')[-1] + "/api/v1/streaming?access_token=" + config['user_token'] + "&stream=user:notification",
on_message = on_message, on_open = on_open, on_close = on_close, on_error=on_close)
ws.run_forever()
while True:
connect_websocket()