#!/bin/bash
#
# Update the program guide information for selected shows from Comedy Central.
#
# Copyright (c) 2005 by Preston Crow
# Distributed under the GNU General Public License, version 2 or later
# (If you make changes, please add your name and date as an additional
# line in the copyright notice.)
#
# All too often, there is no episode information provided by the regular
# listing services, but it is available on the comedycentral.com listing.
# This script will get that information for selected shows and update the
# guide data where the information is not available.
#

comedy_update()
{
wget --output-document=- 'http://www.comedycentral.com/tv_schedule/index.jhtml?seriesId='${SERIESID}'&forever=please' | \
	egrep 'mtvn-cal-desc|^(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)' | \
	sed -e 'sX&nbsp;X Xg' -e '/^[A-Z]/sX^[^ ]* XX' -e '/^[A-Z]/h' -e '/^[A-Z]/d' -e 'sX.*<nobr>XX' -e 'sX<.*<b>X\tX' -e G -e 'sX\nX\tX' -e 'sX<[^>]*>XXg' -e 'sX:X X' | \
	awk -F \	 '{print $3, $1, $2}' | \
	sed	-e s/^Jan/01/ -e s/^Feb/02/ -e s/^Mar/03/ -e s/^Apr/04/ -e s/^May/05/ -e s/^Jun/06/ \
		-e s/^Jul/07/ -e s/^Aug/08/ -e s/^Sep/09/ -e s/^Oct/10/ -e s/^Nov/11/ -e s/^Dec/12/ | \
	egrep '\([^ ]*\)$' | \
	sed -e 'sX(TBDN)X(999999)X' -e 'sX([^ ]*)$X\t&X' -e 'sX\t(X X' -e 'sX)$XX' | \
	awk '{if ( $6 == "AM" ) { printf("%s-%s-%s %s:%s",$3,$1,$2,$4%12,$5)} else {printf("%s-%s-%s %d:%s",$3,$1,$2,12+($4%12),$5)}} {printf("\t%s",$7)} {for(A=8;A<NF;++A) printf(" %s",$A)} {printf("\t%s\n",$NF)}' | \
	awk -F \	 '{printf("UPDATE `program` SET subtitle=%c%s%c,programid=%cEP%010d%c,syndicatedepisodenumber=%c%s%c WHERE title='"'${TITLE}'"' and starttime=%c%s%c and programid like %cSH%%%c;\n",34,$2,34,34,$3,34,34,$3,34,34,$1,34,34,34)}' | \
	mysql --database=mythconverg --user=mythtv --password=mythtv --batch
#cat
}

SERIESID=11600
TITLE="South Park"
comedy_update

SERIESID=11608
TITLE="The Daily Show with Jon Stewart"
comedy_update
