Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Wednesday, November 3, 2010

~/bin: apod-set-bg

Astronomy Picture Of the Day shows a new, usually desktop sized, picture every day. apod-set-bg is a shell script to download the daily image and set it as the desktop background. It also downloads the explanation as a separate html file.

Requirements: curl, tidy, xmlstarlet, feh.

#!/bin/sh

IMG_DIR="$1"

APOD_URL=http://apod.nasa.gov/apod/ 

URL_CONTENT=`curl $APOD_URL | tidy -q -asxml`

IMG_URL=$APOD_URL`echo "$URL_CONTENT" | xml sel -N html='http://www.w3.org/1999/xhtml' -t -v '//html:img/ancestor::html:a/attribute::href' -`

EXPLANATION=`echo "$URL_CONTENT" | xml sel -N html='http://www.w3.org/1999/xhtml' -t -c '/html:html/html:body/html:center[2]' -c '/html:html/html:body/html:p' -`

FILE_NAME=`basename "$IMG_URL"`

FILE_PATH="$IMG_DIR/$FILE_NAME"

curl -o "$FILE_PATH" "$IMG_URL" || exit 1

echo "$EXPLANATION" > "$FILE_PATH.html"

ln -sf "$FILE_PATH" "$IMG_DIR/latest.jpg"
ln -sf "$FILE_PATH.html" "$IMG_DIR/latest.html"

feh --bg-fill "$FILE_PATH"

Put the following in your crontab to run daily.

@daily ID=apod DISPLAY=:0.0 ~/bin/apod-set-bg ~/bg/ >/dev/null 2>&1

Thursday, September 16, 2010

Saving TVNZ “ondemand” episodes

The code in my previous post will work for episodes from TVNZ’s ondemand service; but there is a more direct approach.

Each episode page stores a URL to the episode’s playlist in a javascript property "playlist". The URL points to an XML file containing the URLs of four or more video parts and no ads. The following script downloads the episode page, grabs the playlist URL, downloads the playlist, and parses it. It returns a newline separated list of video URLs.

You will need to have curl and xmlstarlet installed.

#!/bin/sh

if [ "$1" == "" ]; then
    echo "usage: $0 episode-url"
    exit 1
fi

EPISODE="$1"

PLAYLIST=http://tvnz.co.nz`curl "$EPISODE" | grep -Po "(?<=playlist: ').*?(?=')"`

curl "$PLAYLIST" | \
    xml sel -N smil=http://www.w3.org/ns/SMIL \
        -t -m "//smil:video[@systemBitrate='700000']" -v '@src' -n -

I have saved it as tvnz-grab and call it like so:

tvnz-grab <episode-page-url> | xargs curl -O

The four or five parts can be played as a single file using mplayer’s "-fixed-vo" option.

Saving flash video on linux

On linux, the flash-player caches video in files under /tmp. Each video gets a file called /tmp/Flash[random-string]. These are easy enough to find and rename to [meaningful-string].flv. However, some players delete these files on completion, which is a pain given they are the files containing the video we want to save.

Hard linking these files solves the problem. The Flashxxx files will be deleted by the player, but their contents will sill be available at whatever location we made the hard link.

Some players split their content into multiple files, so we want this hard linking process to be automatic.

I have the following script saved as lnflv in my ~/bin.

#!/bin/sh

cd /tmp
for x in Flash*; do
    count=`find . -xdev -samefile "$x" | wc -l`
    if [ $count -gt 1 ]; then
        echo "Not linking $x. Already linked."
    else
        echo "Linking $x."
        ln "$x" "dl-$x.flv"
    fi
done

Run it every thirty seconds like this: watch -n30 lnflv. Each flash file not already hard linked will be linked to /tmp/dl-Flashxxx.flv.

Order by time and rename video parts with something like this:

alpha=a
for f in `find . -size +10M -name dl-Flash\* | xargs ls -tr`; do
    cp "$f" [name]$alpha.flv
    alpha=`echo -n $alpha | tr 'a-y' 'b-z'`
done

Change [name] to the name of whatever you are watching. "-size +10M" filters out files smaller than 10MB; this screens out short ads and is only required for video streams that embed them.

Saturday, July 11, 2009

~/bin: scroff

I play movies using mplayer, and it's somewhat annoying when my screen goes to sleep after half an hour. scroff runs a command with gnome-screensaver or DPMS disabled (depending on which is available).

$ scroff mplayer big_buck_bunny_720p_stereo.ogg

And the script:

#!/bin/sh

if [[ -x `which gnome-screensaver-command 2>/dev/null` ]]
then
    gnome-screensaver-command --inhibit &
    pid=$!
    "$@"
    kill $pid
elif [[ -x `which xset` ]] \
    && [[ `xset q | grep "DPMS is Enabled"` ]]
then
    xset -dpms
    "$@"
    xset +dpms
else
    "$@"
fi

Since I use mplayer all the time I've saved the following as ~/bin/mplayer:

  #!/bin/sh

  scroff /usr/bin/mplayer "$@"

~/bin: until-same

Imagine you've started downloading a large file from a server which doesn't support resuming. It's taking ages and you want to go to bed, but you also don't want the computer to stay on unnecessarily after completion of the download. What do you do?

You use until-same: a simple shell command that repeats a command until it's output is the same twice in a row.

$ until-same -h
usage: until-same [ -v ] <interval> <command>

$ until-same 1m ls -l large.iso ; hibernate

<interval> uses the same syntax as the sleep command (not the shell builtin). -v simply prints the output of each command execution to stdout.

And here's the script:

#!/bin/sh

verbose=0
if [[ "$1" == "-v" ]]; then
    verbose=1
    shift
fi

if [[ "$1" == "-h" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then
    echo "usage: until-same [ -v ] <interval> <command>"
    exit 1
fi

interval="$1"
shift
curr=`$@`
prev="$curr NOTTHESAME"

while [[ "$curr" != "$prev" ]]; do
    env sleep "$interval"
    prev="$curr"
    curr=`$@`
    [[ $verbose == 1 ]] && echo "$curr"
done