#!/usr/bin/perl -w # # Silly perl script that reads the web directory fetched from a tivo box # (after filtering through add-newlines) and looks for episodes of # Doctor Who to download. # # Will fail miserably if the TiVo starts formatting the web page # differently... # use strict; # Get PATH set to include this script's directory and other useful bits my $newpath=`dirname $0`; chomp($newpath); $newpath=`$newpath/echo-path`; chomp($newpath); $ENV{'PATH'}=$newpath; my $topdir='/huge/vids'; my $whodir="$topdir/DoctorWho"; my $dbdir="$whodir/.data"; my $dbfile="$dbdir/allinfo.txt"; my %db; # Read in the existing database to check for already downloaded episodes # we can skip (marked with nodownload=1 after I'm sure the copy I have # is good enough that I'm unlikely to do better). my $fh; my $r; if (open($fh, '<', $dbfile)) { while (<$fh>) { chomp; if (/^\[(.+)\]$/) { my $basename = $1; $r = {}; $db{$basename} = $r; } elsif (/^([A-Za-z0-9_]+)=(.+)$/) { if (defined($r)) { my $key = $1; my $val = $2; $r->{$key} = $val; } } } close($fh); undef($fh); undef($r); } else { print "Cannot read $dbfile : $!\n"; } sub check_one_episode { my $rd = shift; my $episode_name; my $record_date; my $url; my $id; if ($rd->[1]=~/alt=\"BBCAHD\"/) { if ($rd->[2]=~/Doctor Who/) { $episode_name = $rd->[2]; chomp($episode_name); $episode_name=~s/\&[a-zA-Z0-9_]+\;//g; $episode_name=~s/^\]+\>//; $episode_name=~s/\<\/td\> *$//; $episode_name=~s/^\//; $episode_name=~s/\.*$//; $episode_name=~s/\<\/b\>.*$//; $episode_name=~s/([\w']+)/\u\L$1/g; $episode_name=~s/[^A-Za-z_0-9]//g; $episode_name=~s/^DoctorWho//; $record_date = $rd->[3]; if ($record_date=~/(\d+)\/(\d+)/) { my $month=$1; my $day=$2; my @curtime = localtime(time); my $year = $curtime[5] + 1900; my $curmonth = $curtime[4] + 1; if ($curmonth < $month) { $year -= 1; } $month = sprintf("%02d",$month+0); $day = sprintf("%02d",$day+0); $record_date = "$year-$month-$day"; } $url = $rd->[5]; if ($url=~/\{'nodownload'}; } if (((! defined($nodownload)) || ($nodownload ne '1')) && (! -d "$whodir/$record_date-$id-$episode_name")) { my $tempdir="$whodir/$$"; mkdir($tempdir); if (-d $tempdir) { chdir($tempdir); my $fd; if (open($fd, '>', "info.txt")) { my $i; foreach $i (@{$rd}) { print $fd $i; } close($fd); sleep(15); system("get-tivo", $episode_name, $url); chdir($whodir); if ((! -f "$tempdir/$episode_name.mpg") || ((stat("$tempdir/$episode_name.mpg"))[7] == 0)) { system("rm -rf $tempdir"); } else { rename("$$", "$record_date-$id-$episode_name"); system("start-keeper-bg", "$record_date-$id", "$episode_name"); } } } } } } my @rowdata; while (<>) { if (/^\= 6) { &check_one_episode(\@rowdata); undef @rowdata; } } elsif (/^\= 6) { &check_one_episode(\@rowdata); }