forked from bubbl/SimpleGoogleAlarmClock
/
wakeup.py
85 lines (77 loc) · 5.09 KB
/
wakeup.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
#!/usr/bin/env python
## Simple Google Calendar Alarm Clock
## Author: Bart Bania
## Website: http://www.bartbania.com
import gdata.calendar.service as GServ #for connection with GCalendar
import time
import os
import random #to play the mp3 later
from ConfigParser import SafeConfigParser
from feed.date.rfc3339 import tf_from_timestamp #also for the comparator
from datetime import datetime, timedelta #for the time on the rpi end
from apscheduler.schedulers.background import BackgroundScheduler #this will let us check the calender on a regular interval
import logging # used for development. Not needed for normal usage.
logging.basicConfig(filename='wakeup.log', filemode='w')
parser = SafeConfigParser() # initiate Parser and read the configuration file
parser.read('wakeup.cfg')
#************************************************************************************#
#**** Global variables that can be changed in wakeup.cfg file ****#
#************************************************************************************#
email = parser.get('credentials', 'email')
password = parser.get('credentials', 'password')
q = parser.get('alarm', 'query')
mp3_path = parser.get('alarm', 'mp3_path')
calendar = parser.get('alarm', 'calendar')
date = (datetime.now() +timedelta(days=-1)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
endDate = (datetime.now() + timedelta(days=14)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
#************************************************************************************#
#**** Login credentials for your Google Account ****#
#************************************************************************************#
calendar_service = GServ.CalendarService()
calendar_service.email = email
calendar_service.password = password
calendar_service.source = 'SimpleGoogleAlarmClock'
calendar_service.ProgrammaticLogin()
#************************************************************************************#
#**** Main query ****#
#************************************************************************************#
def FullTextQuery(calendar_service):
print 'Full text query for events on Primary Calendar: \'%s\'' % (q)
query = GServ.CalendarEventQuery(calendar, 'private', 'full', q)
query.start_min = date # calling date to set the beginning of query range for the present day
query.start_max = endDate # calling endDate to limit the query range to the next 14 days. change tmedelta(days) to set the range
query.singleevents = 'true' # enables creation of repeating events
query.orderBy = 'startTime' # sort by event start time
query.sortorder = 'a' # sort order: ascending
feed = calendar_service.CalendarQuery(query)
for i, an_event in enumerate(feed.entry):
for a_when in an_event.when:
print " "
print an_event.title.text ,"Scheduled:",i,"For:",time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.start_time))),"Current Time:",time.strftime('%d-%m-%Y %H:%M')
if time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.start_time))) == time.strftime('%d-%m-%Y %H:%M'):
print "Waking you up!"
print "---"
songfile = random.choice(os.listdir(mp3_path)) # choosing by random an .mp3 file from direcotry
print "Now Playing:", songfile
# plays the MP3 in it's entierty. As long as the file is longer
# than a minute it will only be played once:
command ="mpg321" + " " + mp3_path + "'"+songfile+"'"+ " -g 100"
print command
os.system(command) # plays the song
else:
print "Wait for it..." # the event's start time is not the system's current time
#************************************************************************************#
#**** Function to be run by Scheduler ****#
#**** The prints are more for debug than actual necessity ****#
#************************************************************************************#
def callable_func():
os.system("clear")
print "----------------------------"
FullTextQuery(calendar_service)
print "----------------------------"
#************************************************************************************#
#**** Run scheduler service ****#
#************************************************************************************#
sched = BackgroundScheduler(standalone=True)
sched.add_interval_job(callable_func,seconds=10) # define refresh rate. Set to every 10 seconds by default
sched.start() # runs the program indefinatly on an interval of x seconds