Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active September 4, 2017 03:50
Show Gist options
  • Select an option

  • Save paultopia/2cf73cbffe6dde1fd5f21b21d649e79b to your computer and use it in GitHub Desktop.

Select an option

Save paultopia/2cf73cbffe6dde1fd5f21b21d649e79b to your computer and use it in GitHub Desktop.
tweetstorm.py (for ios pythonista. Obsolete, use newer version a gist or two in)
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:
reply_id = firstpost["id_str"]
params = {"in_reply_to_status_id": reply_id}
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
twitter.post_tweet(acct, tweet, params)
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