Skip to content

Commit

Permalink
Add poast.org as nitter backup
Browse files Browse the repository at this point in the history
  • Loading branch information
adumbidiot committed Apr 9, 2024
1 parent 8958cbb commit b79970a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/rss-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
quick-xml = { version = "0.31.0", features = [ "serialize" ] }
reqwest = { version = "0.12.2", default-features = false }
reqwest = { version = "0.12.2", features = [ "http2" ], default-features = false }
serde = { version = "1.0.197", features = ["derive"] }
thiserror = "1.0.58"
tokio = { version = "1.37.0", features = [ "rt" ] }
Expand Down
3 changes: 3 additions & 0 deletions lib/rss-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub use self::model::{
};
use std::time::Duration;

const USER_AGENT_STR: &str = concat!(env!("CARGO_CRATE_NAME"), "/", env!("CARGO_PKG_VERSION"));

/// Library error type
#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -34,6 +36,7 @@ impl Client {
pub fn new() -> Self {
Self {
client: reqwest::Client::builder()
.user_agent(USER_AGENT_STR)
.connect_timeout(Duration::from_secs(10))
.build()
.expect("failed to build client"),
Expand Down
26 changes: 20 additions & 6 deletions src/commands/random_tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rand::{
prelude::SliceRandom,
rngs::OsRng,
};
use rss_client::RssFeed;
use serenity::{
client::Context,
framework::standard::{
Expand All @@ -19,7 +20,7 @@ use std::{
time::Duration,
};

async fn retry<FN, T, E, FU>(mut func: FN, max_tries: u64) -> Result<T, E>
async fn retry<FN, T, E, FU>(mut func: FN, max_tries: u32) -> Result<T, E>
where
FN: FnMut() -> FU,
FU: Future<Output = Result<T, E>>,
Expand All @@ -36,7 +37,7 @@ where
warn!("{error}");

num_try += 1;
tokio::time::sleep(Duration::from_secs(num_try)).await;
tokio::time::sleep(Duration::from_secs(2_u64.pow(num_try))).await;
}
Err(error) => {
break Err(error);
Expand All @@ -45,14 +46,27 @@ where
}
}

async fn get_nitter_feed(
client: &rss_client::Client,
host: &str,
user: &str,
) -> anyhow::Result<RssFeed> {
let url = format!("{host}/{user}/media/rss");
retry(|| client.get_feed(&url), 3)
.await
.with_context(|| format!("failed to get nitter rss feed for \"{user}\" from \"{host}\""))
}

pub async fn get_random_tweet_url(
client: &rss_client::Client,
user: &str,
) -> anyhow::Result<Option<String>> {
let url = format!("https://nitter.privacydev.net/{user}/media/rss");
let feed = retry(|| client.get_feed(&url), 3)
.await
.with_context(|| format!("failed to get nitter rss feed for \"{user}\""))?;
let feed_result = get_nitter_feed(client, "https://nitter.privacydev.net", user).await;

let feed = match feed_result {
Ok(feed) => feed,
Err(_error) => get_nitter_feed(client, "https://nitter.poast.org", user).await?,
};

let entries: Vec<_> = feed
.channel
Expand Down

0 comments on commit b79970a

Please sign in to comment.