-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushnotifications.py
73 lines (64 loc) · 2.35 KB
/
pushnotifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Sends notification using pushover or prowl"""
#import shutil
#import sys
import os
from collections import namedtuple
import requests
def read_pushover_settings(filename):
"""Attempt to read push over settings"""
PushOverSettings = namedtuple('PushOverSettings', ['apptoken', 'userkey'])
try:
currentdir = os.getcwd()
apptoken = ""
userkey = ""
with open(currentdir + "/" + filename, 'r') as file:
apptoken = file.readline().rstrip('\n')
userkey = file.readline().rstrip('\n')
settings = PushOverSettings(apptoken, userkey)
return settings
except IOError:
pass
def read_prowl_settings(filename):
"""Attempt to read prowl settings"""
ProwlSettings = namedtuple('ProwlSettings', ['apikey'])
try:
currentdir = os.getcwd()
apikey = ""
with open(currentdir + "/" + filename, 'r') as file:
apikey = file.readline().rstrip('\n')
settings = ProwlSettings(apikey)
return settings
except IOError:
pass
def send_notification_with_pushover(app_token, user_key, event, message):
"""Tell the phone"""
files = {
'token': (None, app_token),
'user': (None, user_key),
'title': (None, event),
'message': (None, message),
}
requests.post('https://api.pushover.net/1/messages.json', files=files)
def send_notification_with_prowl(apikey, event, message):
"""Tell the phone"""
files = {
'apikey': (None, apikey),
'application': (None, event),
'event': (None, ""),
'description': (None, message),
}
requests.post('https://api.prowlapp.com/publicapi/add', files=files)
def push_notification(event, message):
notification_system = "none"
notify_settings = read_pushover_settings("pushover.txt")
if hasattr(notify_settings, "apptoken"):
notification_system = "pushover"
elif hasattr(notify_settings, "apikey"):
notification_system = "prowl"
notify_settings = read_prowl_settings("prowl.txt")
if notification_system == "pushover":
send_notification_with_pushover(notify_settings.apptoken, notify_settings.userkey, event, message)
elif notification_system == "prowl":
send_notification_with_prowl(notify_settings.apikey, event, message)
else:
print("No notifications configured")