From 54e6b0a139c1a62904d30ca2de2a9721e1bd5adf Mon Sep 17 00:00:00 2001 From: zyppe <210hcl@gmail.com> Date: Sat, 22 Feb 2025 23:59:12 +0800 Subject: [PATCH] save doc to sqlite --- .gitignore | 5 +++- Translator/read.pl | 65 ++++++++++++++++++++++++++++++++++++++++++++++ Translator/sen.pl | 30 +++++++++++++++++++++ Translator/sign.pl | 24 +++++++++++------ 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 Translator/read.pl create mode 100644 Translator/sen.pl diff --git a/.gitignore b/.gitignore index de11b0c..a823c53 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .idea/ .vscode/ -SecretKey.csv \ No newline at end of file +SecretKey.csv +data/ +output/ +*.db \ No newline at end of file diff --git a/Translator/read.pl b/Translator/read.pl new file mode 100644 index 0000000..309d91b --- /dev/null +++ b/Translator/read.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl +use strict; +use warnings; +use v5.26; + +use Carp; +use Data::Dumper; +use File::Find; +use SQL::Abstract; +use DBI; + +my $db = + DBI->connect( "DBI:SQLite:dbname=../data.db", "", "", { RaiseError => 1 } ) + or confess $DBI::errstr; + +sub sqlstr { + return qq +/create table if not exists @_ ( + id integer primary key autoincrement, + title text, + filepath text, + content text +);/; +} +my %tablesql = ( + "origin" => sqlstr("origin"), + "trans" => sqlstr("trans"), +); +my $act; +for my $table ( keys %tablesql ) { + $act = $db->prepare( $tablesql{$table} ); + $act->execute() or confess $DBI::errstr; +} + +my @data; +finddepth( + sub { + return unless ( $_ =~ /.md$/ ); + push @data, $File::Find::name; + }, + '../data/' +); +my $csql = qq +/insert into origin (title,filepath,content) + values (?,?,?);/; +for my $d (@data) { + my %info; + my $content; + open my $file, "< :encoding(UTF-8)", $d; + <$file>; + while (<$file>) { + chomp; + if (/^([^:]+):\s+(.+)/) { + $info{$1} = $2; + } + last if /---/; + } + while (<$file>) { + $content .= $_; + } + close($file); + $act = $db->prepare($csql); + $act->execute( $info{'title'}, $d, $content ) or carp $DBI::errstr; + $act->finish() or carp $DBI::errstr; +} diff --git a/Translator/sen.pl b/Translator/sen.pl new file mode 100644 index 0000000..3328434 --- /dev/null +++ b/Translator/sen.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use strict; +use warnings; +use v5.26; + +use Text::CSV; +use Carp; +use utf8; + +my $csv = Text::CSV->new( { binary => 1, auto_diag => 1 } ); +open my $in, "< :encoding(UTF-8)", $ARGV[0] or confess; +open my $out, "> :encoding(UTF-8)", "../output/import.csv" or confess; +$csv->print( $out, [ "原文", "译文" ] ); +my $msgid; +my $msgstr; +while ( my $line = <$in> ) { + chomp $line; + if ( $line =~ /^msgid "(.*)"/ ) { + $msgid = $1; + } + elsif ( $line =~ /^msgstr "(.*)"/ ) { + $msgstr = $1; + $msgid =~ s/\\n/\n/g; + $msgstr =~ s/\\n/\n/g; + $csv->print( $out, [ $msgid, $msgstr ] ); + print $out "\n"; + } +} +close $in; +close $out; diff --git a/Translator/sign.pl b/Translator/sign.pl index 6b34b7a..a804fbc 100644 --- a/Translator/sign.pl +++ b/Translator/sign.pl @@ -3,13 +3,20 @@ use strict; use warnings; use v5.26; +use Carp; 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); -say "You must provide SecretKey.csv to parent folder!"; -my $SourceText = "Get Current Time in Seconds Perl"; -my $payload = qq - /{"SourceText":"$SourceText","Source":"en","Target":"zh","ProjectId":0}/; +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); my $service = "tmt"; my $host = "$service.tencentcloudapi.com"; @@ -46,7 +53,7 @@ $RequestTimestamp $CredentialScope $HashedCanonicalRequest/; -open my $in, "<", "../SecretKey.csv" or die $!; +open my $in, "<", "../SecretKey.csv" or confess; my $key; <$in>; $key = <$in>; @@ -59,6 +66,7 @@ my $SecretService = hmac_sha256_hex( $SecretDate, $service ); my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" ); my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign ); -my $Authorization = +our $Authorization = "$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature"; -say $Authorization; + +say $payload; \ No newline at end of file