Annotation of openbsd/OpenBSDTorrents/CurrentTorrents.pl, Revision 1.14
1.1 andrew 1: #!/usr/bin/perl -T
1.14 ! andrew 2: #$Id: CurrentTorrents.pl,v 1.13 2005/05/02 21:43:53 andrew Exp andrew $
1.1 andrew 3: use strict;
4: use warnings;
5: use diagnostics;
6:
1.2 andrew 7: use Time::Local;
1.1 andrew 8:
1.9 andrew 9: use lib 'lib';
1.1 andrew 10: use OpenBSDTorrents;
1.13 andrew 11: use BT::OBTMetaInfo;
1.1 andrew 12:
13: %ENV = ();
14:
1.13 andrew 15: use YAML;
1.6 andrew 16:
17: justme();
1.1 andrew 18:
1.12 andrew 19: my $Name_Filter = shift || '';
20: if ($Name_Filter =~ /^(\w*)$/) {
21: $Name_Filter = $1;
22: } else {
23: die "Invalid filter: $Name_Filter";
24: }
25:
1.2 andrew 26: my %files;
1.13 andrew 27: foreach my $DIR ($OBT->{DIR_NEW_TORRENT}, $OBT->{DIR_TORRENT}) {
28: opendir DIR, $DIR
29: or die "Couldn't opendir $DIR: $!";
30: foreach (readdir DIR) {
31: next unless my ($ext) = /\.(torrent|$OBT->{META_EXT})$/;
1.2 andrew 32:
1.13 andrew 33: if (/^([^\/]+)$/) {
34: $_ = $1;
35: } else {
36: die "Invalid character in $_: $!";
37: }
38: my ($name, $year, $mon, $mday, $hour, $min) =
39: /^(.*)-(\d{4})-(\d{2})-(\d{2})-(\d{2})(\d{2})/;
1.2 andrew 40:
1.13 andrew 41: $mon--;
42: my $epoch = timegm(0,$min,$hour,$mday,$mon,$year);
1.2 andrew 43:
1.13 andrew 44: #print "Adding $_\n";
1.2 andrew 45:
1.13 andrew 46: $files{$ext}{$name}{$epoch} = {
47: file => $_,
48: dir => $DIR,
49: path => "$DIR/$_",
50: ext => $ext,
51: year => $year,
52: mon => $mon,
53: mday => $mday,
54: hour => $hour,
55: min => $min,
56: name => $name,
57: epoch => $epoch,
58: };
59:
60: }
61: closedir DIR;
1.2 andrew 62: }
63:
64: my %keep;
65: my @delete;
1.13 andrew 66: foreach my $name (keys %{ $files{torrent} }) {
1.12 andrew 67: next unless $name =~ /^$Name_Filter/;
68: print "Checking $name\n";
1.4 andrew 69:
1.13 andrew 70: foreach my $epoch ( sort { $b <=> $a } keys %{ $files{torrent}{$name} } ) {
1.4 andrew 71: #print "\t$epoch\n";
1.13 andrew 72: my $torrent = $files{torrent}{$name}{$epoch}{path};
1.4 andrew 73:
1.14 ! andrew 74: if (
! 75: keys %{ $files{torrent}{$name} } == 1 &&
! 76: $files{torrent}{$name}{$epoch}{dir}
! 77: eq $OBT->{DIR_TORRENT}
! 78: ) {
! 79: #print "Skipping torrent for $name there is only one.\n";
! 80: next;
! 81: }
! 82:
! 83:
1.4 andrew 84: my $t;
1.13 andrew 85: eval { $t = BT::OBTMetaInfo->new( $torrent ); };
86:
1.4 andrew 87: if ($@) {
88: warn "Error reading torrent $torrent\n";
89: next;
90: }
91:
1.13 andrew 92: $files{torrent}{$name}{$epoch}{comment} = $t->{comment};
1.4 andrew 93: my ($path) = $t->{comment} =~ /Files from ([^\n]+)\n/s;
94:
1.10 andrew 95: unless (-d $OBT->{DIR_FTP} . "/$path") {
1.13 andrew 96: #print "Deleting $files{torrent}{$name}{$epoch}{file} the path doesn't exist.\n";
97: push @delete, $files{torrent}{$name}{$epoch};
98: delete $files{torrent}{$name}{$epoch};
1.4 andrew 99: next;
100: }
101:
1.13 andrew 102: my $hash = $t->info_hash_cached($torrent);
1.4 andrew 103: $hash = unpack("H*", $hash);
104:
1.13 andrew 105: $files{torrent}{$name}{$epoch}{info_hash} = $hash;
1.4 andrew 106:
107: undef $t;
108:
1.2 andrew 109: if (exists $keep{$name}) {
110: if (exists $keep{$name}{$hash}) {
111: push @delete, $keep{$name}{$hash};
1.13 andrew 112: delete $files{torrent}{
113: $keep{$name}{$hash}{name}
114: }{
115: $keep{$name}{$hash}{epoch}
116: };
1.2 andrew 117: $keep{$name}{$hash} =
1.13 andrew 118: $files{torrent}{$name}{$epoch};
1.2 andrew 119: } else {
1.13 andrew 120: push @delete, $files{torrent}{$name}{$epoch};
121: delete $files{torrent}{$name}{$epoch};
1.2 andrew 122: }
123: } else {
124: $keep{$name}{$hash} =
1.13 andrew 125: $files{torrent}{$name}{$epoch};
1.2 andrew 126:
127: }
128: }
129: }
130:
131: #print Dump \%files, \%keep, \@delete;
132:
133: foreach (@delete) {
1.13 andrew 134: print "Deleting '$_->{path}'\n";
135: unlink $_->{path} or die "Couldn't unlink $_->{path}";
136: }
137:
138: foreach my $name (keys %{ $files{$OBT->{META_EXT} } }) {
139: foreach my $epoch (keys %{ $files{ $OBT->{META_EXT} }{$name} }) {
140: unless ( exists $files{torrent}{$name}{$epoch} ) {
141: my $path = $files{$OBT->{META_EXT}}{$name}{$epoch}{path};
142: print "Unlinking '$path'\n";
143: unlink $path or die "couldn't unlink '$path': $!";
144: }
145: }
146: }
147:
148:
149: #print Dump \%keep;
150: foreach my $name (keys %keep) {
151: foreach my $hash (keys %{ $keep{$name} }) {
152: my $file = $keep{$name}{$hash}{file};
153: my $dir = $keep{$name}{$hash}{dir };
154: if ($dir eq $OBT->{DIR_NEW_TORRENT}) {
155: print "Moving $file to current torrents\n";
156: rename("$dir/$file", $OBT->{DIR_TORRENT} . "/" . $file)
157: or die "Couldn't rename '$file': $!";
158:
159: my $name = $keep{$name}{$hash}{name};
160: my $epoch = $keep{$name}{$hash}{epoch};
161:
162: if (exists $files{txt}{$name}{$epoch}) {
163: my $m_file = $files{txt}{$name}{$epoch}{file};
164: my $m_dir = $files{txt}{$name}{$epoch}{dir };
165: rename(
166: "$m_dir/$m_file",
167: $OBT->{DIR_TORRENT} . "/" . $m_file
168: ) or die "Couldn't rename '$m_file': $!";
169: }
170: }
171: }
1.2 andrew 172: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>