A month ago, I've played with Google Calendar API, creating, or rather adjusting a Google Calendar Alarm Clock for Raspberry Pi.
Today, I've finally found time in my head to tune the application. I've githubbed the application, brushed the code a bit, but most of all, I've created a separate config file to store password and application data.
The Configuration File
The Python code doesn't contain any authentication data. I've decided to move it to a separate file, as I believe it's easier for the user to set the variables in a separate place, where no code could be accidentally altered or deleted. The program gets the configuration thanks to ConfigParser that reads the configuration file and passes the variables to the program:
global email, password, q, mp3_path parser = SafeConfigParser() parser.read('wakeup.cfg') email = parser.get('credentials', 'email') password = parser.get('credentials', 'password') q = parser.get('alarm', 'query') mp3_path = parser.get('alarm', 'mp3_path')
The file is has a structure similar to what you would find on Microsoft Windows INI files. The configuration file consists of sections, led by a [section] header and followed by name = value entries. My config file consists of two sections (just my personal preference) one for credentials and the other for event query and mp3 path:
[credentials] email = email@example.com password = *** [alarm] query = wake mp3_path = /path/to/your/mp3/files/
Apart from cleaning the code a bit, I've simplified the event extraction from the calendar. The past events don't have to be deleted from the calendar. This was problematic in the initial version, as it depended on providing the start date manually to the program. Now, Python reads the current date itself and limits the calendar query to the next 14 days, so you can set as many wake up events as you wish. This is done by adding date and endDate variables to the program code:
date = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.000Z') endDate = (datetime.now() + timedelta(days=14)).strftime('%Y-%m-%dT%H:%M:%S.000Z')
Run The Alarm Clock!
I've created a simple bash script to run the alarm clock in background and prevent you from running several instances. Instead of doing:
you can run the
monitor_wakeup.sh script (rememder to
chmod +x it) that is included in the repository:
By default, it runs the alarm clock in background and if invoked again, unless the program was closed, informs you that the code is already running.
For now, to stop the program, you must use the kill command which may be a bit confusing and time consuming:
- Detect the wakeup.py PID (process identification number):
ps aux | grep wakeup
You should get something like:
pi 5411 32.2 1.8 14360 9096 pts/1 S 16:48 0:01 python wakeup.py
The PID it the first number after your user name, here: 5411
- Kill it:
sudo kill PID
sudo kill 5411
Run it in the morning!
For convenience, you don't have to have the alarm clock running all the time. Instead, you can set a crontab entry for the monitor. This will also prevent you from running several instances of the program. To do so, type the command
to edit the table. Add a line:
57 7 * * Mon-Fri python /path/to/wakeup.py
In this example, cron will automatically run the script from Monday to Friday (Mon-Fri) at 7:57 am. If you want to know more about crontab syntax, head here: Crontab - Quick Reference.