]> jfr.im git - z_archive/twitter.git/commitdiff
add doc for new twitter way to send medias (close #279 )
authorRouxRC <redacted>
Tue, 12 May 2015 19:06:24 +0000 (21:06 +0200)
committerRouxRC <redacted>
Tue, 12 May 2015 19:06:24 +0000 (21:06 +0200)
README
twitter/api.py

diff --git a/README b/README
index 74e1a3cd4a8b77e24a79a0b531edcfec1e253de3..d45e4f5a0d6103c550777b73fa373eee9f4d3e31 100644 (file)
--- 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)
 ```
 
index bb1ca6c2be5c527aba1def43f5233ee8508afc8c..4fd97a74e0b7ed73ee4226c7f745ebd78238e0df 100644 (file)
@@ -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