algorithmiaio/algorithmia-python

Unicode error

Closed this issue · 5 comments

Hi,

I'm having trouble to make this code work:

client = Algorithmia.client(ALGO_TOKEN)

algo_text = client.algo('util/Html2Text/0.1.4')
algo_summary = client.algo('nlp/Summarizer/0.1.3')

link_text = algo_text.pipe(title).result
summary = algo_summary.pipe(link_text).result

The first result come back without problem, but I cannot give it as an input to the next API. What am I doing wrong? I tried to encode/decode to/from unicode. I couldn't figure it out.

Log:

2016-10-21 11:14:58,167 - ERROR: Unexpected error: Traceback (most recent call last):
File "/Users/iseckert/DevProjects/bot/linkcrawler-python-bot/bot/slack_bot.py", line 53, in start
event_handler.handle(event)
File "/Users/iseckert/DevProjects/bot/linkcrawler-python-bot/bot/event_handler.py", line 18, in handle
self._handle_by_type(event['type'], event)
File "/Users/iseckert/DevProjects/bot/linkcrawler-python-bot/bot/event_handler.py", line 27, in _handle_by_type
self._handle_message(event)
File "/Users/iseckert/DevProjects/bot/linkcrawler-python-bot/bot/event_handler.py", line 85, in _handle_message
summary = algo_summary.pipe(link_text).result
File "/usr/local/lib/python2.7/site-packages/Algorithmia/algorithm.py", line 39, in pipe
return AlgoResponse.create_algo_response(self.client.postJsonHelper(self.url, input1, *_self.query_parameters))
File "/usr/local/lib/python2.7/site-packages/Algorithmia/client.py", line 53, in postJsonHelper
response = requests.post(self.apiAddress + url, data=input_json, headers=headers, params=query_parameters)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 107, in post
return request('post', url, data=data, json=json, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 53, in request
return session.request(method=method, url=url, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 468, in request
resp = self.send(prep, *_send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 376, in send
timeout=timeout
File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
body=body, headers=headers)
File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 353, in _make_request
conn.request(method, url, *_httplib_request_kw)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1097, in _send_request
self.endheaders(body)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 901, in _send_output
self.send(message_body)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 873, in send
self.sock.sendall(data)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 743, in sendall
v = self.send(data[count:])
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 709, in send
v = self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 14: ordinal not in range(128)

pmcq commented

Hi,
I might need a little more context on how you're running this code - python2 vs python3, and if you have a sample link where this is failing that might be helpful as well. It seems like something is trying to convert the link_text to ascii. I've been able to run the snippet above succesfully on some URLs that returned unicode.

I notice that your linkcrawler-python-bot is using requests version 2.9.1 - in a more recent version 2.10 (which I've tested with) they've upgraded urllib3 which seems to have gotten rid of some unicode encoding errors (https://github.com/kennethreitz/requests/issues/1723)

Hi pmcq,

Thanks for your answer!

It's python2. I've updated the requests module and still there is the error. Actuall this is the project/file: event_handler.py

pmcq commented

Hmm - okay. Do you have a sample URL that wasn't functioning? I can't seem to reproduce this easily and can pass in unicode data okay. Can you try using link_text = algo_text.pipe(title).result.encode('UTF-8')? Not sure this would help but I don't know why the SSL lib is trying to convert it to ascii.

Can you also include these lines in the top of your file:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

Let me know if it works!

@besirkurtulmus: It worked! Thank you very much!