Skip to content

Instantly share code, notes, and snippets.

@vimishor
Created October 31, 2012 11:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vimishor/3986575 to your computer and use it in GitHub Desktop.
Save vimishor/3986575 to your computer and use it in GitHub Desktop.
Tweet from your terminal.
{
"oauth_consumer_key": "XXX",
"oauth_consumer_secret": "XXX",
"access_token": "XXX",
"access_token_secret": "XXX"
}

Step 1

pip install tweepy  
wget --no-check-certificate -O ~/.twitter https://raw.github.com/gist/3986575/.twitter.config-example.json  
wget --no-check-certificate -O /usr/local/bin/twitter https://raw.github.com/gist/3986575/twitter.py && chmod +x /usr/local/bin/twitter

Step 2: Create a new twitter app at https://dev.twitter.com/apps

Step 3: Set OAuth keys and access tokens from your new app in ~/.twitter

Step 4: Test it

$ twitter
Syntax: twitter "Your tweet."
#!/usr/bin/env python
"""
Tweet from your terminal.
Create in your home directory a file named ".twitter" as showed in the example.
You can get the OAuth keys and access tokens from https://dev.twitter.com/apps
"""
import os.path, sys, tweepy, json
try :
message = sys.argv[1]
except IndexError as err:
sys.exit("Syntax: " + os.path.basename(__file__) +" \"Your tweet.\"")
if len(message) > 140:
sys.exit("Your message was longer than 140 characters...")
elif len(message) == 0:
sys.exit("Message not long enough!")
cfg_file = open(os.path.expanduser('~') + "/.twitter");
config = json.load(cfg_file);
consumer_key = config['oauth_consumer_key']
consumer_secret = config['oauth_consumer_secret']
access_token = config['access_token']
access_token_secret = config['access_token_secret']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print "Twitter.com: Hello " + api.me().name
api.update_status(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment