Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy issues, remove some dep #452

Merged
merged 6 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
Telegram RSS bot [@RustRssBot](http://t.me/RustRssBot)

**Supports:**
- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

## Usage

Expand All @@ -25,13 +26,13 @@ Telegram RSS bot [@RustRssBot](http://t.me/RustRssBot)

## Download

The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). Make sure to use the english binary (`rssbot-en-amd64-linux`). The Linux version is statically linked to *musl*, no other dependencies required.
The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). Make sure to use the english binary (`rssbot-en-amd64-linux`). The Linux version is statically linked to _musl_, no other dependencies required.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the style


## Compile

**Please try to download from the Link above, if that's not feasible or you have other requirements you should compile manually**

Install *Rust Nightly* and *Cargo* ([`rustup` recommended](https://www.rustup.rs/)) first, then:
Install _Rust Nightly_ and _Cargo_ ([`rustup` recommended](https://www.rustup.rs/)) first, then:

```
LOCALE=en cargo build --release
Expand All @@ -58,7 +59,7 @@ OPTIONS:
multiple times to allow multiple admins
--api-uri <tgapi-uri> Custom telegram api URI [default: https://api.telegram.org/]
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2M]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]

Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

**Other Languages:** [English](README.en.md)

Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot)
Telegram RSS 机器人  [@RustRssBot](http://t.me/RustRssBot)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 space?


**支持:**
- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

## 使用

Expand All @@ -25,13 +26,13 @@ Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot)

## 下载

可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 *musl* 静态链接, 无需其他依赖
可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 _musl_ 静态链接, 无需其他依赖

## 编译

**请先尝试从上面下载, 如不可行或者有其他需求再手动编译**

先安装 *Rust Nightly* 以及 *Cargo* (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后:
先安装 _Rust Nightly_ 以及 _Cargo_ (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后:

```
cargo build --release
Expand All @@ -56,7 +57,7 @@ OPTIONS:
multiple times to allow multiple admins
--api-uri <tgapi-uri> Custom telegram api URI [default: https://api.telegram.org/]
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2M]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]

Expand Down
45 changes: 39 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::PathBuf;
use std::process;
use std::sync::Arc;

use anyhow::Context;
use anyhow::{anyhow, Context};
use hyper_proxy::{Intercept, Proxy};
use std::sync::OnceLock;
use structopt::StructOpt;
Expand Down Expand Up @@ -67,10 +67,10 @@ pub struct Opt {
)]
// default is 12 hours
max_interval: u32,
/// Maximum feed size, 0 is unlimited
#[structopt(long, value_name = "bytes", default_value = "2097152")]
// default is 2MiB
max_feed_size: u64,
/// Maximum feed size, 0 is unlimited.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the docstring that will show up in --help, don't add .

#[structopt(long, value_name = "bytes", default_value = "2M")]
// Default is 2M.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2M and 2MiB are different

max_feed_size: String,
/// Private mode, only specified user can use this bot.
/// This argument can be passed multiple times to allow multiple admins
#[structopt(
Expand Down Expand Up @@ -105,6 +105,22 @@ fn check_interval(s: String) -> Result<(), String> {
})
}

/// Parse human readable size into bytes.
fn parse_human_size(s: &str) -> anyhow::Result<u64> {
const BASE: u64 = 1024;
let s = s.trim().trim_end_matches(|x| x == 'B' || x == 'b');
match s.chars().last().map(|x| x.to_ascii_lowercase()) {
Some('b') => Ok(s[..s.len() - 1].parse()?),
Some('k') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE),
Some('m') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(2)),
Some('g') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(3)),
Some('t') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(4)),
Some(x) if x.is_ascii_digit() => Ok(s.parse()?),
Some(x) => Err(anyhow!("invalid size character: {}", x)),
None => Err(anyhow!("empty size")),
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
enable_fail_fast();
Expand All @@ -125,7 +141,11 @@ async fn main() -> anyhow::Result<()> {
.context("Initialization failed, check your network and Telegram token")?;

let bot_name = me.user.username.clone().unwrap();
crate::client::init_client(&bot_name, opt.insecure, opt.max_feed_size);
crate::client::init_client(
&bot_name,
opt.insecure,
parse_human_size(&opt.max_feed_size).context("Invalid max_feed_size")?,
);

BOT_NAME.set(bot_name).unwrap();
BOT_ID.set(me.user.id).unwrap();
Expand Down Expand Up @@ -173,3 +193,16 @@ fn print_error<E: std::error::Error>(err: E) {
.show_backtrace(true)
);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_human_size() {
assert_eq!(parse_human_size("2M").unwrap(), 2_097_152);
assert_eq!(parse_human_size("2G").unwrap(), 2_147_483_648);
assert_eq!(parse_human_size("2mb").unwrap(), 2_097_152);
assert_eq!(parse_human_size("2097152").unwrap(), 2_097_152);
}
}