-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheventcalc.py
executable file
·213 lines (200 loc) · 8.23 KB
/
eventcalc.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
#
# Event time-remaining calculator
#
# Part of SIFTools <https://github.com/dburr/SIFTools/>
# By Donald Burr <[email protected]>
# Copyright (c) 2015 Donald Burr.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# curses docs: https://docs.python.org/2/library/curses.html
import time
import datetime
import os
import sys
import getopt
import curses
scr = None
width = None
height = None
def center(string):
global width
global height
string_length = len(string)
horizontal_space = (width // 2) - (string_length // 2)
print "%s%s" % ((" " * horizontal_space), string)
# NOTE: OS DEPENDENT SECTION - this basically returns a tuple containing the screen's size
# (width,height). This code will work on Mac OS X systems and probably Linux as well, but
# will most likely not work properly on Windows machines. If nothing else, you can replace
# this code with a simple "return" statement that returns a hard-coded value (e.g. "return (80,24")
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
return int(cr[1]), int(cr[0])
def calc(year, month, day, hour, minute, fullscreen = False):
global height
today = datetime.datetime.utcnow()
today_hour = today.hour
today_minute = today.minute
today_seconds = today.second
delta = datetime.datetime(year, month, day, hour, minute, 0) - today
s = delta.total_seconds()
days, days_remainder = divmod(s, 86400)
days_hours, days_hours_remainder = divmod(days_remainder, 3600)
days_minutes, days_seconds = divmod(days_hours_remainder, 60)
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
if fullscreen:
absolutely_unused_variable = os.system("clear")
number_of_blank_lines = (height // 2) - (8 // 2) + 1
for x in range(1, number_of_blank_lines):
print ""
center("CURRENT TIME")
center(today.strftime("%Y-%m-%d %H:%M:%S UTC"))
print("")
center("EVENT ENDS")
center("%04d-%02d-%02d %02d:%02d:%02d UTC" % (year, month, day, hour, minute, 0))
print("")
center("TIME REMAINING")
if s <= 0:
center("*** EVENT HAS ENDED ***")
else:
center("%d:%02d:%02d (%dd%dh%dm)" % (hours, minutes, seconds, days, days_hours, days_minutes))
skip_lines = number_of_blank_lines
if today_minute == 35:
skip_lines = skip_lines - 3
print ""
center("Check @sifen_trackbot for a tier update:")
center("https://twitter.com/sifen_trackbot")
if hours > 0 and hours < 6:
skip_lines = skip_lines - 2
print ""
center("*** THE RUSH IS ON!!! ***")
if today_hour == 0 and today_minute == 0:
skip_lines = skip_lines - 2
print ""
if today_seconds % 2 == 0:
print ""
else:
# NOTE: OS DEPENDENT SECTION - this commands plays an alert sound on Mac OS X machines.
# You will need to change it to a different command that plays a sound on Linux or Windows machines.
# (it can also be removed safely, but you won't hear an alert sound when it's time to claim your daily login bonus)
absolutely_unused_variable = os.system("afplay /System/Library/Sounds/Glass.aiff 2>/dev/null &")
center("*** Be sure and claim your daily Login Gift ***")
for x in range(1, skip_lines):
print ""
else:
print " Current time: %s" % today.strftime("%Y-%m-%d %H:%M:%S UTC")
print " Event ends: %04d-%02d-%02d %02d:%02d:%02d UTC" % (year, month, day, hour, minute, 0)
if s <= 0:
print "Time remaining: *** EVENT HAS ENDED ***"
else:
print "Time remaining: %d:%02d:%02d (%dd%dh%dm)" % (hours, minutes, seconds, days, days_hours, days_minutes)
if hours < 6:
print "*** THE RUSH IS ON!!! ***"
def usage():
print "Usage: %s [options]" % os.path.basename(__file__)
print "where [options] can be one or more of:"
print " [-H | --help] Print this help message"
print " [-y | --year] Event year (optional, defaults to current year)"
print " [-M | --month] Event month (optional, defaults to current month)"
print " [-d | --day] Event day (REQUIRED)"
print " [-h | --hour] Event hour (REQUIRED)"
print " [-m | --minute] Event minute (optional, defaults to 0)"
print " [-c | --continuous] Continuously updating display (defaults to off)"
print "Note: event time should be specified in UTC."
def main(argv):
global scr
global width
global height
year = datetime.datetime.utcnow().year
month = datetime.datetime.utcnow().month
day = None
hour = None
minute = 0
continuous_mode = False
#scr = curses.initscr()
(width, height) = getTerminalSize()
#(width, height) = getmaxyx()
try:
options, remainder = getopt.getopt(argv, "Hy:M:d:h:m:c", ["help", "year=", "month=", "day=", "hour=", "minute=", "continuous"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in options:
if opt in ('-H', '--help'):
usage()
sys.exit(0)
elif opt in ('-y', '--year'):
year = int(arg)
elif opt in ('-M', '--month'):
month = int(arg)
elif opt in ('-d', '--day'):
day = int(arg)
elif opt in ('-h', '--hour'):
hour = int(arg)
elif opt in ('-m', '--minute'):
minute = int(arg)
elif opt in ('-c', '--continuous'):
continuous_mode = True
if day is None:
print "Error: must specify day"
usage()
sys.exit(1)
elif hour is None:
print "Error: must specify hour"
usage()
sys.exit(1)
else:
if continuous_mode:
while True:
# NOTE: OS DEPENDENT SECTION - this commands clears the screen. This command should work on Mac and Linux systems.
# For windows, you will need to change it to the following: (note, I have not tested this)
# absolutely_unused_variable = os.system("cls")
absolutely_unused_variable = os.system("clear")
calc(year, month, day, hour, minute, True)
time.sleep(1)
else:
calc(year, month, day, hour, minute)
### main script starts here
if __name__ == "__main__":
main(sys.argv[1:])