Guest User

Untitled

a guest
Apr 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import time, datetime, subprocess, math, sys, os
  2.  
  3. def say(s, cmd):
  4. """
  5. Runs a command with the specified string s. 'cmd' should be a
  6. command line to be run in the default shell using a string template
  7. to specify where the string to speak should be placed and can
  8. include pipes if necessary (i.e. for the festival command line
  9. utility).
  10. """
  11.  
  12. nullfile = open(os.devnull, 'w') # null file to suppress stdout
  13. subprocess.Popen(cmd % s, stdout=nullfile, shell=True)
  14.  
  15. def seconds_until(dt):
  16. return time.mktime(dt.timetuple()) - time.time()
  17.  
  18. def countdown_to(target_time, only_if_below=10, end_statement=None, say_cmd='say "%s"'):
  19. said = set()
  20. while True:
  21. i = int(math.ceil(seconds_until(target_time)))
  22. if i < 0:
  23. if end_statement:
  24. say(end_statement, say_cmd)
  25. break
  26. if i <= only_if_below and i not in said:
  27. said.add(i)
  28. say(i, say_cmd)
  29. sys.stdout.write('%s. ' % i)
  30. sys.stdout.flush()
  31. time.sleep(0.1)
  32.  
  33. if __name__ == '__main__':
  34. countdown_to(
  35. datetime.datetime(2010, 1, 1, 0, 0, 0),
  36. 10,
  37. 'Happy new year!',
  38. 'echo "%s" | festival --tts'
  39. )
Add Comment
Please, Sign In to add comment