From a11c52858c84c4422a22acc6f59aed5517587743 Mon Sep 17 00:00:00 2001 From: syui Date: Sat, 10 Feb 2024 16:27:43 +0900 Subject: [PATCH] refactor --- README.md | 11 +- src/describe.rs | 23 ++++ src/main.rs | 240 ++++++++++++++++++++++++++++++++--------- src/session.rs | 29 +++++ src/timeline_author.rs | 28 +++++ 5 files changed, 272 insertions(+), 59 deletions(-) create mode 100644 src/describe.rs create mode 100644 src/session.rs create mode 100644 src/timeline_author.rs diff --git a/README.md b/README.md index 56567c7..56d3285 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ $ ai ai -t avatar ```sh # ai token $handle -p $password -$ ai t yui.syui.ai -p password +$ ai l yui.syui.ai -p password $ cat ~/.config/ai/token.toml ``` ```sh -# ai token $handle -p $password -s $server -$ ai t ai.syu.is -p password -s syu.is +# ai l $handle -p $password -s $server +$ ai l ai.syu.is -p password -s syu.is ``` ### refresh @@ -39,11 +39,6 @@ $ ai t ai.syu.is -p password -s syu.is $ ai r ``` -``` -# server: syu.is -$ ai r -s syu.is -``` - ### notify ``` diff --git a/src/describe.rs b/src/describe.rs new file mode 100644 index 0000000..66a3a62 --- /dev/null +++ b/src/describe.rs @@ -0,0 +1,23 @@ +extern crate reqwest; +//use crate::data_toml; +use crate::url; + +pub async fn get_request(user: String) -> String { + + //let token = data_toml(&"access"); + let url = url(&"describe"); + + let client = reqwest::Client::new(); + let res = client + .get(url) + .query(&[("repo", &user)]) + //.header("Authorization", "Bearer ".to_owned() + &token) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + return res +} diff --git a/src/main.rs b/src/main.rs index 1236349..ee540ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,14 +11,17 @@ use crate::data::w_cid; use crate::data::c_char; use data::Notify as Notify; +pub mod ascii; pub mod data; pub mod refresh; pub mod token; +pub mod session; pub mod notify; pub mod notify_read; pub mod reply; pub mod reply_link; -pub mod ascii; +pub mod describe; +pub mod timeline_author; fn main() { let args: Vec = env::args().collect(); @@ -26,7 +29,7 @@ fn main() { .command( Command::new("ai") .alias("a") - .action(c_ascii_art) + .action(ascii_art) .flag( Flag::new("type", FlagType::String) .description("type flag") @@ -35,9 +38,9 @@ fn main() { ) .command( Command::new("token") - .alias("t") - .description("handle\n\t\t\t$ ai t yui.syui.ai -p password\n\t\t\t$ ai t yui.syui.ai -p password -s bsky.social") - .action(c_token) + .alias("l") + .description("$ ai l yui.syui.ai -p password\n\t\t\t$ ai t yui.syui.ai -p password -s bsky.social") + .action(token) .flag( Flag::new("password", FlagType::String) .description("password flag") @@ -52,36 +55,156 @@ fn main() { .command( Command::new("refresh") .alias("r") - .description("refresh\n\t\t\t$ ai r\n\t\t\t$ ai r -s bsky.social") - .action(c_refresh) - .flag( - Flag::new("server", FlagType::String) - .description("server flag") - .alias("s"), - ) + .description("$ ai r\n\t\t\t$ ai r -s bsky.social") + .action(refresh), ) .command( Command::new("notify") .alias("n") - .description("notify\n\t\t\t$ ai n") - .action(c_notify), + .description("$ ai n") + .action(notify), ) .command( - Command::new("bot") - .alias("b") - .description("bot\n\t\t\t$ ai b\n\t\t\t$ ai b -s bsky.social") - .action(c_bot) - .flag( - Flag::new("server", FlagType::String) - .description("server flag") - .alias("s"), + Command::new("timeline") + .description("$ ai t") + .alias("t") + .action(timeline), + ) + .command( + Command::new("did") + .description("$ ai did yui.syui.ai") + .action(did) ) - ) - ; + //.command( + // Command::new("like") + // .description("$ ai like \n\t\t\t$ ai like -u ") + // .action(c_like) + // .flag( + // Flag::new("uri", FlagType::String) + // .alias("u"), + // ) + // ) + //.command( + // Command::new("repost") + // .description("$ ai repost \n\t\t\t$ ai repost -u ") + // .action(c_repost) + // .flag( + // Flag::new("uri", FlagType::String) + // .alias("u"), + // ) + //) + //.command( + // Command::new("reply-og") + // .description("$ ai reply-og\n\t\t\t$ ai reply-og -c -u -i -t -d <description> -l <link>") + // .action(reply_og) + // .flag( + // Flag::new("uri", FlagType::String) + // .alias("u"), + // ) + // .flag( + // Flag::new("cid", FlagType::String) + // .alias("c"), + // ) + // .flag( + // Flag::new("link", FlagType::String) + // .alias("l"), + // ) + // .flag( + // Flag::new("title", FlagType::String) + // .alias("t"), + // ) + // .flag( + // Flag::new("description", FlagType::String) + // .alias("d"), + // ) + // .flag( + // Flag::new("img", FlagType::String) + // .alias("i"), + // ) + // ) + // .command( + // Command::new("handle") + // .usage("atr h") + // .description("handle update\n\t\t\t$ atr -h example.com\n\t\t\t$ atr -h user.bsky.social") + // .alias("h") + // .action(c_handle) + // ) + // .command( + // Command::new("feed") + // .usage("atr f") + // .description("feed user\n\t\t\t$ atr f\n\t\t\t$ atr f -u user.bsky.social") + // .alias("f") + // .action(c_feed) + // .flag( + // Flag::new("user", FlagType::String) + // .description("user flag(ex: $ atr f -u user)") + // .alias("u"), + // ) + // ) + // .command( + // Command::new("post") + // .description("$ ai p <text>\n\t\t\t$ ai p <text> -l https://syui.ai") + // .alias("p") + // .action(c_post) + // .flag( + // Flag::new("link", FlagType::String) + // .description("link flag(ex: $ atr p -l)") + // .alias("l"), + // ) + // .flag( + // Flag::new("cid", FlagType::String) + // .description("link flag(ex: $ atr p -l)") + // ) + // .flag( + // Flag::new("uri", FlagType::String) + // .description("link flag(ex: $ atr p -l)") + // ) + // ) + // .command( + // Command::new("reply") + // .usage("atr r {}") + // .description("reply\n\t\t\t$ atr r $text -u $uri -c $cid") + // .action(c_reply) + // .flag( + // Flag::new("uri", FlagType::String) + // .description("uri flag(ex: $ atr r -u)") + // .alias("u"), + // ) + // .flag( + // Flag::new("cid", FlagType::String) + // .description("cid flag(ex: $ atr r -u -c)") + // .alias("c"), + // ) + // .flag( + // Flag::new("link", FlagType::String) + // .description("link flag(ex: $ atr r $text -u $uri -c $cid -l $link)") + // .alias("l"), + // ) + // ) + // .command( + // Command::new("mention") + // .usage("atr mention {}") + // .description("mention\n\t\t\t$ atr @ syui.bsky.social -p $text") + // .alias("@") + // .action(c_mention) + // .flag( + // Flag::new("post", FlagType::String) + // .description("post flag\n\t\t\t$ atr @ syui.bsky.social -p text") + // .alias("p"), + // ) + // ) + + .command( + Command::new("bot") + .alias("b") + .description("bot\n\t\t\t$ ai b\n\t\t\t$ ai b -s bsky.social") + .action(bot), + ) + ; app.run(args); } -fn c_ascii_art(c: &Context) { +fn ascii_art(c: &Context) { if let Ok(t) = c.string_flag("type") { c_ascii(&t); } else { @@ -106,28 +229,24 @@ fn token(c: &Context) { return res } -fn c_token(c: &Context) { - token(c); -} - -fn refresh(c: &Context) { +fn refresh(_c: &Context) { + let server = data_toml(&"host"); let h = async { - let res = refresh::post_request().await; - if let Ok(s) = c.string_flag("server") { - w_cfg(&s, &res) + let session = session::get_request().await; + if session == "err" { + let res = refresh::post_request().await; + println!("{}", res); + w_cfg(&server, &res) } else { - w_cfg("bsky.social", &res) + println!("no refresh"); } }; let res = tokio::runtime::Runtime::new().unwrap().block_on(h); return res } -fn c_refresh(c: &Context) { +fn notify(c: &Context) { refresh(c); -} - -fn notify() { let h = async { let j = notify::get_request(100).await; println!("{}", j); @@ -136,23 +255,42 @@ fn notify() { return res } -fn c_notify(_c: &Context) { - notify(); +fn did(c: &Context) { + refresh(c); + let h = async { + if c.args.len() == 0 { + let j = describe::get_request(data_toml(&"handle")).await; + println!("{}", j); + } else { + let j = describe::get_request(c.args[0].to_string()).await; + println!("{}", j); + } + }; + let res = tokio::runtime::Runtime::new().unwrap().block_on(h); + return res } -fn bot(c: &Context) { +fn timeline(c: &Context) { + refresh(c); + let h = async { + if c.args.len() == 0 { + let str = timeline_author::get_request(data_toml(&"handle").to_string()); + println!("{}",str.await); + } else { + let str = timeline_author::get_request(c.args[0].to_string()); + println!("{}",str.await); + } + }; + let res = tokio::runtime::Runtime::new().unwrap().block_on(h); + return res +} + +fn c_bot(c: &Context) { let h = async { - let server = "bsky.social"; let mut notify = notify::get_request(100).await; if notify == "err" { - if let Ok(s) = c.string_flag("server") { - let res = refresh::post_request().await; - w_cfg(&s, &res); - } else { - let res = refresh::post_request().await; - w_cfg(&server, &res); - } + refresh(c); notify = notify::get_request(100).await; } let notify: Notify = serde_json::from_str(¬ify).unwrap(); @@ -214,8 +352,8 @@ fn bot(c: &Context) { } -fn c_bot(c: &Context) { +fn bot(c: &Context) { loop { - bot(c); + c_bot(c); } } diff --git a/src/session.rs b/src/session.rs new file mode 100644 index 0000000..084f8bc --- /dev/null +++ b/src/session.rs @@ -0,0 +1,29 @@ +extern crate reqwest; +use crate::data_toml; +use crate::url; + +pub async fn get_request() -> String { + + let token = data_toml(&"access"); + let url = url(&"session_get"); + + let client = reqwest::Client::new(); + let res = client + .get(url) + .header("Authorization", "Bearer ".to_owned() + &token) + .send() + .await + .unwrap(); + + let status_ref = res.error_for_status_ref(); + + match status_ref { + Ok(_) => { + return res.text().await.unwrap(); + }, + Err(_e) => { + let e = "err".to_string(); + return e + } + } +} diff --git a/src/timeline_author.rs b/src/timeline_author.rs new file mode 100644 index 0000000..9731c63 --- /dev/null +++ b/src/timeline_author.rs @@ -0,0 +1,28 @@ +extern crate reqwest; +use crate::data_toml; +use crate::url; + +pub async fn get_request(actor: String) -> String { + + let token = data_toml(&"access"); + let url = url(&"record_list"); + + let actor = actor.to_string(); + //let cursor = cursor.unwrap(); + + let col = "app.bsky.feed.post".to_string(); + let client = reqwest::Client::new(); + let res = client + .get(url) + .query(&[("repo", actor),("collection", col)]) + //.query(&[("actor", actor),("cursor", cursor)]) + .header("Authorization", "Bearer ".to_owned() + &token) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + return res +}