-
Notifications
You must be signed in to change notification settings - Fork 21
/
countdown.py
executable file
·169 lines (137 loc) · 4.98 KB
/
countdown.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
from flask.ext.script import Manager
from flask import Flask
from datetime import datetime
import json
import os
import requests
import workdays
app = Flask(__name__)
manager = Manager(app)
"""Creates web app to be deployed on Heroku."""
SLACK_URL = os.environ.get('SLACK_URL')
if not SLACK_URL:
print("Missing environment variable SLACK_URL")
exit(1)
def days_from_christmas():
"""Calculates the number of days between the current date and the next
Christmas. Returns the string to displayed.
"""
currentdate = datetime.now()
christmas = datetime(datetime.today().year, 12, 25)
if christmas < currentdate:
christmas = date(datetime.today().year + 1, 12, 25)
delta = christmas - currentdate
days = delta.days
if days == 1:
return "%d day from the nearest Christmas" % days
else:
return "%d days from the nearest Christmas" % days
def days_from_date(strdate, business_days):
""" Returns the number of days between strdate and today. Add one to date
as date caclulate is relative to time
"""
currentdate = datetime.today()
futuredate = datetime.strptime(strdate, '%Y-%m-%d')
if business_days:
delta = workdays.networkdays(currentdate, futuredate)
else:
delta = (futuredate - currentdate).days + 1
return delta
def events(strdate, event, business_days):
""" Returns string to be displayed with the event mentioned. Sends an error
if date is incorrect
"""
days = days_from_date(strdate, business_days)
day_qualifier = ""
if business_days:
day_qualifier = "business "
assert (days >= -2), "Date needs to be in the future"
if days == -1:
return "%d %sday since %s" % (1, day_qualifier, event)
elif days == -2:
return "%d %sdays since %s" % (2, day_qualifier, event)
elif days == 1:
return "%d %sday until %s" % (days, day_qualifier, event)
else:
return "%d %sdays until %s" % (days, day_qualifier, event)
def date_only(strdate, business_days):
""" Returns string to be displayed. Sends error message if date is
in the past
"""
days = days_from_date(strdate)
day_qualifier = ""
if business_days:
day_qualifier = "business "
assert (days >= -2), "Date needs to be in the future"
futuredate = datetime.strptime(strdate, '%Y-%m-%d')
if days == -1:
return "%d %sday since %s" % (1, day_qualifier, futuredate.strftime("%d %B, %Y"))
if days == -2:
return "%d %sdays since %s" % (days, day_qualifier, futuredate.strftime("%d %B, %Y"))
if days == 1:
return "%d %sday until %s" % (days, day_qualifier, futuredate.strftime("%d %B, %Y"))
else:
return "%d %sdays until %s" % (days, day_qualifier, futuredate.strftime("%d %B, %Y"))
def post(out):
""" Posts a request to the slack webhook. Payload can be customized
so the message in slack is customized. The variable out is the text
to be displayed.
"""
payload = {
"attachments": [
{
"title": "COUNTDOWN!",
"text": out,
"color": "#7CD197"
}
]
}
r = requests.post(SLACK_URL, data=json.dumps(payload))
def post_error():
"""Sends error message in Slack to alert the user
about the incorrect date argument
"""
payload = {
"attachments": [
{
"title": "Error",
"text": ("Date entered must be in the future. "
"\n Go to the <https://heroku.com|Heroku Scheduler> for you app and edit"
" the command"),
"color": "#525162"
}
]
}
r = requests.post(SLACK_URL, data=json.dumps(payload))
@manager.option("-d", "--deadline", dest="date",
help="Specify the deadline in ISO format: yyyy-mm-dd",
metavar="DEADLINE")
@manager.option("-e", "--event", dest="event",
help="Name of the deadline event",metavar="EVENT")
@manager.option("-b", "--business-days", dest="business_days", action="store_true",
help="Give the count of business days only")
def deadline(date, event, business_days):
""" Method takes two optional arguments. Displays in slack channel
the number of days till the event. If no arguments are given,
the number of days till Christmas is displayed.
"""
try:
result = ""
if date:
if event:
result = events(date, event, business_days)
else:
result = date_only(date, business_days)
else:
result = days_from_christmas()
except:
post_error()
else:
post(result)
@manager.command
def initiate():
payload = { "text": "App is now connected to your Slack Channel."}
r = requests.post(SLACK_URL, data=json.dumps(payload))
if __name__ == "__main__":
manager.run()