-
-
Save naoyamakino/840133 to your computer and use it in GitHub Desktop.
from icalendar import Calendar, Event | |
import simplejson as json | |
import re | |
import web | |
from mimerender import mimerender | |
import sys | |
import urllib2 | |
event = {} | |
def get_lat(d): | |
if d.find("<Latitude>") > 0: | |
event['latitude'] = d[d.find("<Latitude>")+10:d.find("</Latitude>")] | |
def get_longitude(d): | |
if d.find("<Longitude>") > 0: | |
event["longitude"] = d[d.find("<Longitude>")+11:d.find("</Longitude>")] | |
def get_lat_long(detail): | |
for d in detail: | |
get_lat(d) | |
get_longitude(d) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print 'Usage: python parser.py [ics file]' | |
sys.exit() | |
else: | |
try: | |
cal = Calendar.from_string(open(sys.argv[1], 'rb').read()) | |
except: | |
print 'error opening ' + sys.argv[1] | |
sys.exit() | |
for component in cal.walk(): | |
if component.name == "VEVENT": | |
extra = {} | |
uid = component.get('UID') | |
match = re.search('[0-9]+', uid) | |
event['name'] = match.group(0) | |
event['radius'] = 500 | |
event['layer_id'] = 'go' | |
dtstart = component.get('DTSTART') | |
summary = str(component.get('SUMMARY')) | |
location = str(component.get('LOCATION')) | |
detail = component.get('X-TRUMBA-CUSTOMFIELD') | |
get_lat_long(detail) | |
date = str(dtstart) | |
event['date_from'] = date[:8] | |
extra['summary'] = summary | |
extra['location'] = location | |
event['extra'] = extra | |
jEvent = json.dumps(event) | |
print jEvent | |
req = urllib2.Request("https://api.geoloqi.com/1/place/create", jEvent, {'content-type':'application/json', 'authorization':'OAuth xxxxxxxxxxGEOLOQI_ACCESS_TOKENxxxxxxxx'}) | |
try: | |
response_stream = urllib2.urlopen(req) | |
response = response_stream.read() | |
print response | |
except urllib2.HTTPError, error: | |
print error.read() | |
continue |
Two things. The server is returning an error message if you were to echo the response, you'd see this error:
{"error":"json_error","error_description":"JSON Parse Error: Syntax error"}
Inspecting the JSON submitted to the server, there seems to be a "u" before the name, latitude and longitude. The parser is blowing up when it encounters this. I'm not really familiar with Python, but it seems that Python's str
function isn't actually a JSON encoder, it encodes to something that can be interpreted by Python, and it appends a "u" or a "d" before integers. See the Python docs on string formatting. A solution would seem to be to use the json library to do the encoding.
Second, you'll need to replace xxxxxxxxxxGEOLOQI_ACCESS_TOKENxxxxxxxx
with the access token available in the Google Doc. I'll email that to you separately.
Thanks!
But I am getting the Bad Request response though. Here is a traceback:
python parser.py events.ics
{'name': u'90689470', 'extra': {'location': 'ID/Chinatown Community Center, 719 8th Ave S, Seattle, WA 98104-3060', 'summary': 'Badminton'}, 'layer_id': 'go', 'date_from': '20101016', 'longitude': u'-122.322443', 'radius': 500, 'latitude': u'47.596499'}
Traceback (most recent call last):
File "parser.py", line 66, in
response_stream = urllib2.urlopen(req)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 121, in urlopen
return _opener.open(url, data)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 380, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 491, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 418, in error
return self._call_chain(_args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 353, in _call_chain
result = func(_args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 499, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
any idea?