Guest User

googlecalendar

a guest
Sep 14th, 2013
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. #These are the imports google said to include
  2. import gdata.calendar.service
  3. import gdata.service
  4. import atom.service
  5. import gdata.calendar
  6. import gdata.calendar
  7. import atom
  8. import getopt
  9. import sys
  10. import string
  11. import time
  12. import urllib2
  13. import MySQLdb
  14.  
  15.  
  16. import xe #for the time comparator
  17. from feed.date.rfc3339 import tf_from_timestamp #also for the comparator
  18. from datetime import datetime #for the time on the rpi end
  19. from apscheduler.scheduler import Scheduler #this will let us check the calender on a regular interval
  20. import os, random #to play the mp3 later
  21.  
  22. #this is more stuff google told me to do, but essentially it handles the login credentials
  23. calendar_service = gdata.calendar.service.CalendarService()
  24. calendar_service.email = 'name@gmail.com' #your email
  25. calendar_service.password = 'password' #your password
  26. calendar_service.source = 'Google-Calendar_Python_Sample-1.0'
  27. calendar_service.ProgrammaticLogin()
  28.  
  29. def checkCalendarEvent(calendar_service, text_query, status, id):
  30.     print 'Full text query for events on Primary Calendar: \'%s\'' % ( text_query,)
  31.     query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', text_query)
  32.     feed = calendar_service.CalendarQuery(query)
  33.     for i, an_event in enumerate(feed.entry):
  34.         for a_when in an_event.when:
  35.             print "---"
  36.             print "id=%i,status=%s,sort=%s" % (id, status, text_query)
  37.             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')
  38.             print an_event.title.text ,"Number:",i,"Event Time:",time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.end_time))),"Current Time:",time.strftime('%d-%m-%Y %H:%M')
  39.             if time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.start_time))) == time.strftime('%d-%m-%Y %H:%M'):
  40.                 print "Comparison: Pass"
  41.                 print "---"
  42.  
  43.                 url1 = "http://192.168.170.13:400/calendar/request.php?s="+ str(id) + "_on"                
  44.                 urllib2.urlopen(url1).read()
  45.  
  46.             if time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.end_time))) == time.strftime('%d-%m-%Y %H:%M'):
  47.                 print "Comparison: Pass"
  48.                 print "---"
  49.  
  50.                 url1 = "http://192.168.170.13:400/calendar/request.php?s="+ str(id) + "_off"
  51.                 urllib2.urlopen(url1).read()
  52.            
  53.             if time.strftime('%d-%m-%Y %H:%M',time.localtime(tf_from_timestamp(a_when.end_time))) == time.strftime('%d-%m-%Y %H:%M'):
  54.                 print "Comparison: Pass"
  55.                 print "---"
  56.  
  57. def ReloadDatabase():
  58. # Open database connection
  59.     db = MySQLdb.connect("localhost","jochim","LUZtfrGU3NnqyfTh","pi_home" )
  60.  
  61. # prepare a cursor object using cursor() method
  62.     cursor = db.cursor()
  63.  
  64. # prepare a cursor object using cursor() method
  65.     cursor = db.cursor()
  66.  
  67. # Prepare SQL query to INSERT a record into the database.
  68.     sql = "SELECT * FROM pi_devices "
  69.     try:
  70.        # Execute the SQL command
  71.        cursor.execute(sql)
  72.        # Fetch all the rows in a list of lists.
  73.        results = cursor.fetchall()
  74.        for row in results:
  75.           id = row[0]
  76.           device = row[2]
  77.           status = row[5]
  78.           sort = row[6]
  79.           aktiv = row[7]
  80.           checkCalendarEvent(calendar_service, sort, status, id)
  81.       # Now print fetched result
  82.           #print "id=%i,device=%s,status=%s,sort=%s,aktiv=%s" % \
  83.                  #(id, device, status, sort, aktiv  )
  84.     except:
  85.         print "Error: unable to fecth data"
  86.  
  87. # disconnect from server
  88.     db.close()
  89.  
  90.  
  91. def callable_func():
  92.     os.system("clear") #this is more for my benefit and is in no way necesarry
  93.     print "------------start-----------"
  94.     ReloadDatabase()
  95.     #FullTextQuery1(calendar_service)
  96.     print "-------------end------------"
  97.  
  98. scheduler = Scheduler(standalone=True)
  99. scheduler.add_interval_job(callable_func,seconds=5)
  100. scheduler.start() #runs the program indefinatly on an interval of 5 seconds
Add Comment
Please, Sign In to add comment