-
-
Save naoyamakino/840133 to your computer and use it in GitHub Desktop.
iCal feed parser for HearNear.org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two things. The server is returning an error message if you were to echo the response, you'd see this 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!