|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +'''Post a message to twitter''' |
| 4 | + |
| 5 | +__author__ = 'dewitt@google.com' |
| 6 | + |
| 7 | +import ConfigParser |
| 8 | +import getopt |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import twitter |
| 12 | + |
| 13 | + |
| 14 | +USAGE = '''Usage: tweet [options] message |
| 15 | +
|
| 16 | + This script posts a message to Twitter. |
| 17 | +
|
| 18 | + Options: |
| 19 | +
|
| 20 | + -h --help : print this help |
| 21 | + --consumer-key : the twitter consumer key |
| 22 | + --consumer-secret : the twitter consumer secret |
| 23 | + --access-key : the twitter access token key |
| 24 | + --access-secret : the twitter access token secret |
| 25 | + --encoding : the character set encoding used in input strings, e.g. "utf-8". [optional] |
| 26 | +
|
| 27 | + Documentation: |
| 28 | +
|
| 29 | + If either of the command line flags are not present, the environment |
| 30 | + variables TWEETUSERNAME and TWEETPASSWORD will then be checked for your |
| 31 | + consumer_key or consumer_secret, respectively. |
| 32 | +
|
| 33 | + If neither the command line flags nor the environment variables are |
| 34 | + present, the .tweetrc file, if it exists, can be used to set the |
| 35 | + default consumer_key and consumer_secret. The file should contain the |
| 36 | + following three lines, replacing *consumer_key* with your consumer key, and |
| 37 | + *consumer_secret* with your consumer secret: |
| 38 | +
|
| 39 | + A skeletal .tweetrc file: |
| 40 | +
|
| 41 | + [Tweet] |
| 42 | + consumer_key: *consumer_key* |
| 43 | + consumer_secret: *consumer_password* |
| 44 | + access_key: *access_key* |
| 45 | + access_secret: *access_password* |
| 46 | +
|
| 47 | +''' |
| 48 | + |
| 49 | + |
| 50 | +def PrintUsageAndExit(): |
| 51 | + print USAGE |
| 52 | + sys.exit(2) |
| 53 | + |
| 54 | + |
| 55 | +def GetConsumerKeyEnv(): |
| 56 | + return os.environ.get("TWEETUSERNAME", None) |
| 57 | + |
| 58 | + |
| 59 | +def GetConsumerSecretEnv(): |
| 60 | + return os.environ.get("TWEETPASSWORD", None) |
| 61 | + |
| 62 | + |
| 63 | +def GetAccessKeyEnv(): |
| 64 | + return os.environ.get("TWEETACCESSKEY", None) |
| 65 | + |
| 66 | + |
| 67 | +def GetAccessSecretEnv(): |
| 68 | + return os.environ.get("TWEETACCESSSECRET", None) |
| 69 | + |
| 70 | + |
| 71 | +class TweetRc(object): |
| 72 | + def __init__(self): |
| 73 | + self._config = None |
| 74 | + |
| 75 | + def GetConsumerKey(self): |
| 76 | + return self._GetOption('consumer_key') |
| 77 | + |
| 78 | + def GetConsumerSecret(self): |
| 79 | + return self._GetOption('consumer_secret') |
| 80 | + |
| 81 | + def GetAccessKey(self): |
| 82 | + return self._GetOption('access_key') |
| 83 | + |
| 84 | + def GetAccessSecret(self): |
| 85 | + return self._GetOption('access_secret') |
| 86 | + |
| 87 | + def _GetOption(self, option): |
| 88 | + try: |
| 89 | + return self._GetConfig().get('Tweet', option) |
| 90 | + except: |
| 91 | + return None |
| 92 | + |
| 93 | + def _GetConfig(self): |
| 94 | + if not self._config: |
| 95 | + self._config = ConfigParser.ConfigParser() |
| 96 | + self._config.read(os.path.expanduser('~/.tweetrc')) |
| 97 | + return self._config |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + try: |
| 102 | + shortflags = 'h' |
| 103 | + longflags = ['help', 'consumer-key=', 'consumer-secret=', |
| 104 | + 'access-key=', 'access-secret=', 'encoding='] |
| 105 | + opts, args = getopt.gnu_getopt(sys.argv[1:], shortflags, longflags) |
| 106 | + except getopt.GetoptError: |
| 107 | + PrintUsageAndExit() |
| 108 | + consumer_keyflag = None |
| 109 | + consumer_secretflag = None |
| 110 | + access_keyflag = None |
| 111 | + access_secretflag = None |
| 112 | + encoding = None |
| 113 | + for o, a in opts: |
| 114 | + if o in ("-h", "--help"): |
| 115 | + PrintUsageAndExit() |
| 116 | + if o in ("--consumer-key"): |
| 117 | + consumer_keyflag = a |
| 118 | + if o in ("--consumer-secret"): |
| 119 | + consumer_secretflag = a |
| 120 | + if o in ("--access-key"): |
| 121 | + access_keyflag = a |
| 122 | + if o in ("--access-secret"): |
| 123 | + access_secretflag = a |
| 124 | + if o in ("--encoding"): |
| 125 | + encoding = a |
| 126 | + message = ' '.join(args) |
| 127 | + if not message: |
| 128 | + PrintUsageAndExit() |
| 129 | + rc = TweetRc() |
| 130 | + consumer_key = consumer_keyflag or GetConsumerKeyEnv() or rc.GetConsumerKey() |
| 131 | + consumer_secret = consumer_secretflag or GetConsumerSecretEnv() or rc.GetConsumerSecret() |
| 132 | + access_key = access_keyflag or GetAccessKeyEnv() or rc.GetAccessKey() |
| 133 | + access_secret = access_secretflag or GetAccessSecretEnv() or rc.GetAccessSecret() |
| 134 | + if not consumer_key or not consumer_secret or not access_key or not access_secret: |
| 135 | + PrintUsageAndExit() |
| 136 | + api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret, |
| 137 | + access_token_key=access_key, access_token_secret=access_secret, |
| 138 | + input_encoding=encoding) |
| 139 | + try: |
| 140 | + status = api.PostUpdate(message) |
| 141 | + except UnicodeDecodeError: |
| 142 | + print "Your message could not be encoded. Perhaps it contains non-ASCII characters? " |
| 143 | + print "Try explicitly specifying the encoding with the --encoding flag" |
| 144 | + sys.exit(2) |
| 145 | + print "%s just posted: %s" % (status.user.name, status.text) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments