#!/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} = <<EOQ; create table bittorrent ( id integer primary key asc, mainSite varchar(50) not null, torrentSite varchar(50) not null, torrentFile varchar(50) not null, torrentPattern varchar(50) not null, code varchar(500), unique( mainSite ) ) EOQ push @tables, "bittorrent"; $queries{bittorrentCheckSums} = <<EOQ; create table bittorrentCheckSums ( id integer primary key asc, checkSum varchar(100) not null, unique( checkSum ) ) EOQ push @tables, "bittorrentCheckSums"; $queries{checkTableExistence} = $dbh->prepare(<<EOQ); select name from sqlite_master where name = ? EOQ { for my $table (@tables) { my $q = $queries{checkTableExistence}; $q->execute($table); my $rec = $q->fetchrow_hashref(); if (! $rec->{name}) { $dbh->do($queries{$table}); } } } $queries{insertBittorrent} = $dbh->prepare(<<EOQ); insert into bittorrent ( mainSite, torrentSite, torrentFile, torrentPattern, code ) values (?, ?, ?, ?, ?) EOQ $queries{insertBittorrentCheckSum} = $dbh->prepare(<<EOQ); insert into bittorrentCheckSums( checkSum ) values ( ? ) EOQ $queries{selectBittorrent} = $dbh->prepare(<<EOQ); select * from bittorrent EOQ $queries{selectBittorrentCheckSum} = $dbh->prepare(<<EOQ); select 1 as bool from bittorrentCheckSums where checkSum = ? EOQ { my $q = $queries{insertBittorrent}; $q->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 = <FILE>) { 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 = <FILE>) { chomp $line; if ($line =~ /$torrentPattern/) { eval $code or die $!; } } my @torrentFiles; open CMD, "ls *torrent |"; while (my $line = <CMD>) { chomp $line; push @torrentFiles, $line; } close CMD; MSTR_LOOP: for my $torrent (@torrentFiles) { print "Checking to see if we already have this release: $torre +nt\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 = <CMD>) { 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; } } } }
In reply to evaling code stored in database by mhearse
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |