#!/usr/bin/perl # A true Ruth Goldberg contraption. use strict; use DBI; use File::Path qw(mkpath); use Digest::MD5 qw(md5_hex); ### Connect to sqlite database. Sqlite handles ### locking for us. my $dbh = DBI->connect( "dbi:SQLite:/home/bittorrent/bittorrent.db", "","", { RaiseError => 0, AutoCommit => 1 } ); my %queries; my @tables; $queries{bittorrent} = <prepare(<execute($table); my $rec = $q->fetchrow_hashref(); if (! $rec->{name}) { $dbh->do($queries{$table}); } } } $queries{insertBittorrent} = $dbh->prepare(<prepare(<prepare(<prepare(<execute( 'http://www.slackware.com', 'getslack/torrents.php', 'torrents.php', 'install-dvd',' if (\\$line =~ /href="(.*)"/) { print "Fetching torrent file: \\$mainSite/\\$1.\\n"; system qq{wget "\\$mainSite/\\$1"}; } ' ); $q->execute( 'http://www.debian.org', 'CD/torrent-cd', 'index.html', 'bt-dvd',' my \$subSite; my \@releases = split /\]/, \$line; for my \$release (\@releases) { if (\$release =~ /(i386|amd64)/) { if (\$release =~ /href="(.*)"/) { \$subSite = \$1; system qq{wget "\$1"}; open FILE, "index.html.1"; while (my \$line1 = ) { chomp \$line1; if (\$line1 =~ /torrent/) { if (\$line1 =~ /href="(.*)"/) { system qq{wget "\$subSite/\$1"}; } } } } } } ' ); } my $q = $queries{selectBittorrent}; $q->execute(); while (my $rec = $q->fetchrow_hashref() ) { my $mainSite = $rec->{mainSite}; my $torrentSite = $rec->{torrentSite}; my $torrentFile = $rec->{torrentFile}; my $torrentPattern = $rec->{torrentPattern}; my $code = $rec->{code}; my $homeDir = "/home/bittorrent"; my $tmpDir = "tmp.$$"; if (! -d $homeDir ) { print "Making $homeDir.\n"; mkpath($homeDir, 0, 0077); } print "Entering $homeDir.\n"; chdir $homeDir; if (! -d $tmpDir ) { print "Making $tmpDir.\n"; mkpath($tmpDir, 0, 0077); } print "Entering $tmpDir.\n"; chdir $tmpDir; print "Fetching $mainSite torrent page.\n"; system qq{wget "$mainSite/$torrentSite"}; open FILE, $torrentFile; while (my $line = ) { chomp $line; if ($line =~ /$torrentPattern/) { eval $code or die $!; } } my @torrentFiles; open CMD, "ls *torrent |"; while (my $line = ) { chomp $line; push @torrentFiles, $line; } close CMD; MSTR_LOOP: for my $torrent (@torrentFiles) { print "Checking to see if we already have this release: $torrent\n"; my $digest = md5_hex($torrent); my $q = $queries{selectBittorrentCheckSum}; $q->execute($digest); my ($bool) = $q->fetchrow_array(); if ($bool) { print "\nLooks like we already have this release, skipping: $torrent\n"; next MSTR_LOOP; } print "Looks like we don't have this release, downloading it: $torrent\n"; system qq{transmissioncli -ep --download-dir $homeDir/$tmpDir --uplimit number 0 --config-dir $homeDir/$tmpDir $torrent &}; while (1) { sleep 15; my $cntr = 1; my @results; open CMD, "transmission-remote --list |"; while (my $line = ) { chomp $line; next if ($cntr++ != 2); @results = split /\s+/, $line; } close CMD; if ($results[5] eq "Done") { print "\n\nStopping transmissioncli.\n"; system "transmission-remote -t 1 -S &"; my $q = $queries{insertBittorrentCheckSum}; $q->execute($digest); next MSTR_LOOP; } } } }