Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding characters #302

Closed
klepretr opened this issue Jun 19, 2018 · 8 comments
Closed

Encoding characters #302

klepretr opened this issue Jun 19, 2018 · 8 comments

Comments

@klepretr
Copy link

Hi !
I would like to test today but I have an issue and I don't know where I can find a solution.
Do you know a solution for this ?

Thanks in advance

package version:

pip show fbchat
Name: fbchat
Version: 1.3.8
Summary: Facebook Chat (Messenger) for Python
Home-page: https://github.com/carpedm20/fbchat/
Author: Taehoon Kim; Moreels Pieter-Jan; Mads Marquart
Author-email: [email protected]
License: BSD License
Location: /usr/local/lib/python2.7/dist-packages
Requires: enum34, requests, lxml, beautifulsoup4
Required-by:

my test script script.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from fbchat import Client

client = Client('xxxxx', 'xxxx')
print client.fetchAllUsers()

execution result:

python script.py
Logging in xxxxx...
Login of xxxxx successful.
[Traceback (most recent call last):
  File "script.py", line 8, in <module>
    print client.fetchAllUsers()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 18: ordinal not in range(128)
@madsmtm
Copy link
Member

madsmtm commented Jun 20, 2018

Hmm, it looks like you have an invisible character inside the text fetchAllUsers? Try removing it, and writing it again?

@klepretr
Copy link
Author

I have exactly the same issue when I use my terminal

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from fbchat import Client
>>> client = Client('xxxxx', 'xxxxx')
Logging in xxxxx...
Login of xxxxx successful.
>>> print client.fetchAllUsers()
[Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 18: ordinal not in range(128)

To be sure this is a issue with encoding, I use print client.searchForUsers('bastien') because I know there are not characters with accents or what ever in this name. And as we can see, there are no problems.

python script.py  
Logging in xxxxx...
Login of xxxxx successful.
[<USER Bastien (xxxxx)>]

Maybe an issue with bad version of python or dependencies.

@madsmtm
Copy link
Member

madsmtm commented Jun 20, 2018

Okay, I narrowed it down to it being the print that was causing trouble. It turned the result into a string, and by doing so turned the User objects into strings. Apparently, doing so is broken in Python 2, and to fix it we have to use:

def __repr__(self):
    return self.__unicode__().encode('UTF-8')

Instead of:

def __repr__(self):
    return self.__unicode__()

Though this doesn't work in Python 3. I'll figure something out in a bit, and make a PR

@madsmtm
Copy link
Member

madsmtm commented Jun 20, 2018

Okay, I cannot for the life of me make this work in both Python 2 and 3, the temporary solution is that you either don't print the threads you have received, or use Python 3

@klepretr
Copy link
Author

Thanks a lot for your help.
No problems I will try with python 3.

@DanoPlurana
Copy link

Got exactly the same problem. Any workaround on python 2.7?

@madsmtm
Copy link
Member

madsmtm commented Jul 3, 2018

Not really, except not trying to print the objects directly. As I said, couldn't figure out how to make it work in both versions. So, instead of:

print client.fetchAllUsers()

you'd do

print [u.name for u in client.fetchAllUsers()]

@madsmtm
Copy link
Member

madsmtm commented Jan 9, 2020

This should be fixed in #399, a while ago

@madsmtm madsmtm closed this as completed Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants