From: RouxRC Date: Tue, 12 May 2015 19:06:24 +0000 (+0200) Subject: add doc for new twitter way to send medias (close #279 ) X-Git-Tag: twitter-1.17.1~3 X-Git-Url: https://jfr.im/git/z_archive/twitter.git/commitdiff_plain/cd830ea5eb90e5534f8574eacad6545e1b5017ee add doc for new twitter way to send medias (close #279 ) --- diff --git a/README b/README index 74e1a3c..d45e4f5 100644 --- a/README +++ b/README @@ -119,15 +119,24 @@ t.users.lookup( # to force a particular method, use `_method` t.statuses.oembed(_id=1234567890, _method='GET') -# Send a tweet with an image included (or set your banner or logo similarily) -# by just reading your image from the web or a file in a string: -status = "PTT ★" +# Send images along with your tweets: +# - first just read images from the web or from files the regular way: with open("example.png", "rb") as imagefile: - params = {"media[]": imagefile.read(), "status": status} -t.statuses.update_with_media(**params) - -# Or by sending a base64 encoded image: -params = {"media[]": base64_image, "status": status, "_base64": True} + imagedata = imagefile.read() +# - then upload medias one by one on Twitter's dedicated server +# and collect each one's id: +t_up = Twitter(domain='upload.twitter.com', + auth=OAuth(token, token_key, con_secret, con_secret_key)) +id_img1 = str(t_up.media.upload(media=imagedata)["media_id"]) +id_img2 = str(t_up.media.upload(media=imagedata)["media_id"]) +# - finally send your tweet with the list of media ids: +t.statuses.update(status="PTT ★", media_ids=",".join([id_img1, id_img2])) + +# Or send a tweet with an image (or set a logo/banner similarily) +# using the old deprecated method that will probably disappear some day +params = {"media[]": imagedata, "status": "PTT ★"} +# Or for an image encoded as base64: +params = {"media[]": base64_image, "status": "PTT ★", "_base64": True} t.statuses.update_with_media(**params) ``` diff --git a/twitter/api.py b/twitter/api.py index bb1ca6c..4fd97a7 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -419,18 +419,29 @@ class Twitter(TwitterCall): # to force a particular method, use `_method` t.statuses.oembed(_id=1234567890, _method='GET') - # Send a tweet with an image included (or set your banner or logo similarily) - # by just reading your image from the web or a file in a string: - status = "PTT ★" + # Send images along with your tweets: + # - first just read images from the web or from files the regular way: with open("example.png", "rb") as imagefile: - params = {"media[]": imagefile.read(), "status": status} - t.statuses.update_with_media(**params) + imagedata = imagefile.read() + # - then upload medias one by one on Twitter's dedicated server + # and collect each one's id: + t_up = Twitter(domain='upload.twitter.com', + auth=OAuth(token, token_key, con_secret, con_secret_key)) + id_img1 = str(t_up.media.upload(media=imagedata)["media_id"]) + id_img2 = str(t_up.media.upload(media=imagedata)["media_id"]) - # Or by sending a base64 encoded image: - params = {"media[]": base64_image, "status": status, "_base64": True} + # - finally send your tweet with the list of media ids: + t.statuses.update(status="PTT ★", media_ids=",".join([id_img1, id_img2])) + + # Or send a tweet with an image (or set a logo/banner similarily) + # using the old deprecated method that will probably disappear some day + params = {"media[]": imagedata, "status": "PTT ★"} + # Or for an image encoded as base64: + params = {"media[]": base64_image, "status": "PTT ★", "_base64": True} t.statuses.update_with_media(**params) + Searching Twitter:: # Search for the latest tweets about #pycon