Helical Alpha Code

comicget: comics.com client


#!/bin/bash
#####################
#
# name: comicget (comicget.sh)
# vers: 0.2a
# date: 071206
# first: 070706
# auth: "Keith Beckman" <kbeckm@alphahelical.com>
# site: http://alphahelical.com/code/scripts/comicget
# desc: comicget is a comics.com client for bash. Currently it only supports
# curl as a user-agent.
#
# todo: archive retrieval, wget/lynx/links/telnet support
# also, a more robust 'recent' mode that allows date selection
# archival of comics by month into compressed archives
# generate a "latest comics page" suitable for using as a browser homepage
#
# license: comicget is copyright 2006 Keith Beckman and is licensed under the
#  GNU General Public License (http://gnu.org/licenses/gpl.txt)
#
#####################

####configuration####

datadir='/tmp'
open_command='open'
open_multiple=1 # 1 if $open_command can handle a list of items, 0 otherwise

# under X11, you might have
#open_command='xv'
#open_multiple=0

#comics dwelling under comics.com/comics
comics_list="alleyoop dilbert drabble getfuzzy hedge jumpstart pearls"

#comics dwelling under comics.com/creators, from the old Creators Syndicate
creators_list="bc wizardofid"

host='http://comics.com'

silent_flag='-s' # what option(s) to pass the UA for silent downloads

seconds=43200 # seconds between updates
verbose=1 # default verbosity level, 0-2
mode='retrieval' # default mode

verstring='comicget v0.2a 071206 keith beckman'

##end configuration##

function echo_usage() {
cat <<!
Usage: comicget -[rVqfvh]
 comicget is a comics.com client
 
 -r|--recent opens comics downloaded in the past day
 -V|--verbose sets maximum verbosity
 -q|--quiet sets no output
 -f|--force forces an update before the interval has elapsed
 -v|--version echoes the version string
 -h|--help displays this help text

!
 }

while [ "$1" != '' ]; do
 case $1 in
  -r|--recent)
   mode='recent'
   ;;
  -V|--verbose)
   verbose=2
   ;;
  -q|--quiet)
   verbose=0
   ;;
  -f|--force)
   force=1
   ;;
  -v|--version)
   echo $verstring && exit
   ;;
  -h|--help|-u|--usage)
   echo_usage && exit
   ;;
  *)
   ;;
 esac
  shift
 done


if [ -f $datadir/touch ]; then
 let dif=$(date +%s)-$(stat -f %m $datadir/touch)
else
 touch $datadir/touch
 let dif=$seconds+1
fi


##MODE: RETRIEVAL##
if [[ $mode == 'retrieval' && ( $dif -gt 43200 || $force == 1 ) ]]; then

case $verbose in
 0|1)
  img_dl_flags=$silent_flag
  dat_dl_flags=$silent_flag
  ;;
 2)
  img_dl_flags=''
  dat_dl_flags=''
  ;;
esac

function grab_image () {

# grab_image takes as $1 a comic directory, i.e. "bc" for /comics/bc
# and for $2 takes the parent directory of $1

 if [ ! -d $datadir/$1 ]; then
  mkdir $datadir/$1
 fi
 
 if [ ! -f $datadir/$1/touch ]; then
  touch $datadir/$1/touch
 fi

 if [ $verbose -gt 0 ]; then
  echo "Checking $1"
 fi

 imgpath=$host/$(curl $dat_dl_flags $host/$2/$1/ |\
 grep "Today's Comic" | grep "$2/$1/archive/images" |\
 sed "s/.*<IMG SRC=\"\/\(.*\)\" ALT=.*>.*/\1/")

 # how we know if we're online
 if [ $imgpath == "$host/" ]; then return 1; fi

 extension=$(sed 's/.*\.\(...\)$/\1/' <<< $imgpath)

 if [ "$(cat $datadir/$1/touch)" != $imgpath ]; then
  if [ $verbose -gt 0 ]; then
   echo "Updating $1"
  fi
  
  if [ $verbose -gt 1 ]; then echo $imgpath; fi
  curl $img_dl_flags $imgpath > $datadir/$1/$1-$(date +%F).$extension

  echo $imgpath > $datadir/$1/touch
 fi

 }

natch=1

for comic in $comics_list; do
 grab_image $comic comics
 if [[ $? -eq 0 && $natch -eq 1 ]]; then
  natch=1; else natch=0; fi
 done

for comic in $creators_list; do
 grab_image $comic creators
 if [[ $? -eq 0 && $natch -eq 1 ]]; then
  natch=1; else natch=0; fi
 done

if [ $natch -eq 1 ]; then touch $datadir/touch; fi

##MODE: RECENT##

elif [ $mode == 'recent' ]; then
 if [ "$open_multiple" -eq 1 ]; then
  $open_command $(find $datadir -mtime 1 | grep '\....$');
 else
  for item in $(find $datadir -mtime 1 | grep '\....$'); do
   $open_command $item
   done
 fi

fi

Generated by GNU enscript 1.6.1 and enscriptclean.

comicget | Download Source | View Source