#!/bin/csh

# New versions and docs at http://bachlab.balbach.net/nb
# Since LibraryThing changes its HTML formating occasionally, the script 
#  will break and you'll need a new version if it stops working 
#  (ie. continual "Bad HTML" errors)

# "nb" -- ver.1.7 (March 2011)

# CUSTOMIZE THIS PART

set NB_PATH="/home/notify/nb/"   # The full path where nb is located
set NB_MAIL="/usr/bin/mailx"     # Email program for sending email
set NB_EMAIL="stephen@localhost" # Email address for alerts 

set WGET="/usr/bin/wget"      # /usr/bin - change path as needed
set GREP="/usr/bin/grep"
set AWK="/usr/bin/awk"
set UNIQ="/usr/bin/uniq"
set DIFF="/usr/bin/diff"
set WC="/usr/bin/wc"
set SORT="/bin/sort -dis"     # /bin  - change path as needed
set TOUCH="/bin/touch"
set CAT="/bin/cat"
set SED="/bin/sed"

# Email warnings/errors (1=on, 0=off)

set email_errors   = 1    # Recommend always on
set email_warnings = 1    # Recommend on at first, turn off later. If getting
                          # too many warnings, change time of day script runs

# END CUSTOMIZE

setenv LANG en_US.UTF-8 
setenv LC_COLLATE en_US.UTF-8

#Is there no argument?
if ($#argv != 1) then
  echo ""
  echo "Usage: /home/user/nb/nb /home/user/nb/nb.cfg"
  echo ""
  exit(1)
endif

#Does the cfg file exist?
if (! -f $argv[1]) then
  echo ""
  echo "Usage: /home/user/nb/nb /home/user/nb/nb.cfg"
  echo ""
  exit(1)
endif

set emailbody = $NB_PATH"emailbody.txt"
echo "LibraryThing New Books" > $emailbody
echo "" >> $emailbody

set newworksflag = 0

foreach authorname (`$CAT $argv[1]`)

  set baseurl = `echo -n "http://www.librarything.com/author/" $authorname | $AWK '{printf("%s%s",$1,$2)}'`
  set fullurl = `echo -n "http://www.librarything.com/author/" $authorname "&all=books" | $AWK '{printf("%s%s%s",$1,$2,$3)}'`
  set newfilename = $NB_PATH`echo -n $authorname ".new" | $AWK '{printf("%s%s",$1,$2)}'`
  set oldfilename = $NB_PATH`echo -n $authorname ".old" | $AWK '{printf("%s%s",$1,$2)}'`
  set workfilename = $NB_PATH`echo -n $authorname ".htm" | $AWK '{printf("%s%s",$1,$2)}'`

# Get the HTML data and check for any error conditions

  $WGET --header="Cookie: LTUnifiedCookie=%7B%22areyouhuman%22%3A1%7D" -q -O- $fullurl > $workfilename # get the source HTML from LT
  set authornamefull = `$SED -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q' $workfilename | $AWK -F"|" '{print $1}'`
  set numberofworks = `$GREP donthave $workfilename | $SED 's|</li>|\n|g' | $GREP "/work/" | $WC -l`
  set highload = `$GREP -ic "highload" $workfilename` # see if LT is under a "highload" condition
  set tempdown = `$GREP -ic "Temporarily down" $workfilename` # see if LT is temporarily down"
  set downtime = `$GREP -ic "downtime" $workfilename` # see if LT is temporarily down"
  set failconn = `$GREP -ic "Failure to connect" $workfilename` # see if LT db is not responding

  if ($numberofworks > 0 && $highload == 0 && $tempdown == 0 && $downtime == 0 && $failconn == 0) then # no error conditions

    if(-f $newfilename) then  # author already being tracked
      mv $newfilename $oldfilename
    else                      # author is new
      $TOUCH $oldfilename
    endif

# Create a sorted list of all works by author

  $GREP donthave $workfilename | $SED 's|</li>|\n|g' | $GREP "/work/" | $AWK -F"title=" '{printf "%s\n",$2}' | $AWK -F'[\"]' '{printf "%s\n",$2}' | $GREP '[^$]' | $SORT | $UNIQ | $SORT > $newfilename

#    $GREP "/work/" $workfilename | $GREP "sectionContent" | $SED 's|<li>|\n|g' | $GREP "title=" | $AWK -F"title=" '{printf "%s\n",$2}' | $AWK -F'[\"]' '{printf "%s\n",$2}' | $GREP '[^$]' | $SORT | $UNIQ | $SORT > $newfilename
#    $GREP "/work/" $workfilename | $AWK -F"title=" '{printf "%s\n",$2}' | $AWK -F'[\"]' '{printf "%s\n",$2}' | $GREP '[^$]' | $SORT | $UNIQ | $SORT > $newfilename

# See how many new works have been added since last check

    set numberofchanges = `$DIFF -bB $oldfilename $newfilename | $GREP ">" | $AWK -F">" '{print $2}' | $WC -l`

    if ($numberofchanges > 0) then  # format the email
      set newworksflag = 1
      echo "" >> $emailbody
      echo `echo -n $numberofchanges "new work(s) for " $authornamefull \($authorname\) " "` >> $emailbody
      echo "" >> $emailbody  
      $DIFF -bB $oldfilename $newfilename | $GREP ">" | $AWK  -F">" '{printf ("%s\n",$2) }' | $SED 's/^[ \t]*//;s/[ \t]*$//' | $GREP '[^$]' | $AWK '{printf (" * %s\n",$0)}' >> $emailbody
      echo "" >> $emailbody  
      echo $baseurl >> $emailbody
      echo "" >> $emailbody  
      echo "" >> $emailbody  
    endif

  else # HTML data is bad, or "highload" or "tempdown" or "failconn" condition
    if ($numberofworks == 0 && $highload == 0 && $tempdown == 0 && $downtime == 0 && $failconn == 0 && $email_errors == 1) then # HTML is bad
      set newworksflag = 1
      echo "" >> $emailbody
      echo "ERROR: Bad/unknown HTML ($workfilename) retrieved from $fullurl" >> $emailbody
    else if ($highload > 0 && $email_warnings == 1) then  # highload 
      set newworksflag = 1
      echo "" >> $emailbody
      echo "WARNING: LT reports High Load for $authorname - skipping for now $fullurl " >> $emailbody
    else if ($tempdown > 0 && $email_warnings == 1) then # tempdown
      set newworksflag = 1
      echo "" >> $emailbody
      echo "WARNING: LT reports Temp Down for $authorname - skipping for now $fullurl " >> $emailbody
    else if ($downtime > 0 && $email_warnings == 1) then # downtime
      set newworksflag = 1
      echo "" >> $emailbody
      echo "WARNING: LT reports Down Time for $authorname - skipping for now $fullurl " >> $emailbody
    else if ($failconn > 0 && $email_warnings == 1) then # failconn
      set newworksflag = 1
      echo "" >> $emailbody
      echo "WARNING: LT reports Failure to connect for $authorname - skipping for now $fullurl " >> $emailbody
    endif
  endif
end

# Email the results

if ($newworksflag == 1) then
  $NB_MAIL -s "New Books at LibraryThing" -a $emailbody $NB_EMAIL < /dev/null

endif
