daily pastebin goal
82%
SHARE
TWEET

Google Calender Alarm Clock

a guest Aug 24th, 2014 431 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from datetime import datetime
  2. import os, random, string, sys
  3.  
  4. from apscheduler.schedulers.blocking import BlockingScheduler
  5. import gdata.calendar.service
  6.  
  7.  
  8. # Configuration
  9. ALARM_EVENT_NAME = 'wake'
  10. MP3_FOLDER = '/Users/username/path/to/mp3/collection'
  11. GOOGLE_EMAIL = 'username@gmail.com'
  12. GOOGLE_PASSWORD = 'password'
  13. FREQUENCY_CHECK = 5 # in seconds
  14.  
  15. calendar_service = gdata.calendar.service.CalendarService()
  16. calendar_service.email = GOOGLE_EMAIL
  17. calendar_service.password = GOOGLE_PASSWORD
  18. calendar_service.ProgrammaticLogin()
  19.  
  20. def full_text_query(calendar_service, text_query):
  21.     print 'Full text query for {} events on Primary Calendar:'.format(text_query)
  22.     query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', text_query)
  23.     feed = calendar_service.CalendarQuery(query)
  24.     today = datetime.today()
  25.     for i, event in enumerate(feed.entry):
  26.         for e in event.when:
  27.             alarm = e.start_time[:-13]
  28.             now = today.strftime('%Y-%m-%dT%H:%M')
  29.             print "Number:", i, "Event Time:", alarm, "Current Time:", now
  30.  
  31.             if alarm == now:
  32.                 print "Comparison: Pass\n---"
  33.                 song_file = random.choice(os.listdir(MP3_FOLDER))
  34.                 print "File Selected: {}".format(song_file)
  35.                 command = "mpg123 '{}/{}'".format(MP3_FOLDER, song_file)
  36.                 os.system(command)
  37.  
  38.             else:
  39.                 print "Comparison: Fail"
  40.  
  41. def callable_func():
  42.     os.system("clear")
  43.     print "------------start-----------"
  44.     full_text_query(calendar_service, text_query=ALARM_EVENT_NAME)
  45.     print "-------------end------------"
  46.  
  47. scheduler = BlockingScheduler()
  48. scheduler.add_job(callable_func, 'interval', seconds=FREQUENCY_CHECK)
  49. scheduler.start()
RAW Paste Data
Top