-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackAnnounce.py
69 lines (57 loc) · 1.83 KB
/
slackAnnounce.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
import requests
import json
import sheetspart
def postToSlack (text):
url = getWebhook()
headers = {"Content-type":"application/json"}
body = {"text":text}
r = requests.post(url = url, headers = headers,data=json.dumps(body))
print(r)
print(r.content)
def getWebhook():
try:
conf = json.load(open("slackConfig.json"))
return conf["secretWebhook"]
except Exception as e:
return ""
def prepareMessage (sheetID,slackRange):
values = sheetspart.getLatestRow(sheetID,slackRange)
names = values[0]
growth = values[1]
totals = values[2]
teams = []
for i in range(len(names)):
team = {"name":names[i],
"growth":int(growth[i]),
"total":int(totals[i])
}
teams.append(team)
teams = sortTeams(teams)
print (teams)
message = """ *Yesterday's contributions* (as of 23:30 UTC+1)
"""
for team in teams:
message = "%s+%s (%s)\t`%s`\n" % (message,format(team["growth"],","),format(team["total"],","),team["name"])
return message
def sortTeams(teams):
changed = True
while changed:
changed = False
for i in range (len(teams)):
try:
oldTeam = teams[i]
newTeam = teams[i+1]
if oldTeam["total"] < newTeam["total"]:
#if the total score is lower, swap.
oldTeam = teams[i]
newTeam = teams[i+1]
teams[i] = newTeam
teams[i+1] = oldTeam
changed = True
except Exception as e:
pass
return teams
if __name__ == "__main__":
config = json.load(open("sheetsConfig.json","r"))
message = prepareMessage(config["sheetID"],config["slackRange"])
postToSlack(message)