PageRenderTime 85ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/alarm.py

https://github.com/dachrisch/Bash-Utils
Python | 129 lines | 109 code | 3 blank | 17 comment | 2 complexity | b738406e69dfc3b9cd9e6933715dd55a MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. Alarm script
  4. ============
  5. Author: Laszlo Szathmary, 2011 (jabba.laci@gmail.com)
  6. Website: https://ubuntuincident.wordpress.com/2011/04/17/alarm-script/
  7. GitHub: https://github.com/jabbalaci/Bash-Utils
  8. A simple alarm script that plays a list of MP3s at a given time.
  9. Very useful if you leave your computer switched on during the night.
  10. Usage:
  11. ------
  12. ./alarm.py -p
  13. Play music. First do this to adjust volume! If the volume is low,
  14. you won't hear it in the morning.
  15. ./alarm.py -t 7h15
  16. Set alarm time. The format is HhM, where H is the hour (24-hour system),
  17. M is the minute, 'h' is the separator.
  18. ./alarm.py
  19. Set alarm with the default time. In my case it's 6h55.
  20. """
  21. import os
  22. import sys
  23. import glob
  24. from optparse import OptionParser
  25. from datetime import datetime
  26. from time import sleep
  27. MUSIC_DIR = '/home/jabba/bin/alarm/at_a_given_time/assets/rock'
  28. MEDIA_PLAYER = '/usr/bin/mplayer'
  29. DEFAULT_TIME = '6h55'
  30. def play_music():
  31. for f in sorted(glob.glob(os.path.join(MUSIC_DIR, '*.mp3'))):
  32. val = os.system("{0} \"{1}\"".format(MEDIA_PLAYER, f))
  33. if val == 2: # interrupted with CTRL-C
  34. sys.exit(val)
  35. def set_alarm(hour, minute):
  36. # autoflush
  37. sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
  38. sep = "=" * 19
  39. print sep
  40. print "| Alarm at {0:2}h{1:02}. |".format(hour, minute)
  41. print sep
  42. alarm_time = hour * 100 + minute
  43. while True:
  44. now = datetime.now()
  45. time = datetime.time(now)
  46. current_time = time.hour * 100 + time.minute
  47. if (current_time >= alarm_time) and (current_time - alarm_time <= 100):
  48. play_music()
  49. sys.exit(0)
  50. else:
  51. sys.stdout.write('.')
  52. try:
  53. sleep(10)
  54. except KeyboardInterrupt:
  55. print
  56. break # break out of 'while True'
  57. def check_alarm(alarm_time):
  58. msg = "{0} error: there is a problem with the alarm time.".format(sys.argv[0])
  59. try:
  60. alarm_time = alarm_time.lower()
  61. if 'h' not in alarm_time:
  62. alarm_time += 'h'
  63. hour, minute = alarm_time.split('h')
  64. if not minute:
  65. minute = '0'
  66. hour = int(hour)
  67. minute = int(minute)
  68. if not ( (0 <= hour <= 23) and (0 <= minute <= 59) ):
  69. print >>sys.stderr, msg
  70. sys.exit(1)
  71. except ValueError:
  72. print >>sys.stderr, msg
  73. sys.exit(1)
  74. return hour, minute
  75. def main(default=DEFAULT_TIME):
  76. parser = OptionParser(usage='%prog [options]')
  77. #[options]
  78. parser.add_option('-t',
  79. '--time',
  80. action='store',
  81. default=default,
  82. type='string',
  83. dest='alarm_time',
  84. help='Alarm time, ex.: 6h55.')
  85. parser.add_option('-p',
  86. '--play',
  87. action='store_true',
  88. default=False,
  89. dest='is_play',
  90. help = 'Play music. Useful for adjusting the volume.')
  91. options, arguments = parser.parse_args()
  92. #print options
  93. #print arguments
  94. if options.is_play:
  95. play_music() # play and
  96. sys.exit(0) # quit
  97. # else
  98. if options.alarm_time:
  99. hour, minute = check_alarm(options.alarm_time)
  100. set_alarm(hour, minute)
  101. if __name__ == "__main__":
  102. main()