Add file to database

This commit is contained in:
zyppe 2025-02-24 22:28:20 +08:00
parent 54e6b0a139
commit 80e3c8cc3a
3 changed files with 66 additions and 48 deletions

View file

@ -4,9 +4,7 @@ use warnings;
use v5.26; use v5.26;
use Carp; use Carp;
use Data::Dumper;
use File::Find; use File::Find;
use SQL::Abstract;
use DBI; use DBI;
my $db = my $db =
@ -14,17 +12,18 @@ my $db =
or confess $DBI::errstr; or confess $DBI::errstr;
sub sqlstr { sub sqlstr {
my ( $table, $primary ) = @_;
return qq return qq
/create table if not exists @_ ( /create table if not exists $table (
id integer primary key autoincrement, id integer / . ( $primary ? 'primary key autoincrement' : '' ) . qq/,
title text, title text,
filepath text, filepath text,
content text content text
);/; );/;
} }
my %tablesql = ( my %tablesql = (
"origin" => sqlstr("origin"), "origin" => sqlstr( "origin", 1 ),
"trans" => sqlstr("trans"), "trans" => sqlstr( "trans", 0 ),
); );
my $act; my $act;
for my $table ( keys %tablesql ) { for my $table ( keys %tablesql ) {

View file

@ -8,34 +8,38 @@ use Time::Piece;
use Digest::SHA qw(sha256_hex hmac_sha256_hex); use Digest::SHA qw(sha256_hex hmac_sha256_hex);
use JSON::MaybeXS qw(encode_json); use JSON::MaybeXS qw(encode_json);
carp "You must provide SecretKey.csv to parent folder!"; use Exporter 'import';
our $SourceText ; our @EXPORT_OK = 'sign';
my $data = {
SourceText => $SourceText,
Source => 'en',
Target => 'zh',
ProjectID => 0
};
my $payload = encode_json($data);
my $service = "tmt"; sub sign {
my $host = "$service.tencentcloudapi.com"; my $SourceText = @_;
my $region = "ap-shanghai"; carp "You must provide SecretKey.csv to parent folder!";
my $action = "TextTranslate"; my $data = {
my $version = "2018-03-21"; SourceText => $SourceText,
my $Algorithm = "TC3-HMAC-SHA256"; Source => 'en',
Target => 'zh',
ProjectID => 0
};
my $payload = encode_json($data);
my $HTTPRequestMethod = "POST"; my $service = "tmt";
my $CanonicalURI = "/"; my $host = "$service.tencentcloudapi.com";
my $CanonicalQueryString = my $region = "ap-shanghai";
( $HTTPRequestMethod eq "POST" ) ? "" : "Limit=10&Offset=0"; my $action = "TextTranslate";
my $CanonicalHeaders = qq my $version = "2018-03-21";
my $Algorithm = "TC3-HMAC-SHA256";
my $HTTPRequestMethod = "POST";
my $CanonicalURI = "/";
my $CanonicalQueryString =
( $HTTPRequestMethod eq "POST" ) ? "" : "Limit=10&Offset=0";
my $CanonicalHeaders = qq
|content-type:application/json; charset=utf-8 |content-type:application/json; charset=utf-8
host:$host host:$host
x-tc-action:$action|; x-tc-action:$action|;
my $SignedHeaders = "content-type;host;x-tc-action"; my $SignedHeaders = "content-type;host;x-tc-action";
my $HashedRequestPayload = sha256_hex $payload; my $HashedRequestPayload = sha256_hex $payload;
my $CanonicalRequest = qq my $CanonicalRequest = qq
/$HTTPRequestMethod /$HTTPRequestMethod
$CanonicalURI $CanonicalURI
$CanonicalQueryString $CanonicalQueryString
@ -43,30 +47,31 @@ $CanonicalHeaders
$SignedHeaders $SignedHeaders
$HashedRequestPayload/; $HashedRequestPayload/;
my $RequestTimestamp = time(); my $RequestTimestamp = time();
my $Date = localtime->strftime('%Y-%m-%d'); my $Date = localtime->strftime('%Y-%m-%d');
my $CredentialScope = "$Date/$service/tc3_request"; my $CredentialScope = "$Date/$service/tc3_request";
my $HashedCanonicalRequest = sha256_hex $CanonicalRequest; my $HashedCanonicalRequest = sha256_hex $CanonicalRequest;
my $StringToSign = qq my $StringToSign = qq
/$Algorithm /$Algorithm
$RequestTimestamp $RequestTimestamp
$CredentialScope $CredentialScope
$HashedCanonicalRequest/; $HashedCanonicalRequest/;
open my $in, "<", "../SecretKey.csv" or confess; open my $in, "<", "../SecretKey.csv" or confess;
my $key; my $key;
<$in>; <$in>;
$key = <$in>; $key = <$in>;
close($in); close($in);
my @key = split( ",", $key ); my @key = split( ",", $key );
my $SecretID = $key[0]; my $SecretID = $key[0];
my $SecretKey = $key[1]; my $SecretKey = $key[1];
my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date ); my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date );
my $SecretService = hmac_sha256_hex( $SecretDate, $service ); my $SecretService = hmac_sha256_hex( $SecretDate, $service );
my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" ); my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" );
my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign ); my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign );
our $Authorization = my $Authorization =
"$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature"; "$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature";
return $Authorization;
}
say $payload;

14
Translator/translate.pl Normal file
View file

@ -0,0 +1,14 @@
#!/usr/bin/perl
use strict;
use warnings;
use v5.26;
use Carp;
use File::Find;
use DBI;
use lib 'sign.pl';
my $db =
DBI->connect( "DBI:SQLite:dbname=../data.db", "", "", { RaiseError => 1 } )
or confess $DBI::errstr;