added dotenv, INPUT, sprintf
parent
10079766f3
commit
4f80acb5e9
|
@ -89,13 +89,42 @@ name = "deepl"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"directories",
|
||||||
|
"dotenv",
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-tls",
|
"hyper-tls",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"sprintf",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "directories"
|
||||||
|
version = "4.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5f0435dee2e8f0783bd48571bd8a8a8f531b5adcf0b8d7dc705a37c81108418d"
|
||||||
|
dependencies = [
|
||||||
|
"dirs-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dirs-sys"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"redox_users",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenv"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
|
@ -548,6 +577,16 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_users"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"redox_syscall",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "remove_dir_all"
|
name = "remove_dir_all"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
|
@ -664,6 +703,12 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sprintf"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7e0c270adf444e0025dea43f5f45c17ef9bf0e603d58381b95dc79cf9bfc0cea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|
|
@ -13,4 +13,7 @@ hyper = { version = "0.14", features = ["full"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
clap = {version = "~2.33.0", features = ["yaml"]}
|
clap = {version = "~2.33.0", features = ["yaml"]}
|
||||||
|
directories = "4.0"
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
sprintf = "0.1.1"
|
11
src/cli.yml
11
src/cli.yml
|
@ -11,7 +11,7 @@ args:
|
||||||
takes_value: true
|
takes_value: true
|
||||||
required: false
|
required: false
|
||||||
- target:
|
- target:
|
||||||
short: t
|
short: d
|
||||||
long: target
|
long: target
|
||||||
value_name: target_lang
|
value_name: target_lang
|
||||||
help: The language your text should be tranlated to.
|
help: The language your text should be tranlated to.
|
||||||
|
@ -20,8 +20,11 @@ args:
|
||||||
- file:
|
- file:
|
||||||
short: f
|
short: f
|
||||||
long: file
|
long: file
|
||||||
desc: File you want to translate.
|
help: File you want to translate.
|
||||||
help: Csv delimiter character
|
|
||||||
required: false
|
required: false
|
||||||
takes_value: true
|
takes_value: true
|
||||||
|
- INPUT:
|
||||||
|
help: Text to translate
|
||||||
|
required: true
|
||||||
|
index: 1
|
||||||
|
|
||||||
|
|
49
src/lib.rs
49
src/lib.rs
|
@ -1,14 +1,37 @@
|
||||||
pub mod deepl_helper {
|
pub mod deepl_helper {
|
||||||
use clap::ArgMatches;
|
use clap::{load_yaml, App};
|
||||||
|
use directories::BaseDirs;
|
||||||
|
use dotenv;
|
||||||
|
use sprintf::sprintf;
|
||||||
|
use std::env::{self};
|
||||||
|
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
source_lang: String,
|
source_lang: String,
|
||||||
destination_lang: String,
|
destination_lang: String,
|
||||||
file: String,
|
file: String,
|
||||||
|
key: String,
|
||||||
|
uri: String,
|
||||||
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Params {
|
impl Params {
|
||||||
pub fn new_from_matches(matches: ArgMatches) -> Self {
|
pub fn new() -> Self {
|
||||||
|
let base_dir = match BaseDirs::new() {
|
||||||
|
Some(dirs) => dirs,
|
||||||
|
None => {
|
||||||
|
panic!("Please provide key in ~/.config/deepl/.env file")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let my_path = base_dir.config_dir().join("deepl/.env");
|
||||||
|
match dotenv::from_path(my_path.as_path()) {
|
||||||
|
Ok(env) => env,
|
||||||
|
Err(_) => panic!("could not load .env file {:?}", my_path.as_path()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let yaml = load_yaml!("cli.yml");
|
||||||
|
let matches = App::from_yaml(yaml).get_matches();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
source_lang: matches.value_of("source_lang").unwrap_or("EN").to_string(),
|
source_lang: matches.value_of("source_lang").unwrap_or("EN").to_string(),
|
||||||
destination_lang: matches
|
destination_lang: matches
|
||||||
|
@ -16,19 +39,25 @@ pub mod deepl_helper {
|
||||||
.unwrap_or("DE")
|
.unwrap_or("DE")
|
||||||
.to_string(),
|
.to_string(),
|
||||||
file: matches.value_of("file").unwrap_or_default().to_string(),
|
file: matches.value_of("file").unwrap_or_default().to_string(),
|
||||||
|
key: env::var("key").unwrap(),
|
||||||
|
uri: env::var("uri").unwrap(),
|
||||||
|
text: matches.value_of("INPUT").unwrap().to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(&self) -> &String {
|
pub fn url(&self) -> String {
|
||||||
&self.source_lang
|
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dest(&self) -> &String {
|
pub fn body(&self) -> String {
|
||||||
&self.destination_lang
|
sprintf!(
|
||||||
}
|
"auth_key=%s&text=%s&target_lang=%s&source_lang=%s",
|
||||||
|
self.key,
|
||||||
pub fn file(&self) -> &String {
|
self.text,
|
||||||
&self.file
|
self.destination_lang,
|
||||||
|
self.source_lang
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use deepl::deepl_helper::Params;
|
||||||
use hyper::{Body, Client, Method, Request, body};
|
use hyper::{Body, Client, Method, Request, body};
|
||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
@ -7,11 +8,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let https = HttpsConnector::new();
|
let https = HttpsConnector::new();
|
||||||
let client = Client::builder().build::<_, hyper::Body>(https);
|
let client = Client::builder().build::<_, hyper::Body>(https);
|
||||||
|
|
||||||
|
let params = Params::new();
|
||||||
|
|
||||||
let request = Request::builder()
|
let request = Request::builder()
|
||||||
.method(Method::POST)
|
.method(Method::POST)
|
||||||
.uri("https://api-free.deepl.com/v2/translate?auth_key=7e47a305-561c-0a25-1e1a-ba929d96a88c:fx")
|
.uri(params.url())
|
||||||
.header("content-type", "application/x-www-form-urlencoded")
|
.header("content-type", "application/x-www-form-urlencoded")
|
||||||
.body(Body::from(r#"auth_key=7e47a305-561c-0a25-1e1a-ba929d96a88c:fx&text=Hello, world&target_lang=DE"#))?;
|
.body(Body::from(params.body()))?;
|
||||||
|
|
||||||
let res = client.request(request).await?;
|
let res = client.request(request).await?;
|
||||||
let body_bytes = body::to_bytes(res.into_body()).await?;
|
let body_bytes = body::to_bytes(res.into_body()).await?;
|
||||||
|
|
Loading…
Reference in New Issue