My very own Twitter Daemon

I've built myself a twitter notifications daemon out of duct-tape and spit (with liberal application of bash)!

First of all, here is the daemon script itself. It is in bash, and runs in a continuous loop until killed. I use several components, most notably "twidge" to download new messages. Twidge is a command line utility for twitter. When twidge receives new messages, I display them with notify-send.

#!/bin/bash
echo "Starting demon..."
 
while true
do
  echo "Downloading messages on "`date`
  messages=`twidge -c ~/.twidgecron lsrecent -alsu|tac|awk -F "t" '{print $2,$4}'`
  if [ ! -z "$messages" ]
  then
    echo "New messages!"
    for terminal in `ls /dev/pts`
    do
      echo "" /dev/pts/$terminal
      echo "New Tweets!" /dev/pts/$terminal
    done
 
    echo "$messages"|while read message
    do
      message=($message)
      sender=${message[0]}
      icon=/usr/share/icons/gnome/scalable/status/mail-unread.svg
      icon=`bash ~/scripts/twitter-user-pic $sender`
      content=${message[@]:1}
      notify-send -i $icon -t 15000 "$sender" "$content"
      for terminal in `ls /dev/pts`
      do
        echo "$sender $content" /dev/pts/$terminal
      done
    done
  else
    echo "Nothing new."
  fi
  sleep 60
done

The "twitter-user-pic" bash script downloads and stores user's profile images. It looks like this.

#!/bin/bash
 
if [ ! -z "$1" ]
then
  picture="$HOME/graphics/avatars/twitter/$1"
  if [ -f "$picture" ]
  then
    echo $picture
  else
    url=`python2.6 ~/scripts/python/twitchy/twitchy.py picture -u "$1"`
    if [ ! -z "$url" ]
    then
      picture="$HOME/graphics/avatars/twitter/$1"
 
      wget -qO - "$url"  "$picture"
 
      echo $picture
    fi
  fi
fi

"twitchy.py" is my own little Python script that can currently only query user profile picture URLs (may be extended later). As you can see, the twitter-user-pic script checks a local file cache to see if the picture exists, and otherwise downloads it. Everything about this is rickety and hacky, starting from the lack of file extensions.

This is my python script:

#!/usr/bin/python2.6
import sys;
import getopt;
import twitter;
 
def main(argv):
  api = twitter.Api();
  api.SetCredentials("arancaytar", "hunter2"); // Don't bother trying.
  
  command = ""
  user = ""
 
  if argv[0][0] != "-":
    command = argv[0]
    argv = argv[1:]
  opts, args = getopt.getopt(argv, "c:u:", ["command=", "user="]);
  for opt, arg in opts:
    if opt in ("-c", "--command"):
      command = arg
    elif opt in ("-u", "--user"):
      user = arg
  if user != "":
    account = api.GetUser(user)
    if command == "picture":
      print account.profile_image_url
  else:
    if command == "picture":
      print "picture command requires -u user"
 
 
main(sys.argv[1:])

So now you can see that while the endless-loop script runs, I will get messages sent both to the desktop (where they are unfortunately not clickable, as notify-send doesn't do actions). Instead, I'm broadcasting them (again, in a pretty dirty way, using /dev/pts/*) to all open TTYs (which I'm anticipating will crash something badly, but nothing has happened so far).

Then I have a starting and stopping script that works like this:

#!/bin/bash
if [ "$1" == 'start' ]
then
  if [ ! -f "$HOME/.twitterd.pid" ]
  then
    echo "Starting"
    ~/scripts/twitcron.sh /dev/null 2/dev/null 
    echo $!  "$HOME/.twitterd.pid"
  else
    echo "Already running: "`cat $HOME/.twitterd.pid`
  fi
else
  if [ "$1" == 'stop' ]
  then
    if [ -f "$HOME/.twitterd.pid" ]
    then
      echo "Stopping"
      pid=`cat $HOME/.twitterd.pid`
      kill $pid
      rm $HOME/.twitterd.pid
    else
      echo "Not running."
    fi
  fi
fi

And it actually works! I've launched the process and now get those tweets live to the desktop and the terminal. It's great. Smile

This is a Cool App

This is great! I've been looking for something that would notify me when I get a response for a tweet. Thanks for sharing this great web design app.

Edit: Thanks for the compliment, but leave your spam at home.

Post new comment

  • You may use these tags: [abbr], [acronym], [b], [center], [code], [color], [cpp], [css], [define], [diff], [dtd], [font], [h3], [h4], [h5], [h6], [hr], [html], [i], [image], [img], [java], [javascript], [justify], [left], [list], [mysql], [node], [ol], [perl], [php], [python], [quote], [right], [s], [sh], [size], [sql], [sub], [sup], [table], [u], [ul], [url], [wikipedia], [xml]
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Easily link to terms in various wikis. For help, see <a href="/interwiki/3">interwiki</a>.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Syndicate content