You are not logged in.

#1 2009-12-25 03:59:06

mutantpineapple
Member
From: UK
Registered: 2009-12-16
Posts: 29

STUpID - Simple Twitter Updater In Dmenu

This is my first attempt at shell scripting. It updates your twitter status. It's basically Zentwitter with the Zenity removed and replaced with dmenu. It's a bit silly but hey.

Why not use Zentwitter? I don't like having to store my twitter password in a plaintext file. STUpID will ask you for your username and password when you use it.

What if I want to store my password in a plaintext file? Then you're welcome to. STUpID will check for a ~/.stupidrc file, and if it exists it will use the first line of the file as your username and the second line as your password. If you like, you can store just the username in this file and STUpID will prompt you for only the password. And vice versa.

Why dmenu? Well it integrates nicely with my XMonad desktop and there are probably others out there like me. If you don't like dmenu then there are a million other twitter clients out there!

What features does STUpID have?
- It accepts your tweet as a command line argument if you want. If you have your username and password stored in .stupidrc, this makes STUpID a fire-and-forget command.
- It looks for ~/.dmenurc to correctly theme the dmenu instances.
- It can (sort of) apply different theming to the password prompt, so that you can for instance set the foreground to the same colour as the background. Voila, hidden password! Check the script to see how this would work.
- It allows you to set up your own output mechanism in the script. It defaults to stdout, but in the script I've commented out the method I use, which is a dzen2 instance located directly below dmenu which appears whenever there's an error.
- Uh, that's pretty much it. It's a simple script.

Is it finished? Nope, I still have to implement some sort of feedback on whether the update succeeded or failed. I'd welcome suggestions for how to implement this.

Is it buggy/poorly-written? Most likely. This is my first shell script, it's 4am and I'm on my third bottle of Baileys. (It's Christmas!) If you see anything wrong please tell me.

Why "STUpID"? I'm no good with names, and this one seemed too fun to resist. Rejected names include "DAFT - Dmenu Application For Twitter" and "SPAZ - SPaz Ain't Zentwitter". The latter was rejected because it was a lie - I started out with Zentwitter and just hacked out all the Zenity.

Your script sucks. Please tell me why. I did this mostly to learn a bit about shell scripting so the more lessons I can glean from it the better.


#!/bin/sh
## STUpID - simple twitter updater in dmenu
# Dan Smith <mutantpineapple@gmail.com>
## Basically just a hacked-up Zentwitter
# http://www.chimeric.de/projects:zentwitter

fetch_dmenu_settings()
{
    APP="$1" # This allows you to store a specific dmenu command for STUpID in .dmenurc
             # e.g. [[ $APP == "STUpIDpassword" ]] && DMENU="$DMENU -nf black"
             # This way you can alter the default theme if you wish or mask password input
             # by making the foreground the same colour as the background, etc.
    DMENURC=~/.dmenurc
    [[ -f $DMENURC ]] && . $DMENURC
    [[ -z $DMENU ]] && DMENU=/usr/bin/dmenu
    echo $DMENU
}

STRC=~/.stupidrc
CURL=/usr/bin/curl
SED=/bin/sed
URL="https://twitter.com/statuses/update.xml"

[[ ! -x $CURL ]] && output "Couldn't find $CURL" && exit 1
[[ ! -x $SED ]] && output "Couldn't find $SED" && exit 1

output()
{
    # Define an output method here if you like. Defaults to stdout.

    # echo "$1" | dzen2 -xs 1 -y 21 -fn xft:Verdana-12:bold -fg red -p 4 &
    echo "$1"
}

[[ -f $STRC ]] && TWUSER=$(${SED} -n 1p $STRC) && TWPASS=$(${SED} -n 2p $STRC)
[[ -z $TWUSER ]] && TWUSER=$(echo "" | $(fetch_dmenu_settings "STUpID") -p "Twitter Username: ")
[[ -z $TWUSER ]] && output "No username given" && exit 1
[[ -z $TWPASS ]] && TWPASS=$(echo "" | $(fetch_dmenu_settings "STUpIDpassword") -p "Twitter Password: ")
[[ -z $TWPASS ]] && output "No password given" && exit 1

count_characters()
{
    count=`echo "$TWEET" | wc -m`;
    [[ $count -gt 140 ]] && output "Your tweet is $count characters long (Maximum 140 characters!)" && unset TWEET
}

[[ $# != 0 ]] && TWEET="$*"

while [[ -z $TWEET ]]
do
        TWEET=$(echo "" | $(fetch_dmenu_settings "STUpID") -p "Tweet: ")
    [[ -z $TWEET ]] && echo "Nothing tweeted!" && exit 1 
        count_characters
done

$CURL -u ${TWUSER}:${TWPASS} -d status="${TWEET}" -d source="STUpID" -s ${URL}

Offline

Board footer

Powered by FluxBB