bash twitter

There are probably lots of variants out there. In fact mine is largely copied from here Inspired by BLT

#!/bin/bash
# a twitter client (uses .netrc for auth)
 
confdir=~/.bt
seenfile=$confdir/seen
feedstyle=$confdir/btfeed.xsl
 
if [ "$1" == "-h" ]; then
 echo " use: $(basename $0) -h | [ tweet ]"
 echo "      -h help"
 echo "      -i install"
 echo "      no arguments gets latest updates"
 exit
fi
 
function binstall {
 local sheet='<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
 
<xsl:template match="/">
<xsl:apply-templates select="//status"/>
</xsl:template>
 
<xsl:template match="status">
<xsl:value-of select="id"/><xsl:text>: &lt;</xsl:text><xsl:value-of select="user/screen_name"/><xsl:text>&gt; </xsl:text>
    <xsl:value-of select="text"/><xsl:text>\n
 
</xsl:text>
</xsl:template>
</xsl:stylesheet>
'
 if [ ! -d $confdir ]; then mkdir -p $confdir; fi
 if [ ! -e $seenfile ]; then touch $seenfile; fi
 if [ ! -e $feedstyle ]; then
   echo -e $sheet > $feedstyle
 fi
}
 
function friends { 
 local tnow=$( date --utc +%s )
 local tthen=$( date --utc --reference=$seenfile +%s )
 if [ $(( tnow - tthen < 60 )) = 1 ]; then
   return
 fi
 local tmpfile=$( mktemp $confdir/result.XXXXXXXX )
 curl --connect-timeout 5 -s -n $1 | xsltproc $feedstyle - > $tmpfile
 local tmpu=$( mktemp ~/.bt/uniques.XXXXXXXX )
 cat $seenfile $tmpfile | sort -n | uniq -u > $tmpu
 cat $seenfile $tmpfile | sort -n | uniq > $seenfile
 cat $tmpu $tmpfile | sort -n | uniq -d | cut -d':' -f2-
 echo
 rm $tmpfile $tmpu 
 touch $seenfile
}
 
function update {
 curl -s -n -d "status=$msg" $1 &>/dev/null || echo "tweet broke"
}
 
 
if [ "$1" == "-i" ]; then
 binstall
 exit
fi
 
if [ -t 0 ]; then msg="$*"; else msg="$(cat -)"; fi
 
if [ "$msg" ];then
 update http://twitter.com/statuses/update.xml
else
 friends http://twitter.com/statuses/friends_timeline.xml
fi

Prerequisites

  • bash
  • curl
  • xsltproc
  • mktemp

How to
1. Copy the above script into ~/bin/bt and make it executable.
2. Write a ~/.netrc containing something like this:

machine twitter.com
login exampleuser
password examplepassword

3. run bt -i to ‘install’. This will create a ~/.bt/ directory with a stylesheet and seen status file.
4. To check your friends timeline run bt with no arguments.
5. To post a message run bt <message> or pipe into bt. (Maybe for the next enhancement I’ll check the length of the message).
6. For friends updates while you use your bash command line, put something like this in your ~/.bashrc:
export PROMPT_COMMAND="bt; $PROMPT_COMMAND"

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*