-
Notifications
You must be signed in to change notification settings - Fork 15
/
insertDLSiteDB
executable file
·96 lines (76 loc) · 2.36 KB
/
insertDLSiteDB
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
#! /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 DJVoiceConfig;
use open ":encoding(utf8)";
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
#----- check input -----
if(! ($ARGV[0] =~ /(RJ[0-9]{6})/) ){
die 'need RJ###### in $ARGV[0]';
}
my $param = decode_json($ARGV[0]);
my $id = $param->{'id'};
my $working_dir = $DJVoiceConfig::WORKING_DIR;
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;
# print "Opened database successfully\n";
my $stmt = "SELECT COUNT(id) 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 @exist = $sth->fetchrow_array();
if($exist[0]) {
say "$id:insert DB duplicate";
$stmt = "UPDATE voiceWork SET read = 1 WHERE id = '$id' AND CAST(read AS INT) = 0;";
}else{
my @keywords = qw(id title circle_id date circle_name);
my $input_check = 1;
foreach (@keywords) {
#say $_ ,':', $param->{$_};
if(!$param->{$_}){
$input_check = 0;
last;
}
}
if(!$input_check){
#say "grapDLSite $id";
my $json_text = `$working_dir/grapDLSite $id`;
#say $json_text;
$param = decode_json( encode('UTF-8', $json_text) );
}
my $title = $param->{'title' };
my $circle_id = $param->{'circle_id' };
my $circle_name = $param->{'circle_name' };
my $image = $param->{'image' };
my $date = $param->{'date' };
my $tag = $param->{'tag' };
my $vocal = $param->{'vocal' };
#say "$id, $title, $circle_id, $date";
$title =~ s/'/''/g;
$stmt = "INSERT INTO voiceWork "
. "(id, title, date, circle_id, circle_name, tag, vocal, read)"
. "VALUES"
. "('$id', '$title', '$date', '$circle_id','$circle_name', '$tag', '$vocal', 1)";
}
$sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
$dbh->disconnect();