Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First release

  • Loading branch information...
commit 42ee32e2ca26cd43b743647fe0c6b677520cf8f7 1 parent 7387428
Gregory Haskins authored April 04, 2011
48  Makefile
... ...
@@ -0,0 +1,48 @@
  1
+# Build an executable, standalone version of tweet.py using PyInstaller
  2
+
  3
+
  4
+# These settings may change based on your system:
  5
+# -----------------------------------------------------------------------------
  6
+# The python interpreter
  7
+PY = python
  8
+
  9
+# Executable file extension
  10
+# POSIX: BINARY_EXT =
  11
+# Windows: BINARY_EXT = .exe
  12
+BINARY_EXT = .exe
  13
+
  14
+# The PyInstaller directory
  15
+PYINST = pyinstaller-1.5-rc1
  16
+# -----------------------------------------------------------------------------
  17
+
  18
+
  19
+# PyInstaller scripts
  20
+CONFIGURE = $(PYINST)/Configure.py
  21
+CONFIGDAT = $(PYINST)/config.dat
  22
+MAKESPEC = $(PYINST)/Makespec.py
  23
+BUILDER = $(PYINST)/Build.py
  24
+
  25
+# BASENAME is the name of the python script
  26
+BASENAME = tweet
  27
+SOURCE = $(BASENAME).py
  28
+SPECFILE = $(BASENAME).spec
  29
+
  30
+# compiled binary path
  31
+BINARY = dist/$(BASENAME)$(BINARY_EXT)
  32
+
  33
+# output files created during build
  34
+OUTFILES = build dist warn$(BASENAME).txt $(SPECFILE) *.log
  35
+
  36
+build: $(BINARY)
  37
+
  38
+$(BINARY): $(SPECFILE) $(CONFIGDAT)
  39
+	$(PY) "$(BUILDER)" "$(SPECFILE)"
  40
+
  41
+$(CONFIGDAT):
  42
+	$(PY) "$(CONFIGURE)"
  43
+
  44
+$(SPECFILE): $(SOURCE) $(CONFIGDAT)
  45
+	$(PY) "$(MAKESPEC)" --onefile "$@"
  46
+
  47
+clean:
  48
+	rm -rfv $(OUTFILES)
71  README.markdown
Source Rendered
... ...
@@ -0,0 +1,71 @@
  1
+Introduction
  2
+============
  3
+
  4
+`cmdline_tweet` is a simple Python application that allows you to send messages
  5
+to a Twitter account from the command line. It extends on the examples from
  6
+Jeff Miller's [tutorial on the subject][1] by improving reusability since the
  7
+same tool handles both authorization and tweeting.
  8
+
  9
+This tool allows you to create command-line programs tied to a single Twitter
  10
+account with all the authorization, etc., handled behind the scenes.
  11
+
  12
+Setup
  13
+=====
  14
+
  15
+Using `cmdline_tweet` first requires [registering an app with Twitter][2] to
  16
+obtain an OAuth consumer key and secret. This is a one-time step, so just open
  17
+up `tweet.py` and replace `__CONSUMER_KEY__` and `__CONSUMER_SECRET__` with
  18
+your strings and forget about it.
  19
+
  20
+The included `Makefile` may be used to bundle `tweet.py` into a standalone
  21
+binary with [PyInstaller][3], and only requires
  22
+specifying the paths to PyInstaller and the Python executable on your system.
  23
+Having standalone binaries can be helpful when deploying to machines without
  24
+Python installed.
  25
+
  26
+Usage
  27
+=====
  28
+
  29
+Each script/executable is tied to a single Twitter account, and all it does
  30
+is send a tweet on behalf of that account. This can be useful for server logs
  31
+or monitoring other automated processes. Or, simply issuing tweets without
  32
+leaving the comfort of the shell environment.
  33
+
  34
+When first run, the program checks for a configuration file with the same name
  35
+and path as itself. For instance, if the script was `/path/to/tweet.py`, then
  36
+it would look for `/path/to/tweet.py.cfg`. If that file is invalid or can't be
  37
+found, then the program opens up a browser window for you to authorize it with
  38
+your Twitter credentials. Once that step is complete, tweeting is as simple as:
  39
+
  40
+    ./tweet.py "All your base are belong to us"
  41
+
  42
+If everything succeeds, the tweet will be posted and the script exits with no
  43
+output. If there was an error (e.g. repeat tweet, network failure), it will be
  44
+printed to standard output. Deleting the `.cfg` file or renaming the executable
  45
+will re-trigger the setup process. This makes it possible to have several
  46
+scripts which tweet to different accounts live side-by-side without conflict.
  47
+
  48
+If you'll be using the script in a headless/server environment, you'll want to
  49
+copy the `.cfg` file from another machine with web browsing capabilities.
  50
+
  51
+Dependencies
  52
+============
  53
+
  54
+- Python >= 2.7 (only minor changes required for further compatibility)
  55
+- [tweepy][4]
  56
+
  57
+[1]: http://jeffmiller.github.com/2010/05/31/twitter-from-the-command-line-in-python-using-oauth
  58
+[2]: http://dev.twitter.com/apps
  59
+[3]: http://www.pyinstaller.org/
  60
+[4]: https://github.com/joshthecoder/tweepy
  61
+
  62
+TODO
  63
+====
  64
+
  65
+Ideas for future improvement:
  66
+
  67
+- Better backwards/forwards compatibility
  68
+- Have `Makefile` call `sed` to replace the appropriate, app-specific params?
  69
+- Integrate consumer key/secret into config file instead of hard-coding?
  70
+- Tweet to multiple accounts with same script/executable? (e.g. `./tweet.py 
  71
+  ACCTNAME MESSAGE`)
67  tweet.py
... ...
@@ -0,0 +1,67 @@
  1
+#!/usr/bin/env python
  2
+"""A simple, reusable command-line twitter client.
  3
+
  4
+Inspired by the example from Jeff Miller at:
  5
+http://jeffmiller.github.com/2010/05/31/twitter-from-the-command-line-in-python-using-oauth
  6
+    
  7
+"""
  8
+
  9
+import os
  10
+import sys
  11
+import webbrowser
  12
+import ConfigParser
  13
+
  14
+import tweepy
  15
+
  16
+CONSUMER_KEY = '__CONSUMER_KEY__'
  17
+CONSUMER_SECRET = '__CONSUMER_SECRET__'
  18
+CONFIG_FILE = os.path.join(os.path.dirname(__file__), 
  19
+                           "{}.cfg".format(sys.argv[0]))
  20
+
  21
+def tweet(message, access_key, access_secret):
  22
+    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  23
+    auth.set_access_token(access_key, access_secret)
  24
+    api = tweepy.API(auth)
  25
+    try:
  26
+        api.update_status(sys.argv[1])
  27
+    except tweepy.error.TweepError as e:
  28
+        print e
  29
+        sys.exit(1)
  30
+
  31
+def setup():
  32
+    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  33
+    auth_url = auth.get_authorization_url()
  34
+    print "Opening Twitter to get user authorization..."
  35
+    print auth_url
  36
+    webbrowser.open(auth_url)
  37
+    
  38
+    verifier = raw_input('Verification PIN: ').strip()
  39
+    auth.get_access_token(verifier)
  40
+    
  41
+    config = ConfigParser.RawConfigParser()
  42
+    config.add_section('OAuth')
  43
+    config.set('OAuth', 'access_key', auth.access_token.key)
  44
+    config.set('OAuth', 'access_secret', auth.access_token.secret)
  45
+    with open(CONFIG_FILE, 'wb') as f:
  46
+        config.write(f)
  47
+    
  48
+    print "Setup complete."
  49
+    print "You may now tweet using: {} MESSAGE\n".format(sys.argv[0])
  50
+    
  51
+
  52
+if __name__ == '__main__':
  53
+    config = ConfigParser.RawConfigParser()
  54
+    config.read(CONFIG_FILE)
  55
+    try:
  56
+        access_key = config.get('OAuth', 'access_key')
  57
+        access_secret = config.get('OAuth', 'access_secret')
  58
+    except ConfigParser.Error:
  59
+        print "Bad/missing settings file '{}'".format(CONFIG_FILE)
  60
+        setup()
  61
+        sys.exit()
  62
+    if len(sys.argv) > 1:
  63
+        tweet(' '.join(sys.argv[1:]), access_key, access_secret)
  64
+    else:
  65
+        print "usage: {} MESSAGE\n".format(sys.argv[0])
  66
+        print "Settings are read from '{}'".format(CONFIG_FILE)
  67
+        print "Deleting this file will reset the current configuration"

0 notes on commit 42ee32e

Please sign in to comment.
Something went wrong with that request. Please try again.