public
Last active

Tweet from the command line using twurl & OAuth

  • Download Gist
twit_twurl.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
################################
#!/usr/bin/env bash
# File: twit_twurl.sh
# Description: Tweet from the command line using twurl & OAuth
#
# Copyright 2010 George Ornbo (Shape Shed)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script requires that you have installed and setup twurl
# For instructions see: http://github.com/marcel/twurl
#
################################
 
################################
# Options
################################
# Turn on growl notifications (requires growlnotify http://growl.info/documentation/growlnotify.php)
# Set to 1 to enable
GROWLNOTIFY=0
 
################################
# API URL Constant
# Handled by twurl but listed here for clarity
################################
TWITTER_API_URL="http://api.twitter.com/1/"
 
################################
# Request format
# options: json, xml
################################
REQUEST_FORMAT=".json"
 
################################
# Twurl command
################################
TWURL='twurl'
 
################################
# Error messages
################################
ERROR_NO_INPUT="I didn't get a tweet so I can't post it :-( Exiting.."
ERROR_NO_METHOD="No method was supplied for the post request"
ERROR_TOO_LONG="The tweet is over 140 characters and too long"
ERROR_WRONG_NO_ARGUMENTS="The wrong number of arguments were received for this function"
 
################################
# Status Constants
################################
STATUS_UPDATE="/statuses/update"
 
function post_request
{
# We are expecting two arguments
# $1 is the API method, $2 is the string (tweet)
if [ $# -ne 2 ]
then
echo $ERROR_WRONG_NO_ARGUMENTS
exit 1
fi
# Check we have a method
# TO DO: Put methods in an array and check it is in array
if [ -z "$1" ]
then
echo $ERROR_NO_METHOD
exit 1
fi
# Check we have a tweet
if [ -z "$2" ]
then
echo $ERROR_NO_INPUT
exit 1
fi
# Check the tweet is not too long
if [ $(echo "$2" | wc -c) -gt 140 ]; then
echo $ERROR_TOO_LONG
exit 1
fi
$TWURL $1$REQUEST_FORMAT -d "status=""$2"
if [ $GROWLNOTIFY == 1 ]
then
growlnotify -t 'Tweet Posted!' -m "$2"
fi
}
 
################################
# Read user input
################################
printf "What are you doing today?\n"
read tweet
 
################################
# Send the tweet to twitter!
################################
post_request $STATUS_UPDATE "$tweet"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.