Skip to content

Instantly share code, notes, and snippets.

@sugiana
Created April 22, 2013 03:37
Show Gist options
  • Save sugiana/5432295 to your computer and use it in GitHub Desktop.
Save sugiana/5432295 to your computer and use it in GitHub Desktop.
# Countdown and do something. Before run install espeak (optional):
#
# apt-get install espeak
#
import os
import sys
from datetime import time, datetime, timedelta
from time import sleep
from optparse import OptionParser
def say_number(n, s=''):
if not s:
return str(n)
if n > 1:
s += 's'
return '%d %s' % (n, s)
def nice_say(td): # timedelta
seconds = td.seconds
hour = seconds / 3600
says = []
if hour > 0:
says.append(say_number(hour, 'hour'))
seconds -= hour * 3600
minute = seconds / 60
if minute > 0:
says.append(say_number(minute, 'minute'))
seconds -= minute * 60
if len(says) < 2 and not hour:
if not minute and seconds < 11:
says.append(say_number(seconds))
else:
says.append(say_number(seconds, 'second'))
return ' '.join(says)
def run(s):
print(s)
os.system(s)
def say_it(s):
run('espeak "%s"' % s)
##################
# Default values #
##################
# Activate 5 minutes later.
t = datetime.now() + timedelta(1.0/24/3600*60*5)
t = time(t.hour, t.minute, t.second)
time_default = t.strftime('%H:%M:%S')
command = '/sbin/halt'
##########################
# Command line arguments #
##########################
pars = OptionParser()
pars.add_option('-t', '--time', default=time_default,
help='Hour:Minute:Second, default ' + time_default)
pars.add_option('-c', '--command', default=command,
help='Execute command, default: ' + command)
option, remain = pars.parse_args(sys.argv[1:])
if option.time:
if option.time[0] == '+': # n detik lagi
t = datetime.now() + timedelta(1.0/24/3600 * int(option.time))
hour, minute, second = t.hour, t.minute, t.second
else:
hour, minute, second = map(lambda x: int(x), option.time.split(':'))
if option.command:
command = option.command
# Convert time to datetime
target_time = time(hour, minute, second)
current_time = datetime.now()
if current_time.time() <= target_time:
target_time = datetime(current_time.year,
current_time.month,
current_time.day,
target_time.hour,
target_time.minute,
target_time.second)
else:
tomorrow = current_time + timedelta(1)
target_time = datetime(tomorrow.year,
tomorrow.month,
tomorrow.day,
target_time.hour,
target_time.minute,
target_time.second)
print('Now %s' % datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
print('Target %s' % target_time.strftime('%d-%m-%Y %H:%M:%S'))
print('Execute command: %s' % command)
# Let's loop
last_say = ''
last_say_time = None
first_time_say = True
minute_still_a_long = 15 * 60
last_minute = 5 * 60
second_still_a_long = 60
last_second = 10
it_is_time = 5
while True:
current_time = datetime.now()
remain_time = target_time - current_time
if remain_time.seconds > last_second:
sleep(1)
print('Rest %d seconds' % remain_time.seconds)
if remain_time.seconds <= 0:
say_it("It's time")
break
say = nice_say(remain_time)
if say == last_say:
print('Still the same sentence')
continue
if last_say_time:
last_say_delta = datetime.now() - last_say_time
if first_time_say:
print('First time loop')
first_time_say = False
# If there is still a "hour" say every 15 minutes.
# If less than 5 minutes say every minute.
# If there is still a "minute" say every 5 minutes.
# If more than 10 seconds, say every 5 seconds.
# Otherwise, say every second.
elif 'hour' in say and last_say_delta.seconds < minute_still_a_long:
print('There is still a "hour", would say in %d seconds' % (
minute_still_a_long - last_say_delta.seconds))
continue
elif 'minute' in say:
if remain_time.seconds < last_minute:
if last_say_delta.seconds < second_still_a_long:
print('Less than %d seconds, would say in %d seconds' % (
second_still_a_long,
second_still_a_long - last_say_delta.seconds))
continue
elif last_say_delta.seconds < last_minute:
print('There is still a "minute", would say in %d seconds' % (
last_minute - last_say_delta.seconds))
continue
elif remain_time.seconds > last_second:
if last_say_delta.seconds < it_is_time:
print('Stay %d seconds, would say in %d seconds' % (
remain_time.seconds,
it_is_time - last_say_delta.seconds))
continue
say_it(say)
last_say = say
last_say_time = datetime.now()
run(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment