From ead9a3c0e93ad74dc9c3503bbc350d3722c8fc36 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 19 Sep 2017 16:36:08 +0200 Subject: [PATCH] Improved error handling, and improved uid-loading Requests would sometimes throw an error while retrieving the c_user cookie (If there were multiple cookies with this name) --- fbchat/client.py | 5 ++++- fbchat/graphql.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 53dc847..2abb7fa 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -157,7 +157,10 @@ class Client(object): self.payloadDefault = {} self.client_id = hex(int(random()*2147483648))[2:] self.start_time = now() - self.uid = str(self._session.cookies['c_user']) + self.uid = self._session.cookies.get_dict().get('c_user') + if self.uid is None: + raise Exception('Could not find c_user cookie') + self.uid = str(self.uid) self.user_channel = "p_" + self.uid self.ttstamp = '' diff --git a/fbchat/graphql.py b/fbchat/graphql.py index ecfca3b..83676f4 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -143,7 +143,11 @@ def graphql_queries_to_json(*queries): return json.dumps(rtn) def graphql_response_to_json(content): - j = json.loads(content, cls=ConcatJSONDecoder) + content = strip_to_json(content) # Usually only needed in some error cases + try: + j = json.loads(content, cls=ConcatJSONDecoder) + except Exception as e: + raise Exception('Error while parsing JSON: {}'.format(repr(content)), e) rtn = [None]*(len(j)) for x in j: