-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslackjson.py
76 lines (60 loc) · 1.84 KB
/
slackjson.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
#!/usr/bin/python2.7
#
# Send json data to Slack
# by Joel Sutton <innerteapot@gmail.com>
#
"""
This script takes the json output file of updatejson.py, sends some stats data
to Slack via a web hook, and then updates a flag in the json file.
"""
__version__ = "1.0"
import argparse
import json
import os.path
import pprint
import requests
def main(args):
# mute warnings to make cron triggering smoother on Ubuntu 14.04 LTS
requests.packages.urllib3.disable_warnings()
if os.path.isfile(args.file):
with open(args.file) as json_file:
data = json.load(json_file)
else:
if args.debug:
print "Unable to find file!"
exit(1)
if data["slack"] != "NO":
if args.debug:
print "Already sent!"
exit(0)
if args.debug:
print "===> Sending data to slack"
u = "https://hooks.slack.com/services/"
h = {
'content-type': 'application/json',
}
payload = {}
payload["text"] = "Current Track: %s\nListener Count: %s" % (
data["current_track"],
data["total_listeners"])
r = requests.post(u, headers=h, json=payload, timeout=5)
if args.debug:
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(r.text)
if r.text != "ok":
exit(1)
data["slack"] = "YES"
with open(args.file, 'w') as outfile:
json.dump(data, outfile)
if args.debug:
print "===> Payload data"
pp.pprint(payload)
print "===> json data"
pp.pprint(data)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Send data to Slack and update json file')
parser.add_argument("file", help="json file")
parser.add_argument("--debug", help="enable debugging output", \
action="store_true")
args = parser.parse_args()
main(args)