Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

can it be perl teim?

Name: Anonymous 2010-04-15 22:18

Hey, /prog/. Here's a script to autosort your anime into folders. You can thank me later.

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy;

my @files = glob "*.mkv *.avi *.mp4";

foreach (@files) {
    my $file = $_;
    $_ =~ s/\ /_/g;

    print "$file\n";

    my @data = ($_ =~ m/^[([^[\]]+)\]([^[\]]+).+$/);
    next unless($data[1]);

    my $anime = ($data[1] =~ m/^((.*?)\d{2,3})/)[1];
   
    unless($anime) {
        $anime = ($data[1] =~ m/^(.+)$/)[0];
    }

    $anime =~ s/\./_/g;

    if ($anime =~ m/\-_$/) {
        $anime =~ s/\-_//g;
        $anime =~ s/_/\ /g;
        $anime =~ s/^\s+//;
        $anime =~ s/\s+$//;
    } else {
        $anime =~ s/_/\ /g;
        $anime =~ s/^\s+//;
        $anime =~ s/\s+$//;
    }

    unless (-d $anime) {
        next unless(mkdir $anime);
    }

    move("$file", "$anime/$file");
}

exit 0;

Name: Anonymous 2010-04-15 22:29


$_ =~ s/\ /_/g;

    print "$file\n";

    my @data = ($_ =~ m/^[([^[\]]+)\]([^[\]]+).+$/);
    next unless($data[1]);

    my $anime = ($data[1] =~ m/^((.*?)\d{2,3})/)[1];
  
    unless($anime) {
        $anime = ($data[1] =~ m/^(.+)$/)[0];
    }

    $anime =~ s/\./_/g;

    if ($anime =~ m/\-_$/) {
        $anime =~ s/\-_//g;
        $anime =~ s/_/\ /g;
        $anime =~ s/^\s+//;
        $anime =~ s/\s+$//;
    } else {
        $anime =~ s/_/\ /g;
        $anime =~ s/^\s+//;
        $anime =~ s/\s+$//;


I really hope that this is invalid code

Name: Anonymous 2010-04-15 22:32

What's wrong? Regex too much for you?

Name: Anonymous 2010-04-15 22:42

>>2
Here, I cleaned it up for you.

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy;

my @files = glob "*.mkv *.avi *.mp4";

foreach (@files) {
    my $file = $_;
    $_ =~ s/\ /_/g;

    print "$file\n";

    my @data = ($_ =~ m/^[([^[\]]+)\]([^[\]]+).+$/);
    next unless($data[1]);

    my $anime = ($data[1] =~ m/^((.*?)\d{2,3})/)[1];
   
    unless($anime) {
        $anime = ($data[1] =~ m/^(.+)$/)[0];
    }

    $anime =~ s/\./_/g;

    if ($anime =~ m/\-_$/) {
        $anime =~ s/\-_//g;
    }
       
    $anime =~ s/_/\ /g;
    $anime =~ s/^\s+//;
    $anime =~ s/\s+$//;

    unless (-d $anime) {
        next unless(mkdir $anime);
    }

    move("$file", "$anime/$file");
}

exit 0;

Name: Anonymous 2010-04-15 22:57

>>4
You're fired.

Name: Anonymous 2010-04-15 22:58

>>5
l2perlnigga

Name: Anonymous 2010-04-15 23:16

Perl Regex always makes my head hurt a bit; but, I can't complain too much since it does get the job done.

Name: Anonymous 2010-04-15 23:19

Sorted anime? My wallet!

Name: Anonymous 2010-04-15 23:23

It's not a valid regex.

What the hell is this?

m/^[([^[\]]+)\]([^[\]]+).+$/

Name: Anonymous 2010-04-15 23:32

>>9
Looks like 4chan doesn't like some of the copy/pasting...

Some characters were removed from regex.

Name: Anonymous 2010-04-15 23:48

>>9
m/^[([^[\]]+)\]([^[\]]+).+$/
I see what you mean.  Character classes:
m/.[.[.[..].....[.[..]...../
By delimiting three "]," there is an imbalance.  Using "\\" to balance the character classes properly and preserve the slash ...

m/^[([^[\\]]+)\\]([^[\\]]+).+$/

... still, this looks like nonsense. "^[([^[\\]]+)\\]" basically says "the beginning of the line matches to the beginning of the line matches to a forward slash one or more times and once more?" am I reading that correctly, what?

Name: Anonymous 2010-04-16 0:17

http://pastebin.com/9BYDK5v7

that's what it's SUPPOSE to be...

Name: Anonymous 2010-04-16 0:24

>>12
Man that is damn ugly. Two problems, etc.

This probably won't work, but:

Name: >>13 2010-04-16 0:27

Yeah, it didn't work.

Name: Anonymous 2010-04-16 0:28

>>14
WORKS FOR ME

Name: Anonymous 2010-04-16 0:29

>>13
Write once, read never. That's what perl is about.

Name: Anonymous 2010-04-16 0:30

Honestly, when you are doing nothing but regex, there's really not much you can do to make it not ugly.

Name: Anonymous 2010-04-16 0:54

>>15
I happen to know you didn't even try.

>>16
Same goes for the rest of my comment. :/

>>17
There's something you can do. OP's code could have broken things down more clearly with roughly the same reliance on regex. It's pointless though, and far better to use only about half as much regex (and still reduce the amount of supporting code needed.)

Name: Anonymous 2010-04-16 1:06

>>18
If you can write something cleaner that can match all of these examples, I'll give you an ecookie:

[SFW]_11eyes_-_06_[490871D2].mkv
[Ayako] A Certain Scientific Railgun - 10 [HQ][H264][CAFB8EE3].mkv
[Eclipse]_Claymore_-_22_(XviD)_[14E4C7B3].avi
[AonE_-_Conclave]_Devil_May_Cry_-_10_[9A4B4DAB].avi
[Ecliptic-BSS] Durarara!! - 12 [191E1603].mkv
[DB-gg]_Eden_of_the_East_the_Movie_I_King_of_Eden_[0747D19A].avi
[SRN]iDOLM@STER_XENOGLOSSIA_07_[53B65992].avi
[DB]_Bleach_264_[012073FE].avi
[A-E_&_Saizen]_Shigurui_01_[270652D1].mkv

There are several different test cases where it makes it difficult to simplify.

Name: Anonymous 2010-04-16 1:10

11eyes
That anime made my eyes burn...

Name: Anonymous 2010-04-16 1:17

>>19
I'm going ZzzzzzZzz now, but I very well might do that tomorrow.

Name: Anonymous 2010-04-16 1:25

>>19
.avi

Name: Anonymous 2010-04-16 1:30

>>22
Have you not seen the four .mkv's in there?

Name: Anonymous 2010-04-16 1:43

Stuff like .+$ in regexps is silly. Your script fails on files that don't end in [CHECKSUM] as well.

Name: Anonymous 2010-04-16 1:54

>>24
No, it doesn't. It works file even if [CHECKSUM] isn't there.

Name: Anonymous 2010-04-16 1:57

>>24
Yeah, I changed it to .+
Dunno why I had it like that earlier... /shrug

Name: Anonymous 2010-04-16 1:58

Stuff like .+$ in regexps is silly. Your script fails on files that don't end in [CHECKSUM] as well.

Here's how I would have written it:

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

for(glob "*.mkv *.avi *.mp4"){
    my $file = $_;
    s/ /_/g;

    my($release)=/^ [ .*? \] ( [^[]+ ) /x or next;
    my($title)=($release=~/^ (.*) \d{2,3} /x,$release);

    $title=~s/[._]/ /g;
    $title=~s/- $//;
    $title=~s/ *$//;
    $title=~s/^ *//;

    print "$file\n";
    mkdir $title and move $file,"$title/$file";
}

Name: Anonymous 2010-04-16 2:00

Name: Anonymous 2010-04-16 2:04

>>25
Okay, on stuff that doesn't have episode number in it and checksum; Eden of the East the Movie I King of Eden av. Ouch.

Name: Anonymous 2010-04-16 2:07

>>26
And it's still incorrect. You should change it to nothing.

Name: Anonymous 2010-04-16 2:08

>>29
Yeah, there's a slight problem with it. Like I said, lots of test cases makes it a pain.

>>27
That will fail if directory already exists.

Name: Anonymous 2010-04-16 2:12

>>28
Your's fails on Bleach line. Didn't try the others.

Name: Anonymous 2010-04-16 2:16

Name: Anonymous 2010-04-16 2:25

Original fails on 11eyes though because (.*?) catches empty string;  Here's the variant that will work for all cases:

my($title)=($release=~/^ (.*\D) \d+ /x,$release);

Name: Anonymous 2010-04-16 8:36

>>27
Finally, someone who understands the purpose of /x, and who also understands the value of monospaced code.

You're hired.

Name: Anonymous 2010-04-16 9:22

# rm -f *.mkv *.mp4 *.avi *.rmvb *.ogm

Name: Anonymous 2010-04-16 9:24

recursive removal of my animu? No thanks!

Name: Anonymous 2010-04-16 10:52

>>35
Personally I think all those spaces in regex looks like ass.

AND NO WHITESPACE BETWEEN ASSIGNMENTS? wtf

Name: Anonymous 2010-04-16 11:38

>>35
/x is just the verbose flag, right? Kind of pointless in regexes as short as those.

Name: Anonymous 2010-04-16 12:20

$anime
My son is a big naruto fan.

Name: Anonymous 2010-04-16 12:57

>>39
You're fired.

Name: Anonymous 2010-04-16 19:20

>>39

/x ignores unescaped spaces

Name: Anonymous 2011-02-03 4:28

Name: Anonymous 2011-02-17 19:21

check 'em
Don't change these.
Name: Email:
Entire Thread Thread List