-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdweet.py
80 lines (65 loc) · 2.52 KB
/
dweet.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
74
75
76
77
78
79
80
__author__ = "madawood"
import logging
import httplib
import signal
import json
logger = logging.getLogger("mqtt_app")
def _sleep_handler(signum, frame):
print "SIGINT Received. Stopping app"
raise KeyboardInterrupt
def _stop_handler(signum, frame):
print "SIGTERM Received. Stopping app"
raise KeyboardInterrupt
class Dweet(object):
__singleton = None # the one, true Singleton
__singleton_init_done = False
def __new__(cls, *args, **kwargs):
# Check to see if a __singleton exists already for this class
# Compare class types instead of just looking for None so
# that subclasses will create their own __singleton objects
if cls != type(cls.__singleton):
# if not cls.__singleton:
cls.__singleton = super(Dweet, cls).__new__(cls, *args, **kwargs)
return cls.__singleton
def __init__(self, enabled=True, server="dweet.io", name=None, timeout=5):
self.enabled = enabled
self.server = server
self.name = name
self.timeout = timeout
logger.debug("Initialized the Dweeting !!")
def dweet(self, content):
if self.enabled:
try:
logger.debug("Connecting to https://%s", self.server)
conn = httplib.HTTPSConnection(self.server, timeout=self.timeout)
# params = urllib.urlencode(content)
url = "/dweet/for/%s" % (self.name)
logger.debug("Dweeting: %s", url)
conn.request("POST", url, body=json.dumps(content), headers={'Content-Type': 'application/json'})
response = conn.getresponse()
logger.debug("Response Status: %s, Response Reason: %s", response.status, response.reason)
except Exception as ex:
logger.error("Exception while dweeting: cause: %s" % ex.message)
@classmethod
def get_instance(cls):
'''
Returns a singleton instance of the class
'''
if not cls.__singleton:
return None
return cls.__singleton
if __name__ == "__main__":
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
logger.setLevel(10)
console = logging.StreamHandler()
console.setLevel(10)
console.setFormatter(formatter)
logger.addHandler(console)
signal.signal(signal.SIGTERM, _stop_handler)
signal.signal(signal.SIGINT, _sleep_handler)
content = {
"key": "value"
}
m = Dweet(name="awake-transport")
for i in range(5):
m.dweet(content)