I am trying to get started writing a Groupme bot using Groupy.
There have been a few related posts to this one, but they seemed a bit more technically challenging of issues, the solutions didn't seem efficient, and none of them solved the issues I'm having.
Here is the code I have so far:
import groupy
token = "{My Token}"
from groupy.client import Client
client = Client.from_token(token)
groups = list(client.groups.list_all())
for group in groups:
print(group)
Firstly, I don't believe this is related but, in eclipse it is giving a warning that groupy is an unused import, even though it seems to me that that is not so.
More importantly, the strange part about the issues I'm having is that the error messages change without any edits to the above code.
Here is an error message that I've gotten:
In each of these error messages, I've edited the beginning of the file locations... because I'm paranoid
Traceback (most recent call last):
File "\Projects\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 15, in <module>
<Group(name='One of my group's names')>
<Group(name='Another one of my group's names')>
print(group.name)
File "\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 27-28: character maps to <undefined>
So, this time the system actually returns a couple groups I am in. But then calls an exception because of a unicode error of sorts.
Without changing any code, I also get this error:
Traceback (most recent call last):
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 49, in data
return self.json()['response']
File "\Python\Python37-32\lib\site-packages\requests\models.py", line 808, in json
return complexjson.loads(self.text, **kwargs)
File "\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "\Python\Python37-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 60865 (char 60864)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "\Eclipse\eclipse-workspace\Gort's Cousin\groupme_bot.py", line 10, in <module>
groups = list(client.groups.list_all())
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 61, in autopage
self.items = self.fetch_next()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 51, in fetch_next
return self.fetch()
File "\Python\Python37-32\lib\site-packages\groupy\pagers.py", line 42, in fetch
return self.endpoint(**self.params)
File "\Python\Python37-32\lib\site-packages\groupy\api\groups.py", line 21, in _raw_list
return [Group(self, **group) for group in response.data]
File "\Python\Python37-32\lib\site-packages\groupy\session.py", line 51, in data
raise exceptions.InvalidJsonError(self._resp) from e
groupy.exceptions.InvalidJsonError: The JSON was incomplete/invalid
I am most likely incorrect, but it seems to me that there is some issue with the way groupy is aggregating the groups, and/or how I've set up my project to handle those calls.
Any help with this issue would be awesome.
I can also add some code from the groupy library if that becomes necessary.
TLDR: Getting a "UnicodeEncodeError" error using Groupy to write a groupme bot in Python
Solved:
It was an issue where group names with emojis were not playing nice with the Cp1252 encoding I have as a global setting in eclipse. I changed the run configuration of the file to encode to UTF-16BE. The emojis don't print correctly to the console, but all of the groups are printed with no errors.
Related
I'd like to download a YAML file from Gitlab using Python requests. I'm almost there, but I havent quite got to the cigar stage.
I am doing the following :-
GITLAB_FILE ="https://my_url/api/v4/projects/my_id/repository/files/path_and_filename.yaml/raw?ref=master&private_token=mytoken"
g=requests.request("GET", GITLAB_FILE, verify=False)
print(g.json())
Now, it all works in as much as I can get to the file ok, but when it comes to accessing the data the print(g.json()) throws an error, but then continues to print out the file contents as I'd hoped. The error is :-
Traceback (most recent call last):
File "/home/myproj/edm/lib/python3.7/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./http_test.py", line 73, in <module>
print (g.json())
File "/home/myproj/edm/lib/python3.7/site-packages/requests/models.py", line 917, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: [Errno Expecting value]
Once this is printed, it then proceeds to print out the contents of my yaml file correctly.
I suspect its something to do with the
print(g.json())
expecting a json format, and encountering a yaml file?
Any pointers as to how I can get the file contents error free would be helpful.
I've answered my own question.
Quite simply instead of
print(g.json())
I used
print(g.text)
Which did exactly what it said on the tin.
I'm using Pycharm professional 2019.03 to execute Python code Streaming Twitter Filter. The running code finish unsuccessfully with message error :
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/Twitter_Stream/twitter_filter.py", line 12, in <module>
with open(file_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/config/api.json'
When I ran the script print(os.getcwd()) the result is C:\Users\HP\PycharmProjects\Twitter_Stream
The Run/Debug configurations Windows show that the working directory is
C:\Users\HP\PycharmProjects\Twitter_Stream and all seem ok but I dont know why this message error. The api.json file is in the same directory than twitter_filter.py
I tried to replace the code :
file_path = '/config/api.json' by file_path = '/Users/HP/PycharmProjects/Twitter_Stream/config/api.json' or
file_path = 'C:/Users/HP/PycharmProjects/Twitter_Stream/config/api.json',
I have this message error :
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/Twitter_Stream/twitter_filter.py", line 13, in <module>
twitter_api = json.loads(f.read())
File "C:\Users\HP\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 7)
Please someone can help me to solve thise problem? Let me know if you need more information
Thank you !
(I would comment but I do not have enough rep points.)
When you see the JSONDecodeError, it means the JSON file is successfully being read by the module.
However, it appears the module deems the file invalid.
From past experiences, I would check if the JSON file is valid with this link: https://jsonformatter.curiousconcept.com/
I think what could be wrong is that your JSON file is using single quotes instead of double quotes, so not properly formatted.
I am currently trying to use folium library in python to create webmaps. I have a file world.json which contains geo_data. I have provided a link to the file at the end of this post. I tried the following code:
data = [json.loads(line) for line in open('world.json', 'r')]
and received the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\name\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How can I load this file?
What I want to achieve is essentially obtain the population data and create a Choropleth and overlay it on my webmap.
Edit: Forgot the link:
https://1drv.ms/u/s!Army95vqcKXpaooVAZU_g-VCAVw?e=vwTknq
Edit: Previous link to skydrive stopped working due to "high traffic". Below is link to dropbox, hopefully this works:
https://www.dropbox.com/s/gmm8db0g03rc7cv/world.json?dl=0
Good news/bad news:
It turns out that this file was encoded in a locale that we are not accustomed to, and json/ascii cannot make sense of some of the character encoding. I tried this, and it seems to be working for me -- with a major caveat:
with open("world.json", "r") as fh:
contents = fh.read()
asciiContents = contents.encode("ascii", errors="ignore")
data = json.loads(asciiContents)
The major caveat is that only 3 countries come through with no encoding errors:
>>> len(data["features"])
3
Maybe there another source for this data that is closer to a native english locale, or maybe someone else can provide wisdom in encoding foreign data in a more friendly way...
The open command will return a file handle, not string lines. I would do:
with open('world.json', 'r') as fh:
data = json.load(fh)
data will then be your contents converted to python (list or dictionary, etc)
I was trying to get the list of name of txt file that was written in Korean in the specified directory with the code below
dir_list = tf.gfile.Glob(engine.TXT_DIR+"/*.txt")
However, This one gives me the following error:
Traceback (most recent call last):
File "D:/Prj_mayDay/Prj_FrankenShtine/shakespear_reborn/main.py", line 108, in <module>
dir_list = tf.gfile.Glob(engine.TXT_DIR+"/*.txt")
File "D:\KimKanna's Class\python35\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 326, in get_matching_files
compat.as_bytes(filename), status)
File "D:\KimKanna's Class\python35\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 325, in <listcomp>
for matching_filename in pywrap_tensorflow.GetMatchingFiles(
File "D:\KimKanna's Class\python35\lib\site-packages\tensorflow\python\util\compat.py", line 106, in as_str_any
return as_str(value)
File "D:\KimKanna's Class\python35\lib\site-packages\tensorflow\python\util\compat.py", line 84, in as_text
return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 19: invalid start byte
Now, throughout some research, I found out the reason
The error is because there is some non-ascii character in the dictionary and it can't be encoded/decoded
However, I do not see any way to apply the solution into my code. or is there?
**if there is alternative code for this. It should be applicable for both cloud stroage bucket / my personal hard drive as the code above did.
I'm using python3, Tensorflow version of 1.2.0-rc2
so after few hours of fiddling around with my code I finally found the solution.
Afterall one of the file inside of the directory I specified had a name in Korean. After I took that out of the directory. problem was gone.
I'm getting an occasional error when trying to fetch a list of worksheets from gdata. This does not happen for all spreadsheets, but will consistently happen to the same spreadsheet for a period of several days to weeks. I suspected permissions, but was unable to find any special permissions for the spreadsheets that cause the error. I'm using OAuth2, gdata 2.0.18, and Python 2.6.8.
Traceback (most recent call last):
File "/mnt/shared_from_host/snake/base/fetchers/google_spreadsheet/common.py", line 176, in get_worksheet_list
feed = client.get_worksheets(spreadsheet_id)
File "/home/ubuntu/.virtualenvs/snakeenv/lib/python2.6/site-packages/gdata/spreadsheets/client.py", line 108, in get_worksheets
**kwargs)
File "/home/ubuntu/.virtualenvs/snakeenv/lib/python2.6/site-packages/gdata/client.py", line 640, in get_feed
**kwargs)
File "/home/ubuntu/.virtualenvs/snakeenv/lib/python2.6/site-packages/gdata/client.py", line 278, in request
version=get_xml_version(self.api_version))
File "/home/ubuntu/.virtualenvs/snakeenv/lib/python2.6/site-packages/atom/core.py", line 520, in parse
tree = ElementTree.fromstring(xml_string)
File "<string>", line 86, in XML
SyntaxError: no element found: line 1, column 0
This seems to be from the request getting an empty string as the response.
Does anybody have any idea on why this might not work, or troubleshooting ideas? Thanks.