I have a perl script to delete all Gallery1 comments!

mrgushi

Joined: 2004-11-28
Posts: 3
Posted: Tue, 2008-01-15 20:42

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;
 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2008-01-15 20:47

Thanks!

Jens
--
Last Gallery v1 Developer.
Tryout the TEST-Version of Gallery 1.6

 
squish0

Joined: 2008-03-02
Posts: 1
Posted: Sun, 2008-03-02 17:49

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";
 
annorax
annorax's picture

Joined: 2002-07-24
Posts: 6
Posted: Thu, 2008-08-07 07:16

Squish0's version worked as advertised for the deep infestation of comment spam on one of my albums.

Thanks!

Jeff G.

 
vandy

Joined: 2009-02-03
Posts: 3
Posted: Tue, 2009-02-03 07:13

Thanks for the script.I have been wondering for this perl script.
Thank you.