Skip to content

Commit

Permalink
Merge pull request #2 from gfx/gihyojp-bot
Browse files Browse the repository at this point in the history
add a bot for gihyo.jp
  • Loading branch information
gfx authored Dec 25, 2023
2 parents ed54abb + c27c1c6 commit 3b53368
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/dpz-bot.yml → .github/workflows/bot.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "DPZ Bot"
name: "The Bots"

on:
# run it on every 7 minutes
Expand Down Expand Up @@ -27,9 +27,18 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('**/rust-toolchain.toml') }}

- run: cargo build
- run: cargo run
- name: "Run for DPZ"
run: cargo run
env:
RSS_URL: https://dailyportalz.jp/feed/headline
BSKY_ID: ${{ secrets.BSKY_DBZ_BOT_ID }}
BSKY_PASSWORD: ${{ secrets.BSKY_DPZ_BOT_PASSWORD }}
MAX_BSKY_POSTS: 1
MAX_BSKY_POSTS: 10

- name: "Run for Gihyo.jp"
run: cargo run
env:
RSS_URL: https://gihyo.jp/feed/rss2
BSKY_ID: ${{ secrets.BSKY_GIHYO_BOT_ID }}
BSKY_PASSWORD: ${{ secrets.BSKY_GIHYO_BOT_PASSWORD }}
MAX_BSKY_POSTS: 10
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ Bluesky (https://bsky.app) 用のRSS botです。

* Site: https://dailyportalz.jp/
* RSS: https://dailyportalz.jp/feed/headline
* workflow: https://github.com/gfx/bluesky-rss-bot/blob/main/.github/workflows/dpz-bot.yml
* workflow: https://github.com/gfx/bluesky-rss-bot/blob/main/.github/workflows/bot.yml
* Bluesky: https://bsky.app/profile/dpz-unofficial-bot.bsky.social

## Gihyo.jp

* Site: https://gihyo.jp/
* RSS: https://gihyo.jp/feed/rss2
* workflow: https://github.com/gfx/bluesky-rss-bot/blob/main/.github/workflows/bot.yml
* Bluesky: https://bsky.app/profile/gihyo-unofficial.bsky.social

## License

MIT License. See [LICENSE](./LICENSE) for details.
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn get_bsky_posts<S: SessionStore + Send + Sync, T: XrpcClient + Send + Sy
actor: session.handle.clone(),
filter: Some("posts_no_replies".into()),
cursor: None,
limit: Some(10),
limit: Some(100),
})
.await?
.feed;
Expand All @@ -56,6 +56,8 @@ async fn find_new_entries(
) -> Result<Vec<Entry>, Box<dyn std::error::Error>> {
let mut new_entries = vec![];

let mut num_entries = 100;

for entry in feed.entries {
let mut found = false;
for post in &posts {
Expand All @@ -77,6 +79,11 @@ async fn find_new_entries(
break;
} else {
new_entries.push(entry);

num_entries -= 1;
if num_entries <= 0 {
break;
}
}
}

Expand Down Expand Up @@ -153,6 +160,11 @@ async fn post_entry<S: SessionStore + Send + Sync, T: XrpcClient + Send + Sync>(
.link(&*url)
.build();

let created_at = entry
.published
.unwrap_or_else(|| chrono::Local::now().into())
.to_rfc3339();

println!("Posting entry {:?} {:?}", text, embed);

let result = agent
Expand All @@ -167,7 +179,7 @@ async fn post_entry<S: SessionStore + Send + Sync, T: XrpcClient + Send + Sync>(
text,
embed,
facets: Some(facets),
created_at: chrono::Local::now().to_rfc3339(),
created_at,
entities: None,
labels: None,
langs: None,
Expand Down Expand Up @@ -195,11 +207,11 @@ async fn post_entries<S: SessionStore + Send + Sync, T: XrpcClient + Send + Sync
) -> Result<(), Box<dyn std::error::Error>> {
let mut count = 0;
for entry in entries.into_iter().rev() {
count += 1;
if count > max_bsky_posts {
break;
}
post_entry(agent, session, entry).await?;
count += 1;
}

Ok(())
Expand Down

0 comments on commit 3b53368

Please sign in to comment.