-
Notifications
You must be signed in to change notification settings - Fork 15
/
buildDLSite
executable file
·151 lines (124 loc) · 3.66 KB
/
buildDLSite
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
#! /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 smart_mv;
use DJVoiceConfig;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
my $destDir = $DJVoiceConfig::PUSH_STORAGE_PATH;
my $working_dir = $DJVoiceConfig::WORKING_DIR;
my $dlc_json_file = $ARGV[0];
our $target = $ARGV[1];
our @all_file;
open JSONF, "<", $dlc_json_file;
my $dlc_json;
foreach my $line (<JSONF>){
chomp($line);
$line =~ s/^\s*//;
$dlc_json .= $line if(!($line =~ /^$/));
}
close JSONF;
my $data = decode_json( $dlc_json );
my @data_array = @$data;
#$target .= '/' if( substr($target, -1) ne '/');
{
#chdir $ARGV[0] only in this section
local $CWD = $target;
File::Find::find(
{
wanted => sub {
if(/^.*RJ.*\z/s && -d $_){
my $depth = tr!/!!; # count slashes to get depth
return if $depth > 1;
Encode::_utf8_on($_);
push @all_file, $_;
}
},
no_chdir => 1
},
'.'
);
}
my $driver = "SQLite";
my $database = $DJVoiceConfig::PUSH_DATABASE_PATH;
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
foreach (@all_file){
my $dir = $_;
#my $dir = $all_file[0];
if(! ($dir =~ /(RJ[0-9]{6})/) ){
die 'need RJ###### in $ARGV[1]';
}
my $id = $1;
my $stmt = "SELECT COUNT(id), circle_id, circle_name FROM voiceWork WHERE id = '$id';";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
my @row = $sth->fetchrow_array();
my $param;
my $fetch_success = $row[0];
if($fetch_success) {
$param->{'circle_id' } = decode('UTF-8', $row[1]);
$param->{'circle_name' } = decode('UTF-8', $row[2]);
my ($current_work) = grep { $_->{id} eq $id }@data_array;
$param->{'image'} = $current_work->{'image'};
#say "$param->{'image'} , $param->{'circle_id' } , $param->{'circle_name' }";
}
my $json;
if( !($fetch_success && $param->{'image'}) ){
$json = `$working_dir/grapDLSite '$id'`;
#say $json;
$param = decode_json($json);
}
#----- grap image
{
local $CWD = $target;
my $file = my $image = $param->{'image'};
$file =~ s/^.*\///;
$file = "$dir/$file";
$file =~ s/"/\\"/g;
say "grap image:$file";
my $command = 'curl -s -o "' . $file . '" ' . "'$image'";
say $command;
system($command) if( ! -e $file);
}
#----- insert DB or set read = 0 -> 1
if($json =~ /'/ || $json eq ''){
# exception : json with ( ' )
$json = sprintf '{"id":"%s"}' , $id;
}
#my $err_msg = `'`;
# say $err_msg;
if(system("$working_dir/insertDLSiteDB '$json'")){
die 'somthing wrong with insertDLSiteDB $json';
}
#----- orgnize File
# next;
{
local $CWD = $target;
$param->{'circle_name'} =~ s/\//@/g;
my $target_dir = "$destDir/$param->{'circle_id'} $param->{'circle_name'}/";
#say $target_dir;
mkdir "$target_dir", 0755 or warn "mkdir $target_dir fail: $!" if( ! -e -d $target_dir );
smart_move_in($dir, $target_dir);
}
}
#----- update DL count
system("$working_dir/updateDLSiteDB $dlc_json_file");