Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.23
1.1 andrew 1: #!/usr/bin/perl -T
1.23 ! andrew 2: #$RedRiver: CurrentTorrents.pl,v 1.22 2006/07/24 18:03:53 andrew Exp $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.2 andrew 7: use Time::Local;
1.16 andrew 8: use Fcntl ':flock';
1.23 ! andrew 9: use File::Basename;
! 10: #use YAML;
1.1 andrew 11:
1.9 andrew 12: use lib 'lib';
1.1 andrew 13: use OpenBSDTorrents;
1.17 andrew 14: use BT::MetaInfo::Cached;
1.1 andrew 15:
16: %ENV = ();
17:
1.15 andrew 18: #justme();
1.1 andrew 19:
1.12 andrew 20: my $Name_Filter = shift || '';
21: if ($Name_Filter =~ /^(\w*)$/) {
22: $Name_Filter = $1;
23: } else {
24: die "Invalid filter: $Name_Filter";
25: }
26:
1.15 andrew 27: my %Possible_Torrents;
28: Process_Dir($OBT->{DIR_FTP});
29:
1.2 andrew 30: my %files;
1.15 andrew 31: my %keep;
32: my @delete;
1.13 andrew 33: foreach my $DIR ($OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT}) {
34: opendir DIR, $DIR
35: or die "Couldn't opendir $DIR: $!";
36: foreach (readdir DIR) {
37: next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/;
1.2 andrew 38:
1.13 andrew 39: if (/^([^\/]+)$/) {
40: $_ = $1;
41: } else {
42: die "Invalid character in $_: $!";
43: }
1.23 ! andrew 44: my $epoch = 0;
! 45: my $name = basename($_, '.torrent');
! 46:
! 47: if (my ($base, $year, $mon, $mday, $hour, $min) =
! 48: /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/) {
1.2 andrew 49:
1.23 ! andrew 50: $mon--;
! 51: $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
! 52: $name = $base;
! 53: }
1.2 andrew 54:
1.13 andrew 55: #print "Adding $_\n";
1.2 andrew 56:
1.13 andrew 57: $files{$ext}{$name}{$epoch} = {
58: file => $_,
59: dir => $DIR,
60: path => "$DIR/$_",
61: ext => $ext,
1.23 ! andrew 62: #year => $year,
! 63: #mon => $mon,
! 64: #mday => $mday,
! 65: #hour => $hour,
! 66: #min => $min,
1.13 andrew 67: name => $name,
68: epoch => $epoch,
69: };
70:
1.23 ! andrew 71: if (
! 72: $name =~ m/\A $OBT->{BASENAME} /xms &&
! 73: ! exists $Possible_Torrents{$name}
! 74: ) {
1.15 andrew 75: print "Would remove $_\n";
76: push @delete, $files{$ext}{$name}{$epoch};
77: }
1.13 andrew 78: }
79: closedir DIR;
1.2 andrew 80: }
81:
1.13 andrew 82: foreach my $name (keys %{ $files{torrent} }) {
1.12 andrew 83: next unless $name =~ /^$Name_Filter/;
1.22 andrew 84: #print "Checking $name\n";
1.4 andrew 85:
1.13 andrew 86: foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {
1.4 andrew 87: #print "\t$epoch\n";
1.13 andrew 88: my $torrent = $files{torrent}{$name}{$epoch}{path};
1.4 andrew 89:
1.14 andrew 90: if (
91: keys %{ $files{torrent}{$name} } == 1 &&
92: $files{torrent}{$name}{$epoch}{dir}
93: eq $OBT->{DIR_TORRENT}
94: ) {
95: #print "Skipping torrent for $name there is only one.\n";
96: next;
97: }
98:
1.4 andrew 99: my $t;
1.18 andrew 100: eval {
101: $t = BT::MetaInfo::Cached->new(
102: $torrent,
103: {
1.22 andrew 104: cache_root => '/tmp/OBTFileCache'
105: #$OBT->{DIR_HOME} . '/FileCache'
1.18 andrew 106: }
107: );
108: };
1.13 andrew 109:
1.4 andrew 110: if ($@) {
111: warn "Error reading torrent $torrent\n";
112: next;
113: }
114:
1.13 andrew 115: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.4 andrew 116: my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
117:
1.10 andrew 118: unless (-d $OBT->{DIR_FTP} . "/$path") {
1.13 andrew 119: #print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n";
120: push @delete, $files{torrent}{$name}{$epoch};
121: delete $files{torrent}{$name}{$epoch};
1.4 andrew 122: next;
123: }
124:
1.17 andrew 125: my $hash = $t->info_hash;
1.4 andrew 126: $hash = unpack("H*", $hash);
127:
1.16 andrew 128: undef $t;
129:
1.13 andrew 130: $files{torrent}{$name}{$epoch}{info_hash} = $hash;
1.4 andrew 131:
132:
1.2 andrew 133: if (exists $keep{$name}) {
134: if (exists $keep{$name}{$hash}) {
135: push @delete, $keep{$name}{$hash};
1.13 andrew 136: delete $files{torrent}{
137: $keep{$name}{$hash}{name}
138: }{
139: $keep{$name}{$hash}{epoch}
140: };
1.2 andrew 141: $keep{$name}{$hash} =
1.13 andrew 142: $files{torrent}{$name}{$epoch};
1.2 andrew 143: } else {
1.13 andrew 144: push @delete, $files{torrent}{$name}{$epoch};
145: delete $files{torrent}{$name}{$epoch};
1.2 andrew 146: }
147: } else {
148: $keep{$name}{$hash} =
1.13 andrew 149: $files{torrent}{$name}{$epoch};
1.2 andrew 150:
151: }
152: }
153: }
154:
155: #print Dump \%files, \%keep, \@delete;
1.23 ! andrew 156: #exit;
1.2 andrew 157:
158: foreach (@delete) {
1.13 andrew 159: print "Deleting '$_->{path}'\n";
160: unlink $_->{path} or die "Couldn't unlink $_->{path}";
161: }
162:
163: foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) {
164: foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) {
165: unless ( exists $files{torrent}{$name}{$epoch} ) {
166: my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path};
167: print "Unlinking '$path'\n";
168: unlink $path or die "couldn't unlink '$path': $!";
169: }
170: }
171: }
172:
173:
1.20 andrew 174: #print Dump \%keep;
175: foreach my $name (keys %keep) {
176: foreach my $hash (keys %{ $keep{$name} }) {
177: my $file = $keep{$name}{$hash}{file};
178: my $dir = $keep{$name}{$hash}{dir };
179: if ($dir eq $OBT->{DIR_NEW_TORRENT}) {
180: print "Moving $file to current torrents\n";
181: rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file)
182: or die "Couldn't rename '$file': $!";
183:
184: my $name = $keep{$name}{$hash}{name};
185: my $epoch = $keep{$name}{$hash}{epoch};
186:
187: if (exists $files{txt}{$name}{$epoch}) {
188: my $m_file = $files{txt}{$name}{$epoch}{file};
189: my $m_dir = $files{txt}{$name}{$epoch}{dir };
190: rename(
191: "$m_dir/$m_file",
192: $OBT->{DIR_TORRENT} . "/" . $m_file
193: ) or die "Couldn't rename '$m_file': $!";
194: }
195: }
196: }
197: }
1.15 andrew 198:
199: sub Process_Dir
200: {
201: my $basedir = shift;
202:
203: my ($dirs, $files) = Get_Files_and_Dirs($basedir);
204: if (@$files) {
205: my $dir = $basedir;
206: $dir =~ s/^$OBT->{DIR_FTP}\///;
207: my $torrent = Name_Torrent($dir);
208: $torrent =~ s/-.*$//;
209: $Possible_Torrents{$torrent} = 1;
210: }
211:
212: foreach my $subdir (@$dirs) {
213: next if $subdir eq '.';
214: next if $subdir eq '..';
215: Process_Dir("$basedir/$subdir")
216: }
217: }
218:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>