changeset 4:c199bd681e4e

Twitter API accepts direct URLs for media.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 29 Jul 2018 14:27:24 -0700
parents 98687befb7bf
children e7eef70acf58
files silorider/silos/twitter.py tests/test_silos_twitter.py
diffstat 2 files changed, 9 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/silorider/silos/twitter.py	Sun Jul 29 13:36:04 2018 -0700
+++ b/silorider/silos/twitter.py	Sun Jul 29 14:27:24 2018 -0700
@@ -70,11 +70,6 @@
         if not tweettxt:
             raise Exception("Can't find any content to use for the tweet!")
 
-        media_ids = upload_silo_media(entry, 'photo', self._media_callback)
-
         logger.debug("Posting tweet: %s" % tweettxt)
-        self.client.PostUpdate(tweettxt, media=media_ids)
-
-    def _media_callback(self, tmpfile, mt):
-        with open(tmpfile, 'rb') as tmpfp:
-            return self.client.UploadMediaChunked(tmpfp)
+        media_urls = entry.get('photo', [], force_list=True)
+        self.client.PostUpdate(tweettxt, media=media_urls)
--- a/tests/test_silos_twitter.py	Sun Jul 29 13:36:04 2018 -0700
+++ b/tests/test_silos_twitter.py	Sun Jul 29 14:27:24 2018 -0700
@@ -1,5 +1,4 @@
 import pytest
-from .mockutil import mock_urllib
 
 
 def test_one_article(cli, feedutil, tweetmock):
@@ -18,7 +17,7 @@
     ctx, _ = cli.run('process', feed)
     assert ctx.cache.wasPosted('test', 'https://example.org/a-new-article')
     toot = ctx.silos[0].client.tweets[0]
-    assert toot == ('A new article https://example.org/a-new-article', None)
+    assert toot == ('A new article https://example.org/a-new-article', [])
 
 
 def test_one_micropost(cli, feedutil, tweetmock):
@@ -33,7 +32,7 @@
     ctx, _ = cli.run('process', feed)
     assert ctx.cache.wasPosted('test', '/01234.html')
     toot = ctx.silos[0].client.tweets[0]
-    assert toot == ("This is a quick update.", None)
+    assert toot == ("This is a quick update.", [])
 
 
 def test_one_micropost_with_one_photo(cli, feedutil, tweetmock, monkeypatch):
@@ -48,18 +47,11 @@
     cli.appendSiloConfig('test', 'twitter', url='/blah')
     tweetmock.installTokens(cli, 'test')
 
-    with monkeypatch.context() as m:
-        import silorider.silos.twitter
-        mock_urllib(m)
-        m.setattr(silorider.silos.twitter.TwitterSilo, '_media_callback',
-                  _patched_media_callback)
-        ctx, _ = cli.run('process', feed)
+    ctx, _ = cli.run('process', feed)
 
     assert ctx.cache.wasPosted('test', '/01234.html')
-    media = ctx.silos[0].client.media[0]
-    assert media == ('/retrieved/fullimg.jpg', 1)
     toot = ctx.silos[0].client.tweets[0]
-    assert toot == ("This is a quick photo update.", [1])
+    assert toot == ("This is a quick photo update.", ['/fullimg.jpg'])
 
 
 def test_one_micropost_with_two_photos(cli, feedutil, tweetmock, monkeypatch):
@@ -75,24 +67,12 @@
     cli.appendSiloConfig('test', 'twitter', url='/blah')
     tweetmock.installTokens(cli, 'test')
 
-    with monkeypatch.context() as m:
-        import silorider.silos.twitter
-        mock_urllib(m)
-        m.setattr(silorider.silos.twitter.TwitterSilo, '_media_callback',
-                  _patched_media_callback)
-        ctx, _ = cli.run('process', feed)
+    ctx, _ = cli.run('process', feed)
 
     assert ctx.cache.wasPosted('test', '/01234.html')
-    media = ctx.silos[0].client.media[0]
-    assert media == ('/retrieved/fullimg1.jpg', 1)
-    media = ctx.silos[0].client.media[1]
-    assert media == ('/retrieved/fullimg2.jpg', 2)
     toot = ctx.silos[0].client.tweets[0]
-    assert toot == ("This is a photo update with 2 photos.", [1, 2])
-
-
-def _patched_media_callback(self, tmpfile, mt):
-    return self.client.UploadMediaChunked(tmpfile)
+    assert toot == ("This is a photo update with 2 photos.",
+                    ['/fullimg1.jpg', '/fullimg2.jpg'])
 
 
 @pytest.fixture(scope='session')
@@ -111,18 +91,10 @@
         assert access_token_secret == 'TEST_ACCESS_SECRET'
 
         self.tweets = []
-        self.media = []
-        self.next_mid = 1
 
     def PostUpdate(self, tweet, media=None):
         self.tweets.append((tweet, media))
 
-    def UploadMediaChunked(self, filename):
-        mid = self.next_mid
-        self.next_mid += 1
-        self.media.append((filename, mid))
-        return mid
-
 
 class TwitterMockUtil:
     def installTokens(self, cli, silo_name):