1
0
This commit is contained in:
syui 2024-02-10 16:27:43 +09:00
parent 2f9839620f
commit a11c52858c
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
5 changed files with 272 additions and 59 deletions

View File

@ -23,14 +23,14 @@ $ ai ai -t avatar
```sh ```sh
# ai token $handle -p $password # ai token $handle -p $password
$ ai t yui.syui.ai -p password $ ai l yui.syui.ai -p password
$ cat ~/.config/ai/token.toml $ cat ~/.config/ai/token.toml
``` ```
```sh ```sh
# ai token $handle -p $password -s $server # ai l $handle -p $password -s $server
$ ai t ai.syu.is -p password -s syu.is $ ai l ai.syu.is -p password -s syu.is
``` ```
### refresh ### refresh
@ -39,11 +39,6 @@ $ ai t ai.syu.is -p password -s syu.is
$ ai r $ ai r
``` ```
```
# server: syu.is
$ ai r -s syu.is
```
### notify ### notify
``` ```

23
src/describe.rs Normal file
View File

@ -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
}

View File

@ -11,14 +11,17 @@ use crate::data::w_cid;
use crate::data::c_char; use crate::data::c_char;
use data::Notify as Notify; use data::Notify as Notify;
pub mod ascii;
pub mod data; pub mod data;
pub mod refresh; pub mod refresh;
pub mod token; pub mod token;
pub mod session;
pub mod notify; pub mod notify;
pub mod notify_read; pub mod notify_read;
pub mod reply; pub mod reply;
pub mod reply_link; pub mod reply_link;
pub mod ascii; pub mod describe;
pub mod timeline_author;
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
@ -26,7 +29,7 @@ fn main() {
.command( .command(
Command::new("ai") Command::new("ai")
.alias("a") .alias("a")
.action(c_ascii_art) .action(ascii_art)
.flag( .flag(
Flag::new("type", FlagType::String) Flag::new("type", FlagType::String)
.description("type flag") .description("type flag")
@ -35,9 +38,9 @@ fn main() {
) )
.command( .command(
Command::new("token") Command::new("token")
.alias("t") .alias("l")
.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") .description("$ ai l yui.syui.ai -p password\n\t\t\t$ ai t yui.syui.ai -p password -s bsky.social")
.action(c_token) .action(token)
.flag( .flag(
Flag::new("password", FlagType::String) Flag::new("password", FlagType::String)
.description("password flag") .description("password flag")
@ -52,36 +55,156 @@ fn main() {
.command( .command(
Command::new("refresh") Command::new("refresh")
.alias("r") .alias("r")
.description("refresh\n\t\t\t$ ai r\n\t\t\t$ ai r -s bsky.social") .description("$ ai r\n\t\t\t$ ai r -s bsky.social")
.action(c_refresh) .action(refresh),
.flag(
Flag::new("server", FlagType::String)
.description("server flag")
.alias("s"),
)
) )
.command( .command(
Command::new("notify") Command::new("notify")
.alias("n") .alias("n")
.description("notify\n\t\t\t$ ai n") .description("$ ai n")
.action(c_notify), .action(notify),
) )
.command(
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 <cid>\n\t\t\t$ ai like <cid> -u <uri>")
// .action(c_like)
// .flag(
// Flag::new("uri", FlagType::String)
// .alias("u"),
// )
// )
//.command(
// Command::new("repost")
// .description("$ ai repost <cid>\n\t\t\t$ ai repost <cid> -u <uri>")
// .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 <text> -c <cid> -u <uri> -i <img> -t <title> -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(
Command::new("bot") Command::new("bot")
.alias("b") .alias("b")
.description("bot\n\t\t\t$ ai b\n\t\t\t$ ai b -s bsky.social") .description("bot\n\t\t\t$ ai b\n\t\t\t$ ai b -s bsky.social")
.action(c_bot) .action(bot),
.flag(
Flag::new("server", FlagType::String)
.description("server flag")
.alias("s"),
)
) )
; ;
app.run(args); app.run(args);
} }
fn c_ascii_art(c: &Context) { fn ascii_art(c: &Context) {
if let Ok(t) = c.string_flag("type") { if let Ok(t) = c.string_flag("type") {
c_ascii(&t); c_ascii(&t);
} else { } else {
@ -106,28 +229,24 @@ fn token(c: &Context) {
return res return res
} }
fn c_token(c: &Context) { fn refresh(_c: &Context) {
token(c); let server = data_toml(&"host");
}
fn refresh(c: &Context) {
let h = async { let h = async {
let session = session::get_request().await;
if session == "err" {
let res = refresh::post_request().await; let res = refresh::post_request().await;
if let Ok(s) = c.string_flag("server") { println!("{}", res);
w_cfg(&s, &res) w_cfg(&server, &res)
} else { } else {
w_cfg("bsky.social", &res) println!("no refresh");
} }
}; };
let res = tokio::runtime::Runtime::new().unwrap().block_on(h); let res = tokio::runtime::Runtime::new().unwrap().block_on(h);
return res return res
} }
fn c_refresh(c: &Context) { fn notify(c: &Context) {
refresh(c); refresh(c);
}
fn notify() {
let h = async { let h = async {
let j = notify::get_request(100).await; let j = notify::get_request(100).await;
println!("{}", j); println!("{}", j);
@ -136,23 +255,42 @@ fn notify() {
return res return res
} }
fn c_notify(_c: &Context) { fn did(c: &Context) {
notify(); 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 h = async {
let server = "bsky.social";
let mut notify = notify::get_request(100).await; let mut notify = notify::get_request(100).await;
if notify == "err" { if notify == "err" {
if let Ok(s) = c.string_flag("server") { refresh(c);
let res = refresh::post_request().await;
w_cfg(&s, &res);
} else {
let res = refresh::post_request().await;
w_cfg(&server, &res);
}
notify = notify::get_request(100).await; notify = notify::get_request(100).await;
} }
let notify: Notify = serde_json::from_str(&notify).unwrap(); let notify: Notify = serde_json::from_str(&notify).unwrap();
@ -214,8 +352,8 @@ fn bot(c: &Context) {
} }
fn c_bot(c: &Context) { fn bot(c: &Context) {
loop { loop {
bot(c); c_bot(c);
} }
} }

29
src/session.rs Normal file
View File

@ -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
}
}
}

28
src/timeline_author.rs Normal file
View File

@ -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
}