| 
 我是个嗜睡者,我希望有一个闹钟能够很方便的进行设定,然后随机播放一首歌曲来叫醒我。  
 
  
一段时间以来,我都在用这个程,但是这个程序需要很多令人心烦的用户输入。这对于一个嗜睡的人来说实在不是什么理想的解决方案(我就忍受不了,所以才会有这篇博文)。但是,这个程序的“fire()”命令工作的倒是非常好。要使用这个程序你需要AP scheduler以及MPG321(sudo apt-get install mpg321)  
但是,这种方案对我来说还是行不通。我希望能有一种方式可以改变我每天都在使用的闹钟的用途(在这种方式下我不需要修改我的工作流程)。我登录了Google Calender(Google日历),因为每天我几乎都可以使用任何设备来为我的日程添加事件。搜索了一下我发现使用Google日历的Python API来实现我心中所想的方式一点都不难。  
下面开始吧,你需要下载并安装Google Data Library。我使用的是这个版本。从顶层目录解压缩.tar.gz文件,通过setup.py来安装。然后运行tests/run_data_tests.py来检查是否一切都安装成功。我这边都是正常的,如果你那边没法工作,请参阅由Google官方撰写的指南,那上面会指引你如何安装和运行。  
这个程序主要的部分可以归结为一条单独的布尔语句。但是首先,我们需要先设定一些库和API。Google日历的Python API产生的是rfc3339时间,对于这个应用来说有很多信息都是不相关的  
为了转换时间格式,我在StackOverflow上找到了一些有用的信息。  
最后贴一下代码: [mw_shl_code=applescript,true]#These are the imports google said to include 
import gdata.calendar.service 
import gdata.service 
import atom.service 
import gdata.calendar 
import gdata.calendar 
import atom 
import getopt 
import sys 
import string 
import time 
import xe #for the time comparator 
from feed.date.rfc3339 import tf_from_timestamp #also for the comparator 
from datetime import datetime #for the time on the rpi end 
from apscheduler.scheduler import Scheduler #this will let us check the calender on a regular interval 
import os, random #to play the mp3 later 
#this is more stuff google told me to do, but essentially it handles the login credentials 
calendar_service = gdata.calendar.service.CalendarService() 
calendar_service.email = 'youremail@yourdomain' #your email 
calendar_service.password = 'yourgcalpassword' #your password 
calendar_service.source = 'Google-Calendar_Python_Sample-1.0' 
calendar_service.ProgrammaticLogin() 
def FullTextQuery(calendar_service, text_query='wake'): 
print 'Full text query for events on Primary Calendar: \'%s\'' % ( text_query,) 
query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', text_query) 
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 ,"Number:",i,"Event Time:",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 "Comparison: Pass" 
print "---" 
songfile = random.choice(os.listdir("/home/pi/alarmclock/test_MP3s/")) #chooses the .mp3 file 
print "File Selected:", songfile 
command ="mpg321" + " " + "/home/pi/alarmclock/test_MP3s/" + "'"+songfile+"'"+ " -g 100" #plays the MP3 in it's entierty. As long as the song is longer than a minute then will only trigger once in the minute that start of the "wake" event 
print command 
os.system(command) #runs the bash command 
else: 
print "Comparison:Fail" #the "wake" event's start time != the system's current time 
def callable_func(): 
os.system("clear") #this is more for my benefit and is in no way necesarry 
print "------------start-----------" 
FullTextQuery(calendar_service) 
print "-------------end------------" 
scheduler = Scheduler(standalone=True) 
scheduler.add_interval_job(callable_func,seconds=5) 
scheduler.start() #runs the program indefinatly on an interval of 5 seconds[/mw_shl_code] 
感谢各位的阅读,如果你有什么问题或建议,请在评论栏中给我留言吧。  
 |