1
0

add config

This commit is contained in:
syui 2024-02-06 20:09:27 +09:00
parent aed7773a61
commit a2dd0a6008
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
4 changed files with 76 additions and 0 deletions

View File

@ -7,3 +7,4 @@ edition = "2021"
seahorse = "*" seahorse = "*"
reqwest = { version = "*", features = ["blocking", "json"] } reqwest = { version = "*", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
shellexpand = "*"

13
src/data.rs Normal file
View File

@ -0,0 +1,13 @@
//use config::{Config, ConfigError, File};
//use serde_derive::{Deserialize, Serialize};
pub fn token_file(s: &str) -> String {
let file = "/.config/ai/token";
let mut f = shellexpand::tilde("~").to_string();
f.push_str(&file);
match &*s {
"toml" => f + &".toml",
"json" => f + &".json",
_ => f + &"." + &s,
}
}

View File

@ -1,10 +1,14 @@
pub mod refresh; pub mod refresh;
pub mod token; pub mod token;
pub mod ascii; pub mod ascii;
pub mod data;
use seahorse::{App, Command, Context, Flag, FlagType}; use seahorse::{App, Command, Context, Flag, FlagType};
use std::env; use std::env;
use std::fs;
use std::io::Write;
use crate::ascii::c_ascii; use crate::ascii::c_ascii;
use crate::data::token_file;
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
@ -59,10 +63,15 @@ fn c_refresh(c: &Context) {
fn token(c: &Context) { fn token(c: &Context) {
let m = c.args[0].to_string(); let m = c.args[0].to_string();
let f = token_file(&"json");
let mut f = fs::File::create(f.clone()).unwrap();
let h = async { let h = async {
if let Ok(p) = c.string_flag("password") { if let Ok(p) = c.string_flag("password") {
let str = token::post_request(m.to_string(), p.to_string()).await; let str = token::post_request(m.to_string(), p.to_string()).await;
println!("{}",str); println!("{}",str);
f.write_all(&str.as_bytes()).unwrap();
} }
}; };
let res = tokio::runtime::Runtime::new().unwrap().block_on(h); let res = tokio::runtime::Runtime::new().unwrap().block_on(h);

53
src/reply.rs Normal file
View File

@ -0,0 +1,53 @@
extern crate reqwest;
use crate::token_toml;
use crate::url;
use serde_json::json;
use iso8601_timestamp::Timestamp;
pub async fn post_request(text: String, cid: String, uri: String, cid_p: String, uri_p: String) -> String {
let token = token_toml(&"access");
let did = token_toml(&"did");
let handle = token_toml(&"handle");
let url = url(&"record_create");
//let url = "https://bsky.social/xrpc/com.atproto.repo.createRecord";
let col = "app.bsky.feed.post".to_string();
let d = Timestamp::now_utc();
let d = d.to_string();
let post = Some(json!({
"repo": handle.to_string(),
"did": did.to_string(),
"collection": col.to_string(),
"record": {
"text": text.to_string(),
"createdAt": d.to_string(),
"reply": {
"root": {
"cid": cid.to_string(),
"uri": uri.to_string()
},
"parent": {
"cid": cid_p.to_string(),
"uri": uri_p.to_string()
}
}
},
}));
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&post)
.header("Authorization", "Bearer ".to_owned() + &token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
return res
}