Search code examples
pythontwitterpraw

How to execute twitter.Api.PostUpdate in loop?


This code executes with error:

  # some constants and auth before, looks not important
  topPosts = reddit.get_subreddit('funny').get_top(limit=3)
  for post in topPosts:
        twitter.PostUpdate(status = post.title, media = post.url)

Console log:

Traceback (most recent call last):
  File "script.py", line 17, in <module>
    twitter.PostUpdate(status = post.title, media = post.url)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 990, in PostUpdate
    media_additional_owners)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 1132, in UploadMediaChunked
    boundary = bytes("--{0}".format(uuid4()), 'utf-8')
TypeError: str() takes at most 1 argument (2 given)

If I do just post.label in loop it works perfectly.

If I execute only one (w/o loop) PostUpdate it works perfectly.

I think it's happening because PostUpdate is asynchronous, but can't figure out how to fix it. Please help.


Solution

  • This is a bug in python-twitter library and it's fixed in this PR. The problem is that bytes in python2 equals to str and accepts only one argument while in python3 bytes requires encoding as a second argument.