-
Notifications
You must be signed in to change notification settings - Fork 15
/
unfoldDLSiteFile
executable file
·200 lines (163 loc) · 4.64 KB
/
unfoldDLSiteFile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#! /usr/bin/perl
use 5.014;
use autodie;
#use LWP::Simple;
use Web::Query;
use JSON;
use utf8;
use Encode;
use DBI;
use Data::Dumper;
use File::Copy;
use File::Basename;
use File::Find ();
use File::chdir;
use DJVoiceConfig;
use open ":encoding(utf8)";
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
my @suffix_list = ('.zip', '.rar', 'lzh');
my $working_dir = $DJVoiceConfig::WORKING_DIR;
our $target = $ARGV[0];
$target .= '/' if( substr($target, -1) ne '/');
#chdir $ARGV[0] or die 'chdir fail';
$CWD = $ARGV[0];
our @all_file;
# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
# Traverse desired filesystems and get @all RJ##### files;
File::Find::find({wanted => \&wanted}, $target);
# clssify file, folder, double folder
# and flating doble folder
my %file_array;
my @folder_array;
#print Dumper(@all_file);
foreach (@all_file ){
my $check = check_RJ_unify($_);
say $check, ':', $_;
if( $check ){
if( ! -d $_){
#say $check, ':file:', $_;
$file_array{ $_ } = $check;
}elsif(/\//){
#say $check, ':double folder:', $_;
&smart_move_in($_, '.');
s/.*\///g;
@folder_array = (@folder_array, $_);
}else{
#say $check, ':single folder:', $_;
@folder_array = (@folder_array, $_);
}
}
}
# move files into RJ###### named folder
# mkdir if the folder doesn't exist
while ( my($file, $folder) = each %file_array ){
if( ! -e $folder ){
mkdir "$folder", 0755 or warn "mkdir $folder fail: $!";
move "$file", "$folder";
say "mv $file $folder";
@folder_array = (@folder_array, $folder);
}else{
&smart_move_in($file, $folder);
}
}
# block empty folders
# move empty folders into 'empty'
our $is_empty_flag ;
my $empty_folder;
my @empty_index_array;
foreach my $i (0..$#folder_array) {
$is_empty_flag = 1;
#say '-----' . $i, ':', $folder_array[$i];
File::Find::find(
{wanted => \&is_empty_folder}, $folder_array[$i] );
#say 'is_empty:' . $is_empty_flag;
if($is_empty_flag == 1){
@empty_index_array = (@empty_index_array, $i);
#$empty_folder = splice @folder_array, $i, 1;
#say "$folder_array[$i] is empty";
}
}
mkdir "empty", 0755 or warn "mkdir empty fail: $!" if ( $#empty_index_array != 0 );
foreach (reverse @empty_index_array) {
say "move $_:$folder_array[$_] into empty";
$empty_folder = splice @folder_array, $_, 1;
&smart_move_in("$empty_folder", './empty');
}
# rename all folders with Complete DLSite name;
mkdir "fail", 0755 if ( ! -e "./fail" );
foreach my $folder (@folder_array) {
#say "calling rename with: " . $folder;
my $new_name ;
{
local $CWD = $working_dir;
$new_name = `./dlSiteRename '$folder'`;
# say "new_name = $new_name";
}
if($new_name =~ /\[\]/){
&smart_move_in("$folder", './fail');
}else{
&smart_rename($folder, $new_name);
}
}
# ------------ sub rutine -------------
sub is_empty_folder {
if( /mp3|wav|zip|rar|lzh/s ){
$is_empty_flag = 0;
}
}
sub wanted {
/^.*RJ.*\z/s
&& save_addr($name);
# save_addr($name);
}
sub save_addr {
Encode::_utf8_on($_[0]);
# print $_[0] . "\n";
my $new_file = $_[0];
# $target is a global variable reprecent the root of working directory:
$new_file =~ s#$target## ;
@all_file = ( @all_file, $new_file );
1;
}
sub check_RJ_unify {
my @words = split /\//, $_[0];
foreach ( @words ){
$_ =~ s/.*(RJ[0-9]{6}).*/$1/;
}
my $i = 0;
my $keyword = $words[$#words];
foreach( @words ){
$i++ if ($_ eq $keyword );
}
if( $i == 1 ){ $keyword; }
else { 0; }
}
sub smart_move_in {
my $file = $_[0];
my $folder = $_[1];
my ($i, $dash) = 1, '';
my ($name, $path, $suffix) = fileparse($file, @suffix_list);
while ( -e "$folder/$name$dash$suffix" ){ $dash = sprintf "-%d", $i++; }
say "mv $file $folder/$name$dash$suffix";
move "$file", "$folder/$name$dash$suffix";
}
sub smart_rename {
my $file = $_[0];
my $new_name = $_[1];
if( $file ne $new_name ){
my ($i, $dash) = 1, '';
my ($name, $path, $suffix) = fileparse($new_name, @suffix_list);
while ( -e "$new_name$dash$suffix" ){ $dash = sprintf "-%d", $i++; }
say "mv $file $new_name$dash$suffix";
rename "$file", "$new_name$dash$suffix";
}
}
#print Dumper(%file_array);
#print Dumper(@folder_array);