Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Slashdot.org

Journal James A. A. Joyce's Journal: Slashbot alpha 2

#!/bin/bash

##################################
#
# slashbot
# a public domain shell script
#
# A shell script for automatically and constantly checking the Slashdot
# front page for new stories in order to make getting the FP easier
#
# This will work fine if run in a random directory without arguments and
# without being run as root: it stores temporary files in /tmp and will
# send out a message using 'wall' when a new story is posted.
#
#################################
#
# 'slashbot' will take up to two arguments, both of which are optional.
#
# The first, if given, will be used as the number of seconds to pause for
# between each reloading of the Slashdot page: if this is set too low
# then this script could conceivably get you banned; at least 30 seconds is
# recommended unless you're very desperate.
#
# The second, if given, is a file from which HTTP cookies are read: this is
# useful if you want to masquerade as a logged in user. The cookie file must
# be in Lynx/Mozilla/Netscape format.
#
#################################

# naptime:
# how long to sleep for between front page grabs in seconds
# the default is two minutes, which is fairly conservative
if [ "$1" ]
then
    echo "Notice: setting naptime to $1 seconds" >&2
    NAPTIME="$1"    # first argument is naptime in seconds
else
    NAPTIME=120
fi

# mf_naptime:
# how long to sleep for between front page grabs in seconds when a story
# from the "Mysterious Future" is ready
# the default is one minute, which is OK for short lengths of time
if [ "$2" ]
then
    echo "Notice: setting mf_naptime to $2 seconds" >&2
    MF_NAPTIME="$2"
else
    MF_NAPTIME=60
fi

# cf:
# the location of a cookie file to use, if any
# the default is to read no cookies and use no cookies
if [ "$3" ]
then
    echo "Notice: using $3 as a cookie file" >&2
    CF="$3"
else
    CF=""
fi

ORIG_NAPTIME=$NAPTIME

#################################
# clean_die()
#################################
function clean_die()
{
    rm -f /tmp/slashbot_listing[12].txt /tmp/slashdot.html

    if [ "$1" != "" ]
    then
        exit $1
    else
        exit 0
    fi
}

#################################
# get_front_page_titles()
#    This function outputs to standard output the story titles listed on
#    the "light" cut-down version of the slashdot.org front page: it's
#    about 15-30KB; good for modem users and good for us!
#################################
function get_front_page_titles()
{
    if [ "$CF" != "" ]
    then
        curl --connect-timeout 3 --max-time 33 -b "$CF" \
            'http://slashdot.org/index.pl?light=1&noboxes=1' \
            2> /dev/null | \
            grep -v '<H2>News for nerds, stuff that matters</H2>' \
            > /tmp/slashdot.html
    else
        curl --connect-timeout 3 --max-time 33 \
            'http://slashdot.org/index.pl?light=1&noboxes=1' 2> /dev/null | \
            grep -v '<H2>News for nerds, stuff that matters</H2>' \
            > /tmp/slashdot.html
    fi

    # you need to be logged in for the next four lines to do anything
    if grep -q 'another story coming up soon' /tmp/slashdot.html
    then
        echo "New story currently in Mysterious Future" >&2
        NAPTIME=$MF_NAPTIME
    else
        NAPTIME=$ORIG_NAPTIME
    fi

    if test -s /tmp/slashdot.html
    then
        grep -o '<H2>.*</H2>' /tmp/slashdot.html | \
            sed 's/<A HREF=.*\/\">//g' | \
            sed 's/<\/A>//g' | \
            sed 's/<H2>//g' | \
            sed 's/<\/H2>//g'
    else
        echo "Error: couldn't retrieve front page articles' titles" >&2
        clean_die 1
    fi
}

#################################
# new_story_alert()
#################################
function new_story_alert()
{
    if test -g `which wall`
    then
        wall slashbot: new Slashdot story: \
            \'$(head -1 /tmp/slashbot_listing2.txt)\'
    else
        wall -n slashbot: new Slashdot story: \
            \'$(head -1 /tmp/slashbot_listing2.txt)\'
    fi
}

trap 'clean_die 0' 2 3

# Check to see if another instance of slashbot is currently running: if so,
# exit abnormally
if test -f /tmp/slashdot.html
then
    echo "Error: another instance of slashbot is running" >&2
    clean_die 2
fi

# Main program loop below

while [ 1 ]
do

    echo "Getting front page" >&2
    get_front_page_titles > /tmp/slashbot_listing1.txt
    if test -a /tmp/slashbot_listing2.txt
    then
        echo "Comparing to old copy" >&2
        diff /tmp/slashbot_listing[12].txt > /dev/null
        if [ "$?" == 1 ]
        then
            new_story_alert
        fi
    else
        echo "Notice: no old copy to compare" >&2
    fi

    echo -n "Info: most recent story is '" >&2
    echo $(head -1 /tmp/slashbot_listing1.txt)\' >&2

    mv /tmp/slashbot_listing1.txt /tmp/slashbot_listing2.txt

    echo "Info: sleeping for $NAPTIME seconds" >&2
    sleep $NAPTIME
    echo "Info: done waiting" >&2

done
This discussion has been archived. No new comments can be posted.

Slashbot alpha 2

Comments Filter:

If you want to put yourself on the map, publish your own map.

Working...