You are not logged in.

#1 2009-04-23 12:06:30

gondil
Member
Registered: 2008-05-25
Posts: 19

Script creates+deletes Google Calendar-events. (free SMS-notification)

I posted this in another thread too, but since a discussion would be off-topic there (http://bbs.archlinux.org/viewtopic.php?id=64933):
Here a Python script that creates a Google Calendar-event in the main calendar, with as subject the contents of a specified file.
Feel free to post modifications/optimizations! I use it this way to get notified of unanswered conversations in finch (as in the thread above), it was easier to work with a file then with specifying arguments..

PS. The script uses python-mechanize, which is in community.

#!/usr/bin/python

# From: gondil
# To: the Arch Linux forums ;)
# Version 2 (deletion added)

import mechanize
import sys
import urllib2
import httplib
from urllib2 import HTTPError

# Create a mechanize browser
mech = mechanize.Browser()
mech.set_handle_robots(False)

# With Goggles desired UAgent
mech.addheaders = [("User-agent", "Java/1.5.0_06")]

# See the G-API: We need a some token here, that is displayed by /~pkelchte/ (mah personal php site on ESAT that only does that)
mech.open("https://www.google.com/accounts/AuthSubRequest?scope=http://www.google.com/calendar/feeds/&session=1&secure=0&next=http://homes.esat.kuleuven.be/~pkelchte/index.php")

# But before we get the token, we need to submit our user-data
mech.select_form(nr=0)
mech["Email"] = ""  # REPLACE THIS WITH YOUR GOOGLE ACCOUNT
mech["Passwd"] = "" # AND THIS WITH YOUR PASSWORD

try:
    mech.submit()
except:
    print "Did not submit credentials"
    sys.exit("Error in submitting credentials")

# Because that's not enough, we need to confirm that we want to go trough with this and submit another form...
mech.select_form(nr=0)
mech.submit(id='allow')

# By now, we have one token, but it's not the final token! We need *another* token, called the AuthSubSessionToken, sigh...
mech.addheaders = [("Authorization", "AuthSub token=\"" + mech.response().read() +  "\""), ("User-agent", "Java/1.5.0_06")]
mech.open("http://www.google.com/accounts/AuthSubSessionToken")

# A bunch of tokens later...
# Let's use urllib2 to do this POST request (some xml-y thing is the string you would manually type in the "New event" box on Google Calendar)

# Encore some headers
authsub = "AuthSub token=\"" + mech.response().read().replace("\n","").split("=")[1] + "\""
headers = {"Authorization": authsub, "User-Agent": "Java/1.5.0_06", "Content-Type": "application/atom+xml", "GData-Version": "2"}

# Read the file that we're interested in! Damn, it's so interesting!!
file = open('/home/gondil/public_html/fifo', 'r') # CHANGE THIS FILE WITH YOUR THING
message = file.read()
file.close()

# The actual event
event = """
    <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005'>
      <content type="html">""" + message  +  """</content>
      <gCal:quickadd value="true"/>
      </entry>
    """
req = urllib2.Request("http://www.google.com/calendar/feeds/default/private/full", event, headers)
calresponse = urllib2.urlopen(req)

# Normally, we stop here... but since Google likes traffic, we need to go to a slightly different url, with the same headers and POST data
req2 = urllib2.Request(calresponse.geturl(), event, headers)
try:
    calresponse2 = urllib2.urlopen(req2)

# You can check but normally this is a 201 CREATED response or something, I don't really care... It's my code, right :P
except HTTPError, e :
    # I placed this sleep to give the event at least a 20 second lifetime (poor, poor event...)
    import time
    time.sleep(20)
    
    # Retrieve the event's edit url
    eventurl = e.read().split("<link rel='edit' type='application/atom+xml' href='http://www.google.com")[1].split("'/>")[0]
    
    # The Deletion has to be done via httplib, because Google wants a DELETE request (urllib2 only handles GET and POST)
    conn = httplib.HTTPConnection("www.google.com")
    conn.request("DELETE", eventurl, "", headers)
    calresponse3 = conn.getresponse()
    
    # Again, they like to have a little more traffic, we need to append a session ID to that last url (we can find it in the redirect page)
    eventurl2 = calresponse3.read().split("HREF=\"")[1].split("\"")[0]
    
    # Ooh and here there is need of a new header, no questions please
    headers2 = {"Authorization": authsub, "User-Agent": "Java/1.5.0_06", "Content-Type": "application/atom+xml", "GData-Version": "2", "If-Match": "*"}
    conn.request("DELETE", eventurl2, "", headers2)
    calresponse4 = conn.getresponse()
    
    # No errors? Ok we can close the connection
    conn.close()

index.php looks like this (again I don't guarantee that I'll keep my index.php page like that for eternity, but I'll notify you when the url changes):

<?php
print $_POST['token'];
print $_GET['token'];
?>

Last edited by gondil (2009-04-23 22:45:38)

Offline

Board footer

Powered by FluxBB