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

locale.Error: unsupported locale setting #27

Closed
pragyanshu1997 opened this issue Aug 8, 2019 · 18 comments
Closed

locale.Error: unsupported locale setting #27

pragyanshu1997 opened this issue Aug 8, 2019 · 18 comments
Labels
bug Something isn't working question Further information is requested
Milestone

Comments

@pragyanshu1997
Copy link

image
I'm new to python and getting this following error. FYI, I'm from India so, is there any issue in Indian Date Format?

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

Your date format should be supported. What language is your Facebook in?

Facebook language here: https://www.facebook.com/settings?tab=language

@mobeigi mobeigi added the question Further information is requested label Aug 8, 2019
@pragyanshu1997
Copy link
Author

The language is English (UK)

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

Run the program again with the logging level set to DEBUG in the code and email me logs (email on Github profile page). I'll have a closer look.

@Mr-Sawyer
Copy link

Mr-Sawyer commented Aug 8, 2019

I get the same error stack when the script tries to retrieve current month's birthdays.
If I edit the script in order to process only the next months it gets everything fine.
Probably something goes wrong when the script process facebook birthdays which are displayed by weekdays names, but I can't figure out why.
My locale settings are English (US) in Facebook and en_US.utf8 in Ubuntu WSL.

@bobpullen
Copy link

I'm also affected by this issue. My Facebook language settings are English (UK) and region format UK (English). @mobeigi I've emailed you my debug log.

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

Okay so basically Facebook uses day names for the following 7 days instead of a date when displaying the data. So if a birthday is in 3 days from today which is Thursday, it will show as Sunday instead of a date. To make things worse these day names are in the locale of the user. The best solution at the time was to generate day names based on the users locale which seems to work well for most locales.

Doing some testing now. Thanks for the logs @bobpullen

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

en_US and en_GB locales should work fine. I tested extensively on windows and centos machine.

I take it you're all experiencing this issue on unix machines.

Can somebody try this solution and try to run script again:
https://stackoverflow.com/a/36394262/1800854

@pragyanshu1997
Copy link
Author

What do you think locale could be @moeigi

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

The locale will match your Facebook language setting. As you're using English (UK) it will be en_GB.

Try this simple python program, does it also give you an error:

import locale

locale.setlocale(locale.LC_ALL, 'en_GB')

@Mr-Sawyer
Copy link

Mr-Sawyer commented Aug 8, 2019

Ok, understood the problem.
In line 635
locale.setlocale(locale.LC_ALL, user_locale)
You set the locale in the python environment using the user_locale variable, which stores facebook locale.
The problem is that if that locale is not installed in the system it gives "unsupported locale setting" error.
For instance the locales installed on my Ubuntu VM are:
immagine

And the locale actually used is:
immagine

As you can see the locale variable is en_US.UTF-8 instead of en_US and that should be the value that the function locale.setlocale should use.

Editing line 635 hardcoding the locale did the trick for me:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

@mobeigi
Copy link
Owner

mobeigi commented Aug 8, 2019

Then might be worth trying to set all locale by itself and then with locale plus utf-8 and UTF-8 postfixes. Would make it more robust I think. I'll do some research on the default locale settings on different hosts and try to make it decently robust.

Thanks for the help!

@Mr-Sawyer
Copy link

Mr-Sawyer commented Aug 8, 2019

Maybe using Babel library could help?
You could set the Facebook locale in Babel, without relying on system installed locales

Library: http://babel.pocoo.org/en/latest/index.html
Weekdays function: http://babel.pocoo.org/en/latest/api/core.html#babel.core.Locale.days

@bobpullen
Copy link

bobpullen commented Aug 8, 2019

Editing line 635 hardcoding the locale did the trick for me:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

Trying the equivilent hasn't worked for me. hardcoding to en_GB.UTF-8 still gives me...

"GET /ajax/mercury/composer_query.php?value=<REDACTED>&fb_dtsg_ag=<REDACTED>&__a=1 HTTP/1.1" 200 None [2019-08-09 00:14:47,510] urllib3.connectionpool DEBUG (_make_request) https://www.facebook.com:443 "GET /ajax/mercury/composer_query.php?value=<REDACTED>&fb_dtsg_ag=<REDACTED>&__a=1 HTTP/1.1" 200 None Traceback (most recent call last): File "./fb2cal.py", line 609, in parse_birthday_day_month parsed_date = datetime.strptime(birthday_date_str, locale_date_format_mapping[user_locale]) File "/usr/local/lib/python3.6/_strptime.py", line 565, in _strptime_datetime tt, fraction = _strptime(data_string, format) File "/usr/local/lib/python3.6/_strptime.py", line 362, in _strptime (data_string, format)) ValueError: time data 'Saturday' does not match format '%d/%m'

@mobeigi
Copy link
Owner

mobeigi commented Aug 9, 2019

@bobpullen Edited your post to remove some sensitive info in the debug logs, careful! Anyway, can you try en_GB.utf-8 as well (lower case utf-8). I think moving to use Babel instead of relying on system locales is a good idea in any case.

@Mr-Sawyer
Copy link

@bobpullen Just to be sure, did you put quotes around the locale? And what is the output of locale -a in your shell? Are you using the last version of the script (asking it because your error stack looks different from mine)?

@bobpullen
Copy link

@DharmaLotus, yes, was using quotes. I've scrapped everything, cloned the latest version and changed to lower-case locale as per @mobeigi's suggestion. All working now, and I'm a happy bunny. Kudos to @mobeigi 👍

@mobeigi mobeigi added the bug Something isn't working label Aug 10, 2019
@mobeigi mobeigi added this to the 1.0.3 milestone Aug 10, 2019
@mobeigi
Copy link
Owner

mobeigi commented Aug 10, 2019

Okay so for the final solution I decided to use babel as suggested by @DharmaLotus to fetch the day names. I kept system locales as a back up method in case the babel lookup failed. Also improved the system locale fallback approach to explicitly check the windows list of locales on Windows machines and the locale_alias list otherwise. Searches include the direct Facebook locale and variants with the UTF-8 and utf-8 variants.

I think this solves the issue for most people.

On my machine, the following locales fail both the babel and system locale lookups but we'll investigate further when a user using one of these locales shows up.

co_FR 
cx_PH 
en_UD 
es_LA 
gn_PY 
ja_KS 
sy_SY 
sz_PL 
tz_MA 
zz_TR 

Thanks all!

@pragyanshu1997
Copy link
Author

Okay. Thanks @mobeigi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants