Get events in EG from Google Calendar

If you have a question or need help, this is the place to be.

Get events in EG from Google Calendar

Postby miljbee » Wed Nov 03, 2010 12:13 pm

Image

gCal.xml
(17.48 KiB) Downloaded 426 times


Hello,

I have got something that might interrests you if you have some complex schedules, or just want some easy controls of your schedules.

It's like a plugin, but it isn't !

It's a python script that runs when needed (you don't need to call it, it will start by itself - or something like that). This python script triggers events in EG according to what is stored in your Google Calendar(s). The main benefits compared to a "local scheduler" are :
- Google Calendars can be accessed in many ways. For exemple, if you have a modern smartphone, you can probably update your google Calendar from anywhere. You will get those changes in EG as soon as you have saved them in Google Calendar. Same thing for your computer at work, the one of your wife, ...
- A lot of ready to use Google Calendars already exists. You just have to pick them to get that schedule into EG. For example vacations, birthdays, ... for more complex calendars like sunrise/sunset, if you can get those data into excel, you can import them in google calendar.

If you don't want to read this long post, and just want to see ... here is the minimum you need to have it working :

1 - connect to your Google Calendar, open its settings and click the orange "xml" button which is at the bottom of the setting page. it's the private URL of your calendar. a popup should appear with something like this in it :
"http://www.google.com/calendar/feeds/ut6pmckl45mb8p4q8mf1dofriss%40group.calendar.google.com/private-3ae2397c29b9f36795be25b460e83d2c/basic" : Copy this URL to your clipboard. You can read more accurate instructions on how to get this URL by reading this : http://code.google.com/intl/fr/apis/cal ... agicCookie

2 - while you are into your Google calendar, create a test event that starts now and ends in a few hours, or less, it's up to you !

3 - get the attached gCal.xml file and load it into EG

4 - in EG, edit the "Watch gCal" python script which is in the autostart folder.

5 - goto the end of the script where you will find some eg.globals vars

6 - replace the eg.globals.gCal_privateURLs.append('blabla') with the url in your clipboard

7 - in this URL, replace the final "/basic" with "/full"

8 - if you need some proxy settings to connect to the internet, uncomment "SetUpProxy()" in "def WatchGCal():" and put your settings in "def SetUpProxy():"

9 - click OK

10 - right click "Watch gCal" and "enable Item"

11 - right click "Watch gCal" and "execute Item"

If there is an event currently running in your gCal, EG will trigger a corresponding event.
If there is no event at this time in your gCal, create one (see instruction 2 !), and EG will trigger an event in five minutes max.


More details :

The script runs at a specified interval time (default = 300s = 5 mins = eg.globals.gCal_checkEvery).
When it runs, it requests from Google the entries in your calendar that ends or start after now and before now + the interval time.
Google returns an XML feed that describes each entries that match the search query.
Those entries are saved locally if they are current (startTime<now and endTime>now) in eg.globals.gCal_currentEvents
If one entry is already in eg.globals.gCal_currentEvents, we do nothing
if one entry isn't in eg.globals.gCal_currentEvents, we add it and ask EG to trigger an event for the start of this entry
if entry is in eg.globals.gCal_currentEvents but not in the results returned by Google, we remove it and ask EG to trigger an event for the end of this entry.
For the entries that are not current (that starts after now, but before now + the interval time), we use them to compute the next check date (when we will connect again to Google).
It means that you will always get events from EG at the exact time the entries are stored in your Google calendar.
The only exception is if you modify an entry and schedule it within the current interval time.

The current entries are stored in eg.globals.gCal_currentEvents.
It's a python dictionary. The keys of this dictionary is "gCalTitle.entryTitle". If you don't know what I mean, put some events that runs now in Google, execute the python script. then create and run a new python script with this inside
Code: Select all
print eg.globals.gCal_currentEvents.keys().

So you can get an entry with
Code: Select all
eg.globals.gCal_currentEvents["gCalTitle.entryTitle"]

each entry in gCal_currentEvents is also a python dict. The keys are the attributes of the entry. here are a few examples :
Code: Select all
eg.globals.gCal_currentEvents["gCalTitle.entryTitle"]["location"]
eg.globals.gCal_currentEvents["gCalTitle.entryTitle"]["content"]
eg.globals.gCal_currentEvents["gCalTitle.entryTitle"]["startTime"]

startTime and endTime are struct_time in localTime : http://docs.python.org/library/time.html

You can get more informations on how this all works from Google :
http://code.google.com/intl/fr/apis/cal ... Parameters
http://code.google.com/intl/fr/apis/gda ... ml#Queries
http://code.google.com/intl/fr/apis/cal ... agicCookie

Of course, this python script might not fit your exact needs, but I think you have all the functions needed to get any information from Google Calendar services.
As an example, if you need to get only future events from a Google Calendar you can create a function GetGCalFutureEventsXML from GetGCalEventsByDateXML.
You will just need to modify the parameters passed to google in the query URL. If you visit this http://code.google.com/intl/fr/apis/cal ... Parameters, you will see that you just need to replace start-min and start-max with futureevents=true (and it might also be a good idea to set max-results). You will then need to parse the returned events and check if one or more events are current (because google consider that an event that start before now and ends after now is a futureevent). If it's current, then don't keep it ...

Finally, I am too impatient to share this code with you ! So, it hasn't been extensively tested. I don't really know what will happen if you loose your internet connection, or if Google returns a bad response (like service temporary unavailable). But I am sure we will be able to check this shortly !

Comments are welcome.

Enjoy !
Image
Attachments
demogCal.jpg
miljbee
Experienced User
 
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: Get events in EG from Google Calendar

Postby Foune » Tue Jul 26, 2011 4:47 pm

Hi miljbee,

First of all, thank you for your work. I've just tried this script, it is a really good job. This gives tons of new possibilities to any automation setup.
For now, I've made some basic tests, first of all I had to make a change to prevent errors when displaying some unicode data when accents are used. I had to modify "GetEltData(XML,EltTagName)", especially because str(result) won't work with accents. I decided to use this workaround :

Code: Select all
   eltXML=GetEltsByTagName(XML,EltTagName)[0]
    if len(eltXML.childNodes)!=0:
      result=eltXML.childNodes[0].data
      logThis("Exiting GetEltData and returning " + str(eltXML.childNodes[0].data.encode("ascii", "xmlcharrefreplace")),4)
    else:
      result=""
      logThis("Exiting GetEltData and returning nothing",4)


Let me know if you improved your script recently, thanks again,

Franck
Foune
 
Posts: 21
Joined: Sun Jul 03, 2011 7:08 pm

Re: Get events in EG from Google Calendar

Postby miljbee » Wed Jul 27, 2011 1:32 pm

Hello,

Thank you for the code update. I never used my script with unicode data. So i will trust your code. Feel free to post your updated script in this thread.

If you use this script, you should know that occasionnaly, Google will send back erroneous/garbage data that will make the script crash. It's annoying because if the script crashes, it's not re scheduled, so it won't generate anymore events since it won't run anymore unless you relaunch it. In the main func, I should have enclosed the main code in a try except block to prevent this, but I never took the time to correct this.

Perhap's I will sometime !
miljbee
Experienced User
 
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: Get events in EG from Google Calendar

Postby Foune » Wed Jul 27, 2011 2:28 pm

All right, I'll check all minor problems later, in real life. I keep you informed if I improve your script. Thanks again.

EDIT : by the way, at first start I often get this :

Code: Select all
Traceback (most recent call last) (1534):
  File "C:\Program Files\EventGhost\eg\Classes\Scheduler.py", line 137, in MainLoop
    func(*args, **kwargs)
  File "0", line 263, in WatchGCal
TypeError: 'NoneType' object is not callable
Foune
 
Posts: 21
Joined: Sun Jul 03, 2011 7:08 pm

Re: Get events in EG from Google Calendar

Postby miljbee » Thu Jul 28, 2011 8:05 am

Code: Select all
Traceback (most recent call last) (1534):
  File "C:\Program Files\EventGhost\eg\Classes\Scheduler.py", line 137, in MainLoop
    func(*args, **kwargs)
  File "0", line 263, in WatchGCal
TypeError: 'NoneType' object is not callable


I never experienced this problem. Sorry !
miljbee
Experienced User
 
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: Get events in EG from Google Calendar

Postby JMAutio » Fri Aug 19, 2011 10:18 pm

Hi

Could you explain how to add this to a current tree?
JMAutio
 
Posts: 1
Joined: Fri Aug 19, 2011 9:30 pm

Re: Get events in EG from Google Calendar

Postby kkl » Tue Aug 06, 2013 9:22 pm

miljbee,
Thanks very much for writing and sharing this. It provides a lot of power.
Using it with the Vera plug-in, I've found it much easier to control my MiCasaVerde Vera from EG than using the Google Calendar app directly on that device. I never could get that to work. I can also now control specific devices, not just scenes. :D
kkl
Experienced User
 
Posts: 223
Joined: Wed May 04, 2011 9:32 pm

Re: Get events in EG from Google Calendar

Postby blaher » Wed Aug 07, 2013 5:34 am

kkl wrote: I can also now control specific devices, not just scenes. :D


Do you mean you can't get devices to work with the Vera plugin?
blaher
Experienced User
 
Posts: 364
Joined: Thu Nov 17, 2011 1:27 am

Re: Get events in EG from Google Calendar

Postby kkl » Thu Aug 08, 2013 12:25 am

I mean that the Google Calendar app in Vera acts as a trigger. Without writing lua code, a trigger can only activate a scene, not a device. You can make a scene with a single device, but then you have to make an ON scene and an OFF scene. If you do use typical scene triggers with Google Calendar, you also have to create an OFF scene. Controlling Vera devices and scenes from EG using Google Calendar and the Vera plug-in is more straightforward IMHO.
kkl
Experienced User
 
Posts: 223
Joined: Wed May 04, 2011 9:32 pm

Re: Get events in EG from Google Calendar

Postby blaher » Thu Aug 08, 2013 2:06 am

I see, cheers. Would you mind sharing your EG .xml? It's interesting to see how people use it for home automation.
blaher
Experienced User
 
Posts: 364
Joined: Thu Nov 17, 2011 1:27 am

Re: Get events in EG from Google Calendar

Postby Pliind » Wed Feb 05, 2014 9:50 pm

Don't know what I'm doing wrong but the events shoot just fine. But my log is spammed by this:

"22:45:36 GetGCalEventsByDateXML : Pb while requesting google, Please check your internet connection and your private URLs or use an alternate scheduller ! ..."

And i could not really understand (or atleast not) get the futureevents to work properly.

What i want to acheive is to set of an event 1 hour before a specific google event is to occure. That there would save me soo much work and energy :P
Pliind
 
Posts: 37
Joined: Tue Dec 31, 2013 7:40 pm

Re: Get events in EG from Google Calendar

Postby goodcoproflcop » Thu Dec 04, 2014 9:59 am

Mhh I'm having the same issue;
GetGCalEventsByDateXML : Pb while requesting google, Please check your internet connection and your private URLs or use an alternate scheduller ! ...

Not sure what I'm doing wrong... Anyone got it working? (perhaps something changed in the google cal api, this plugin is a little old)
goodcoproflcop
 
Posts: 20
Joined: Tue Aug 13, 2013 2:18 pm

Re: Get events in EG from Google Calendar

Postby goodcoproflcop » Thu Dec 04, 2014 11:03 am

Ow can somebody please move this topic to "Plugin support"?
goodcoproflcop
 
Posts: 20
Joined: Tue Aug 13, 2013 2:18 pm

Re: Get events in EG from Google Calendar

Postby Rauna » Wed Dec 10, 2014 10:19 pm

And I'm having the same issue
Rauna
 
Posts: 3
Joined: Wed Jan 30, 2013 11:29 am

Re: Get events in EG from Google Calendar

Postby Surio89 » Tue Jan 20, 2015 3:42 pm

The used Google calender API is outdated. We have to update to the new API. Has someone a working script?
Surio89
 
Posts: 9
Joined: Sat May 14, 2011 8:57 am

Next

Return to General Support

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests