Hello all,
After dealing with a bunch of comment spam and deciding that I no longer wanted to deal with ANY comments, a friend of mine came up with this quick perl script (it requires a special module, that you can download)
You run it on your albums.dat file: rmcomments <albums.dat >albums.new.dat
#!/usr/bin/perl -w
# tristan+perl@ethereal.net 3jan2008
use strict;
use PHP::Serialization qw(serialize unserialize);
local $/;
local $_ = <>;
my $a = unserialize $_;
for (@$a) {
delete $_->{comments};
}
#use Data::Dumper;
#print Dumper $a;
print serialize $a;
Posts: 6818
Thanks!
Jens
--
Last Gallery v1 Developer.
Tryout the TEST-Version of Gallery 1.6
Posts: 1
This was a great email and has inspired me to extend the program so that it asks you what you want to do for each comment rather than delete everything. I didn't want to risk deleting real comments.
Syntax: rmcomments --in <filename> --out <filename>
Two more options:
--default yes This will switch the default if you press return at the prompt to delete the comment
--no-autobad This will turn off the automatic deletion of comments with bad words in them
The regexp in the code should be modified to suit your definition of bad words if you use the autobad feature.
James
#!/usr/bin/perl -w # tristan+perl@ethereal.net 3jan2008 # 2008-03-02 use strict; use PHP::Serialization qw(serialize unserialize); use Getopt::Long; sub prompt ($;$) { ## no critic my ($mess, $def) = @_; my $isa_tty = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; my $dispdef = defined $def ? "[$def] " : " "; $def = defined $def ? $def : ""; local $|=1; local $\; print "$mess $dispdef"; my $ans; if ($ENV{PERL_MM_USE_DEFAULT} || (!$isa_tty && eof STDIN)) { print "$def\n"; } else { $ans = <STDIN>; if( defined $ans ) { chomp $ans; } else { # user hit ctrl-D print "\n"; } } return (!defined $ans || $ans eq '') ? $def : $ans; } our $in = undef; our $out = undef; our $default = "no"; our $autobad = 1; GetOptions("in=s" => \$in, "out=s", \$out, "default=s", \$default, "autobad!", \$autobad); die "Syntax: $0 --in <filename> --out <filename>\n" unless defined $in and defined $out; my $inputdata = ""; open(IN, $in) or die "failed to open $in: $!\n"; my $buffer; while (sysread IN, $buffer, 4096) { $inputdata.= $buffer; } close(IN) or die "failed to close $in: $!\n"; my $a = unserialize $inputdata; foreach my $item (@$a) { if (defined $item->{comments}) { my @newcomments; foreach (@{$item->{comments}}) { print "\n--- COMMENT DATED ".localtime($_->{datePosted})." by $_->{name} ----\n"; print $_->{commentText}, "\n"; print "--- END COMMENT ---\n"; if ($autobad && $_->{commentText} =~ /viagra|tramadol|cialis|pills|sex|teen|ringtone|replica rolex|replica watches|levitra|phentermine|personals|adult toy|lingerie|vibrator|dildos|anal toy/i) { print "Automatically deleting (to turn off use --no-autobad)\n" } else { my $ans = prompt("Delete comment?", $default); if ($ans eq "y" or $ans eq "yes") { print "The above comment will be deleted.\n"; } else { push @newcomments, $_; } } } $item->{comments} = \@newcomments; } } print "All done.\n"; open(OUT, ">", $out) or die "failed to open $out: $!\n"; print OUT serialize $a; close(OUT) or die "failed to close $out: $!\n";Posts: 6
Squish0's version worked as advertised for the deep infestation of comment spam on one of my albums.
Thanks!
Jeff G.
Posts: 3
Thanks for the script.I have been wondering for this perl script.
Thank you.