Tweet It For Me

Very simple app for tweeting a message to twitter wall. You can either use it in commandline to tweet your message or Save your tweets in tweet_stack.py and tell the cronjob to automatically send tweets for you. ~ Enjoy Tweeting

Project hosted @ https://github.com/rajivm1991/Tweet-It-For-Me

Usage:

Commandline usage:

Copy the tweet script file to your /usr/bin/ directory

Automate your Tweets

Open terminal:

step1: $ crontab -e

step2: Add the line

0,30 * * * * python ~/snowfall/snowspace/Tweet-It-For-Me/tweet_it_for_me.py

and save the cronjob file.

step3: Save your tweet messages one per line in the tweet_stack.txt

step4: Just go and do your work or sleep :-).. Cronjob will send your tweets one by one in 30 minute intervals.

The Code:

def make_tweet_list(tweet):
    if len(tweet) <= 140:
            return 
    else:
        tweet_list = []
        while tweet:
            tweet_list.append(tweet[:137] + ('...' if tweet[137:] else ''))
            tweet = tweet[137:]
        return tweet_list

def pop_a_tweet(tweet_stack='tweet_stack.txt'):
    import os
    DATA_DIR = os.path.dirname(os.path.abspath(__file__))
    STACK_FILE = os.path.join(DATA_DIR, tweet_stack)
    tweets = open(STACK_FILE, 'r').readlines()
    if tweets:
        popped_tweet = tweets.pop().strip()
        f = open(STACK_FILE, 'w')
        f.writelines(tweets)
        f.close()
        return make_tweet_list(popped_tweet)

import sys
tweet_list = []
for tweet in sys.argv[1:]:
    tweet_list += make_tweet_list(tweet)
if not tweet_list:
    tweet_list = pop_a_tweet()
if tweet_list:
    from twitter import Api as TwitterApi
    print 'Attempting Twitter Api Login...'
    Twitter = TwitterApi(
        consumer_key="Your Twitter CONSUMER_KEY",
        consumer_secret="Your Twitter CONSUMER_SECRET",
        access_token_key="Your Twitter ACCESS_TOKEN_KEY",
        access_token_secret="Your Twitter ACCESS_TOKEN_SECRET"
    )
    for tweet in tweet_list:
        Twitter.PostUpdate(tweet)
        print 'Tweeting:', tweet
    print "Tweeted successfully!"
else:
    print "Oops!. No tweets found in the tweet_stack!"

Note:

  • Install python-twitter package
  • You must register for Twitter API and fill your API credentials in the required place inside the tweet_it_for_me.py file
Advertisements

Share your blooming thoughts here . . .

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s