From 3dce83de9315b5286d099c6585c27f88d69996ce Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 28 Aug 2019 13:27:09 +0200 Subject: [PATCH] Move Client._upload to State --- fbchat/_client.py | 25 +------------------------ fbchat/_state.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 8d598e5..64a4614 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -1305,30 +1305,7 @@ class Client(object): ) def _upload(self, files, voice_clip=False): - """Upload files to Facebook. - - `files` should be a list of files that requests can upload, see - `requests.request `_. - - Return a list of tuples with a file's ID and mimetype. - """ - file_dict = {"upload_{}".format(i): f for i, f in enumerate(files)} - - data = {"voice_clip": voice_clip} - - j = self._payload_post( - "https://upload.facebook.com/ajax/mercury/upload.php", data, files=file_dict - ) - - if len(j["metadata"]) != len(files): - raise FBchatException( - "Some files could not be uploaded: {}, {}".format(j, files) - ) - - return [ - (data[mimetype_to_key(data["filetype"])], data["filetype"]) - for data in j["metadata"] - ] + return self._state._upload(files, voice_clip=voice_clip) def _sendFiles( self, files, message=None, thread_id=None, thread_type=ThreadType.USER diff --git a/fbchat/_state.py b/fbchat/_state.py index 1dbe020..dfbe278 100644 --- a/fbchat/_state.py +++ b/fbchat/_state.py @@ -266,3 +266,29 @@ class State(object): "queries": _graphql.queries_to_json(*queries), } return self._post("/api/graphqlbatch/", data, as_graphql=True) + + def _upload(self, files, voice_clip=False): + """Upload files to Facebook. + + `files` should be a list of files that requests can upload, see + `requests.request `_. + + Return a list of tuples with a file's ID and mimetype. + """ + file_dict = {"upload_{}".format(i): f for i, f in enumerate(files)} + + data = {"voice_clip": voice_clip} + + j = self._payload_post( + "https://upload.facebook.com/ajax/mercury/upload.php", data, files=file_dict + ) + + if len(j["metadata"]) != len(files): + raise _exception.FBchatException( + "Some files could not be uploaded: {}, {}".format(j, files) + ) + + return [ + (data[_util.mimetype_to_key(data["filetype"])], data["filetype"]) + for data in j["metadata"] + ]