Add send GIF images support

When uploading and sending GIF images, the keys are explicitly changed
to "gif_id" or "gif_ids" rather than "image_id" or "image_ids".
This commit is contained in:
Tang
2017-10-03 00:50:27 +08:00
parent 9d5f06b810
commit ee81620c14

View File

@@ -938,7 +938,7 @@ class Client(object):
return self._doSendRequest(data) return self._doSendRequest(data)
def _uploadImage(self, image_path, data, mimetype): def _uploadImage(self, image_path, data, mimetype, is_gif=False):
"""Upload an image and get the image_id for sending in a message""" """Upload an image and get the image_id for sending in a message"""
j = self._postFile(self.req_url.UPLOAD, { j = self._postFile(self.req_url.UPLOAD, {
@@ -949,9 +949,12 @@ class Client(object):
) )
}, fix_request=True, as_json=True) }, fix_request=True, as_json=True)
# Return the image_id # Return the image_id
if not is_gif:
return j['payload']['metadata'][0]['image_id'] return j['payload']['metadata'][0]['image_id']
else:
return j['payload']['metadata'][0]['gif_id']
def sendImage(self, image_id, message=None, thread_id=None, thread_type=ThreadType.USER): def sendImage(self, image_id, message=None, thread_id=None, thread_type=ThreadType.USER, is_gif=False):
""" """
Sends an already uploaded image to a thread. (Used by :func:`Client.sendRemoteImage` and :func:`Client.sendLocalImage`) Sends an already uploaded image to a thread. (Used by :func:`Client.sendRemoteImage` and :func:`Client.sendLocalImage`)
@@ -972,11 +975,14 @@ class Client(object):
data['specific_to_list[0]'] = 'fbid:' + str(thread_id) data['specific_to_list[0]'] = 'fbid:' + str(thread_id)
data['specific_to_list[1]'] = 'fbid:' + str(self.uid) data['specific_to_list[1]'] = 'fbid:' + str(self.uid)
if not is_gif:
data['image_ids[0]'] = image_id data['image_ids[0]'] = image_id
else:
data['gif_ids[0]'] = image_id
return self._doSendRequest(data) return self._doSendRequest(data)
def sendRemoteImage(self, image_url, message=None, thread_id=None, thread_type=ThreadType.USER): def sendRemoteImage(self, image_url, message=None, thread_id=None, thread_type=ThreadType.USER, is_gif=False):
""" """
Sends an image from a URL to a thread Sends an image from a URL to a thread
@@ -991,10 +997,10 @@ class Client(object):
thread_id, thread_type = self._getThread(thread_id, thread_type) thread_id, thread_type = self._getThread(thread_id, thread_type)
mimetype = guess_type(image_url)[0] mimetype = guess_type(image_url)[0]
remote_image = requests.get(image_url).content remote_image = requests.get(image_url).content
image_id = self._uploadImage(image_url, remote_image, mimetype) image_id = self._uploadImage(image_url, remote_image, mimetype, is_gif)
return self.sendImage(image_id=image_id, message=message, thread_id=thread_id, thread_type=thread_type) return self.sendImage(image_id=image_id, message=message, thread_id=thread_id, thread_type=thread_type, is_gif=is_gif)
def sendLocalImage(self, image_path, message=None, thread_id=None, thread_type=ThreadType.USER): def sendLocalImage(self, image_path, message=None, thread_id=None, thread_type=ThreadType.USER, is_gif=False):
""" """
Sends a local image to a thread Sends a local image to a thread
@@ -1008,8 +1014,8 @@ class Client(object):
""" """
thread_id, thread_type = self._getThread(thread_id, thread_type) thread_id, thread_type = self._getThread(thread_id, thread_type)
mimetype = guess_type(image_path)[0] mimetype = guess_type(image_path)[0]
image_id = self._uploadImage(image_path, open(image_path, 'rb'), mimetype) image_id = self._uploadImage(image_path, open(image_path, 'rb'), mimetype, is_gif)
return self.sendImage(image_id=image_id, message=message, thread_id=thread_id, thread_type=thread_type) return self.sendImage(image_id=image_id, message=message, thread_id=thread_id, thread_type=thread_type, is_gif=is_gif)
def addUsersToGroup(self, user_ids, thread_id=None): def addUsersToGroup(self, user_ids, thread_id=None):
""" """