1
0
This commit is contained in:
syui 2023-10-20 23:31:16 +09:00
parent 430b5977d8
commit fc863ad31d
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
6 changed files with 212 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
Cargo.lock
target
*.json
*.DS_Store
**.DS_Store

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "ai"
version = "0.1.0"
edition = "2021"
[dependencies]
seahorse = "*"
reqwest = { version = "*", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] }

80
src/ascii.rs Normal file
View File

@ -0,0 +1,80 @@
pub fn c_ascii(x: bool) {
let logo = "
";
let avatar = "
";
match x {
true => println!("{}", avatar),
false => println!("{}", logo),
}
}

74
src/main.rs Normal file
View File

@ -0,0 +1,74 @@
pub mod refresh;
pub mod token;
pub mod ascii;
use seahorse::{App, Command, Context, Flag, FlagType};
use std::env;
use crate::ascii::c_ascii;
fn main() {
let args: Vec<String> = env::args().collect();
let app = App::new(env!("CARGO_PKG_NAME"))
.command(
Command::new("ai")
.alias("a")
.action(c_ascii_art)
.flag(
Flag::new("type", FlagType::Bool)
.description("type flag")
.alias("t"),
)
)
.command(
Command::new("refresh")
.alias("r")
.action(c_refresh),
)
.command(
Command::new("token")
.alias("t")
.description("handle\n\t\t\t$ ai t syui.bsky.social -p password")
.action(c_token)
.flag(
Flag::new("password", FlagType::String)
.description("password flag")
.alias("p"),
)
)
;
app.run(args);
}
fn c_ascii_art(c: &Context) {
c_ascii(c.bool_flag("type"));
}
fn refresh(c: &Context) {
let m = c.args[0].to_string();
let h = async {
let str = refresh::post_request(m.to_string()).await;
println!("{}",str);
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}
fn c_refresh(c: &Context) {
refresh(c);
}
fn token(c: &Context) {
let m = c.args[0].to_string();
let h = async {
if let Ok(p) = c.string_flag("password") {
let str = token::post_request(m.to_string(), p.to_string()).await;
println!("{}",str);
}
};
let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res
}
fn c_token(c: &Context) {
token(c);
}

19
src/refresh.rs Normal file
View File

@ -0,0 +1,19 @@
extern crate reqwest;
pub async fn post_request(refresh: String) -> String {
let url = "https://bsky.social/xrpc/com.atproto.server.refreshSession";
let client = reqwest::Client::new();
let res = client
.post(url)
.header("Authorization", "Bearer ".to_owned() + &refresh)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
return res
}

24
src/token.rs Normal file
View File

@ -0,0 +1,24 @@
extern crate reqwest;
use std::collections::HashMap;
pub async fn post_request(handle: String, pass: String) -> String {
let url = "https://bsky.social/xrpc/com.atproto.server.createSession";
let mut map = HashMap::new();
map.insert("identifier", &handle);
map.insert("password", &pass);
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&map)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
return res
}