From 84efc31248649fd3e70ac0acae31ee7865bd9881 Mon Sep 17 00:00:00 2001 From: syui Date: Sun, 18 Aug 2024 01:28:04 +0900 Subject: [PATCH] add feed watch --- .config/ai/scpt | 2 +- src/feed_watch.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 25 ++++++++++++++++ test/ai.zsh | 4 +++ test/feed.zsh | 5 ++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/feed_watch.rs create mode 100644 test/feed.zsh diff --git a/.config/ai/scpt b/.config/ai/scpt index 7c80e30..9cbca76 160000 --- a/.config/ai/scpt +++ b/.config/ai/scpt @@ -1 +1 @@ -Subproject commit 7c80e30b3ed3bb2badc220e8fda3a28c55323ea9 +Subproject commit 9cbca76fc7846495894b254881cde1f7dc0ae363 diff --git a/src/feed_watch.rs b/src/feed_watch.rs new file mode 100644 index 0000000..cb115b0 --- /dev/null +++ b/src/feed_watch.rs @@ -0,0 +1,75 @@ +use seahorse::Context; + +//use crate::openai; +use crate::feed_get; + +use crate::data::data_toml; +use crate::data::Timeline; + +pub fn c_feed_watch(c: &Context) { + let mut feed = "at://did:plc:4hqjfn7m6n5hno3doamuhgef/app.bsky.feed.generator/cmd".to_string(); + if c.string_flag("url").is_ok() { + feed = c.string_flag("url").unwrap(); + } + let mut tag = "syai".to_string(); + if c.string_flag("tag").is_ok() { + tag = c.string_flag("tag").unwrap(); + } + + let h = async { + let notify = feed_get::get_request(feed).await; + if notify == "err" { + return; + //refresh(c); + //notify = feed_get::get_request("at://did:plc:4hqjfn7m6n5hno3doamuhgef/app.bsky.feed.generator/cmd".to_string()).await; + } + let timeline: Timeline = serde_json::from_str(¬ify).unwrap(); + let n = timeline.feed; + let host = data_toml(&"host"); + let length = &n.len(); + let su = 0..*length; + for i in su { + let handle = &n[i].post.author.handle; + let did = &n[i].post.author.did; + let cid = &n[i].post.cid; + let uri = &n[i].post.uri; + let _time = &n[i].post.indexedAt; + let cid_root = cid; + let uri_root = uri; + + let mut text = ""; + if !n[i].post.record.text.is_none() { + text = &n[i].post.record.text.as_ref().unwrap(); + } + + let vec: Vec<&str> = text.split_whitespace().collect(); + let com = vec[0].trim().to_string(); + let mut prompt = "".to_string(); + let mut prompt_sub = "".to_string(); + + if com == "@ai" || com == "/ai" || com == tag { + prompt_sub = vec[1..].join(" "); + } else { + prompt = vec[1..].join(" "); + if vec.len() > 1 { + prompt_sub = vec[2..].join(" "); + } + } + + if prompt.is_empty() == false || com.is_empty() == false { + println!("{}", handle); + + if c.bool_flag("debug") == true { + println!( + "cid:{}\nuri:{}\ncid_root:{}\nuri_root:{}\nhost:{}\ndid:{}", + cid, uri, cid_root, uri_root, host, did + ); + } + println!("{}", prompt_sub); + println!("---"); + } + } + }; + let res = tokio::runtime::Runtime::new().unwrap().block_on(h); + return res; +} diff --git a/src/main.rs b/src/main.rs index 31130ad..169cfad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use crate::data::data_refresh; use crate::data::url; use crate::data::w_cfg; use crate::data::w_refresh; +use crate::feed_watch::c_feed_watch; use data::ProfileIdentityResolve; @@ -39,6 +40,7 @@ pub mod session; pub mod timeline_author; pub mod token; pub mod feed_get; +pub mod feed_watch; pub mod delete_record; fn main() { @@ -70,6 +72,22 @@ fn main() { .alias("f"), ) ) + .command( + Command::new("feed_watch") + .action(feed_watch) + .flag( + Flag::new("url", FlagType::String) + .alias("u"), + ) + .flag( + Flag::new("tag", FlagType::String) + .alias("t"), + ) + .flag( + Flag::new("debug", FlagType::Bool) + .alias("d"), + ) + ) .command( Command::new("follow_all") .action(follow_all), @@ -301,6 +319,13 @@ fn bot(c: &Context) { } } +fn feed_watch(c: &Context) { + refresh(c); + loop { + c_feed_watch(c); + } +} + fn follow_all(_c: &Context) { c_follow_all(); } diff --git a/test/ai.zsh b/test/ai.zsh index 7988af9..dcf9b3d 100755 --- a/test/ai.zsh +++ b/test/ai.zsh @@ -16,6 +16,7 @@ source $d/reply.zsh source $d/notify.zsh source $d/notify_cid.zsh source $d/cron.zsh +source $d/feed.zsh case $1 in refresh|r) @@ -36,4 +37,7 @@ case $1 in cid) cid ;; + feed) + feed + ;; esac diff --git a/test/feed.zsh b/test/feed.zsh new file mode 100644 index 0000000..a714046 --- /dev/null +++ b/test/feed.zsh @@ -0,0 +1,5 @@ +function feed(){ + token=`cat ~/.config/ai/token.json|jq -r .accessJwt` + url=at://did:plc:4hqjfn7m6n5hno3doamuhgef/app.bsky.feed.generator/cmd + curl -sL "https://public.api.bsky.app/xrpc/app.bsky.feed.getFeed?feed=$url" -H "Authorization: Bearer $token" +}