diff --git a/Translator/read.pl b/Translator/read.pl index 309d91b..594a581 100644 --- a/Translator/read.pl +++ b/Translator/read.pl @@ -4,9 +4,7 @@ use warnings; use v5.26; use Carp; -use Data::Dumper; use File::Find; -use SQL::Abstract; use DBI; my $db = @@ -14,17 +12,18 @@ my $db = or confess $DBI::errstr; sub sqlstr { + my ( $table, $primary ) = @_; return qq -/create table if not exists @_ ( - id integer primary key autoincrement, +/create table if not exists $table ( + id integer / . ( $primary ? 'primary key autoincrement' : '' ) . qq/, title text, filepath text, content text );/; } my %tablesql = ( - "origin" => sqlstr("origin"), - "trans" => sqlstr("trans"), + "origin" => sqlstr( "origin", 1 ), + "trans" => sqlstr( "trans", 0 ), ); my $act; for my $table ( keys %tablesql ) { diff --git a/Translator/sign.pl b/Translator/sign.pl index a804fbc..896d11e 100644 --- a/Translator/sign.pl +++ b/Translator/sign.pl @@ -8,34 +8,38 @@ use Time::Piece; use Digest::SHA qw(sha256_hex hmac_sha256_hex); use JSON::MaybeXS qw(encode_json); -carp "You must provide SecretKey.csv to parent folder!"; -our $SourceText ; -my $data = { - SourceText => $SourceText, - Source => 'en', - Target => 'zh', - ProjectID => 0 -}; -my $payload = encode_json($data); +use Exporter 'import'; +our @EXPORT_OK = 'sign'; -my $service = "tmt"; -my $host = "$service.tencentcloudapi.com"; -my $region = "ap-shanghai"; -my $action = "TextTranslate"; -my $version = "2018-03-21"; -my $Algorithm = "TC3-HMAC-SHA256"; +sub sign { + my $SourceText = @_; + carp "You must provide SecretKey.csv to parent folder!"; + my $data = { + SourceText => $SourceText, + Source => 'en', + Target => 'zh', + ProjectID => 0 + }; + my $payload = encode_json($data); -my $HTTPRequestMethod = "POST"; -my $CanonicalURI = "/"; -my $CanonicalQueryString = - ( $HTTPRequestMethod eq "POST" ) ? "" : "Limit=10&Offset=0"; -my $CanonicalHeaders = qq + my $service = "tmt"; + my $host = "$service.tencentcloudapi.com"; + my $region = "ap-shanghai"; + my $action = "TextTranslate"; + 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 host:$host x-tc-action:$action|; -my $SignedHeaders = "content-type;host;x-tc-action"; -my $HashedRequestPayload = sha256_hex $payload; -my $CanonicalRequest = qq + my $SignedHeaders = "content-type;host;x-tc-action"; + my $HashedRequestPayload = sha256_hex $payload; + my $CanonicalRequest = qq /$HTTPRequestMethod $CanonicalURI $CanonicalQueryString @@ -43,30 +47,31 @@ $CanonicalHeaders $SignedHeaders $HashedRequestPayload/; -my $RequestTimestamp = time(); -my $Date = localtime->strftime('%Y-%m-%d'); -my $CredentialScope = "$Date/$service/tc3_request"; -my $HashedCanonicalRequest = sha256_hex $CanonicalRequest; -my $StringToSign = qq + my $RequestTimestamp = time(); + my $Date = localtime->strftime('%Y-%m-%d'); + my $CredentialScope = "$Date/$service/tc3_request"; + my $HashedCanonicalRequest = sha256_hex $CanonicalRequest; + my $StringToSign = qq /$Algorithm $RequestTimestamp $CredentialScope $HashedCanonicalRequest/; -open my $in, "<", "../SecretKey.csv" or confess; -my $key; -<$in>; -$key = <$in>; -close($in); -my @key = split( ",", $key ); -my $SecretID = $key[0]; -my $SecretKey = $key[1]; -my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date ); -my $SecretService = hmac_sha256_hex( $SecretDate, $service ); -my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" ); -my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign ); + open my $in, "<", "../SecretKey.csv" or confess; + my $key; + <$in>; + $key = <$in>; + close($in); + my @key = split( ",", $key ); + my $SecretID = $key[0]; + my $SecretKey = $key[1]; + my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date ); + my $SecretService = hmac_sha256_hex( $SecretDate, $service ); + my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" ); + my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign ); -our $Authorization = + my $Authorization = "$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature"; + return $Authorization; +} -say $payload; \ No newline at end of file diff --git a/Translator/translate.pl b/Translator/translate.pl new file mode 100644 index 0000000..03955ed --- /dev/null +++ b/Translator/translate.pl @@ -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; +