-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibnotif.py
More file actions
99 lines (84 loc) · 2.62 KB
/
libnotif.py
File metadata and controls
99 lines (84 loc) · 2.62 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# File: libnotif.py
#
# Project: Websocket webkit notification pusher
# Component: notification api code
#
# Authors: Dominic May;
# Lord_DeathMatch;
# Mause
#
# Description: A ridiculously simple method of controlling the notifications system programmatically
import time
from socket import *
try:
import json
except ImportError:
import simplejson as json
def if_connected(handler):
print dir(handler)
# if not handler.connected:
# raise socket.error('Not connected')
class NotifConn:
def __init__(self, host='localhost', port=50012):
self.buf = 1024
self.host = host
self.port = port
self.connected = False
def connect(self):
self.addr = (self.host, self.port)
self.clientsocket = socket(AF_INET, SOCK_STREAM)
self.clientsocket.connect(self.addr)
self.connected = True
# @if_connected
def request_clients(self):
self.clientsocket.send('/clients')
data = json.loads(self.clientsocket.recv(self.buf))
return data
# @if_connected
def request_notifs(self):
self.clientsocket.send('/notifs')
data = json.loads(self.clientsocket.recv(self.buf))
return data
# @if_connected
def send_notif(self, title, client='a', content='', icon=''):
# '%s:%s~%s~%s' % (client, icon, title, content)
self.clientsocket.send(json.dumps({
'client': client,
'icon': icon,
'title': title,
'content': content
})
)
status = self.clientsocket.recv(self.buf)
if status.startswith('error_'):
return status
# @if_connected
def disconnect(self):
self.connected = False
self.clientsocket.close()
def quick_send(title, client='a', content='', icon=''):
conn = NotifConn('localhost', 50012)
if len(conn.request_clients()) != 0:
conn.send_notif(title, client, content, icon)
conn.disconnect()
if __name__ == '__main__':
conn = NotifConn('localhost', 50012)
conn.connect()
print 'Waiting for clients...'
while len(conn.request_clients()) == 0:
time.sleep(1)
print 'Clients;',
print conn.request_clients()
message = True
try:
while message != '':
message = raw_input('Message? ')
conn.send_notif(str(message))
except:
if conn.connected:
conn.disconnect()
finally:
if conn.connected:
conn.disconnect()
conn.disconnect()