Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active September 4, 2017 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paultopia/13263b605862121e11a737155b2c7779 to your computer and use it in GitHub Desktop.
Save paultopia/13263b605862121e11a737155b2c7779 to your computer and use it in GitHub Desktop.
Easy tweetstorms for pythonista on ios. Usage: have a twitter account on device, run the script, approve access, type your text into the box.
# like the previous version (https://gist.github.com/paultopia/2cf73cbffe6dde1fd5f21b21d649e79b) but threading by reply previous tweet instead of first one
from dialogs import text_dialog
import twitter
from time import sleep
acct = twitter.get_all_accounts()[0]
# if there are multiple accounts this will need to be changed to get different account. I only have one account so don't care.
# right now I also don't care to have authentication error handling, will need to experiment with unauthenticated devices to figure out what it throws.
def numerate_tweets(tweetlist):
numtweets = len(tweetlist)
newoutlist = []
for index, tweet in enumerate(tweetlist):
numerizer = " (%d/%d)" % (index + 1, numtweets)
newoutlist.append(tweet.strip() + numerizer)
return newoutlist
def make_tweetstorm_list():
text = text_dialog()
if text: # bail on cancel
if len(text) > 140:
words = text.split(" ")
outlist = []
thistweet = ""
# will reserve ten characters for numbers, should handle reasonable-length tweetstorms.
for w in words:
if len(thistweet + " " + w) > 130:
outlist.append(thistweet)
thistweet = w.strip()
else:
thistweet = thistweet + " " + w.strip()
outlist.append(thistweet)
tweetlist = numerate_tweets(outlist)
else:
tweetlist = [text]
return tweetlist
print("tweetstorm cancelled!")
return None
def post_tweet_list_as_replies(tweetlist):
if tweetlist:
firstpost = twitter.post_tweet(acct, tweetlist[0])
if len(tweetlist) > 1:
params = {"in_reply_to_status_id": firstpost["id_str"]}
newlist = tweetlist[1:]
for tweet in newlist:
sleep(1) # to avoid pissing off the twitter api gods. might still run into rate-limit issues for really big tweetstorms
new_post = twitter.post_tweet(acct, tweet, params)
params = {"in_reply_to_status_id": new_post["id_str"]}
return firstpost # so it can be printed or saved or url-extracted or something in bigger scripts.
else:
print("you didn't give me any tweets to post, sorry.")
return None
def tweetstorm():
tweetlist = make_tweetstorm_list()
post_tweet_list_as_replies(tweetlist)
if __name__ == "__main__":
tweetstorm()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment