I'm trying to pull a google calendar (successful) and then put the contents into a mysql db (almost successful). On one of the field I keep getting an encode error:
#!/usr/bin/python
from xml.etree import ElementTree
import gdata.calendar.data
import gdata.calendar.client
import gdata.acl.data
import atom.data
import time
import MySQLdb
calendar_client = gdata.calendar.client.CalendarClient() username = 'user@gmail.com' visibility = 'public' projection = 'full'
feed_uri = calendar_client.GetCalendarEventFeedUri(calendar=username, visibility=visibility, projection=projection)
# define mysql db connection/credentials
conn = MySQLdb.connect (host = "localhost", user = "test1", passwd = "test1", db = "googlecal")
cursor = conn.cursor ()
feed = calendar_client.GetCalendarEventFeed(uri=feed_uri)
print 'Events on Primary Calendar: %s' % (feed.title.text,)
for i, an_event in enumerate(feed.entry):
print '\t%s. %s' % (i, an_event.title.text,)
data_point = {}
data_point[ 'title' ] = (i, an_event.title.text,)
print '\t%s. %s' % data_point[ 'title' ]
for a_when in an_event.when:
print '\t\t%s. %s' % (i, an_event.content.text,)
data_point[ 'content' ] = (i, an_event.content.text,)
print '\t\tStart time: %s' % (a_when.start,)
data_point[ 'start' ] = (a_when.start,)
print '\t\tEnd time: %s' % (a_when.end,)
data_point[ 'end' ] = (a_when.end,)
cursor.execute("""insert into events (id, title, content, start, end)
values (NULL, %s, %s, %s, %s)""",
(data_point[ 'title' ],
data_point[ 'content' ],
data_point[ 'start' ],
data_point[ 'end' ]))
conn.commit()
cursor.close ()
conn.close ()
Traceback (most recent call last):
File "/home/unclecameron/Documents/workspace/google_pull_calendar/src/get_gmail.cal.py", line 45, in <module>
data_point[ 'end' ]))
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 247, in literal
return self.escape(o, self.encoders)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 185, in unicode_literal return db.literal(u.encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 404: ordinal not in range(256)
the db is utf-8, I've also tried inserting it after encoding like this:
data_point[ 'content' ].encode('utf-8', 'ignore')
and
data_point['content'] = i, smart_str(an_event.content.text)
the rest of the fields work fine, what am I doing wrong?