https://github.com/nubilfi/botagent
Detect bot user agents using regular expressions.
https://github.com/nubilfi/botagent
backend regex rustlang user-agent
Last synced: 4 months ago
JSON representation
Detect bot user agents using regular expressions.
- Host: GitHub
- URL: https://github.com/nubilfi/botagent
- Owner: nubilfi
- License: mit
- Created: 2024-08-21T08:22:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T03:11:30.000Z (almost 2 years ago)
- Last Synced: 2025-12-27T02:29:29.073Z (6 months ago)
- Topics: backend, regex, rustlang, user-agent
- Language: Rust
- Homepage: https://crates.io/crates/botagent
- Size: 41 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# botagent
[](https://crates.io/crates/botagent)
[](https://github.com/nubilfi/botagent/actions?branch=main)
[](https://docs.rs/botagent/latest/botagent/)
[](https://codecov.io/gh/nubilfi/botagent)
**botagent** is a Rust library for detecting bot user agents using regular expressions. It reads patterns from a JSON file, compiles them into a regex, and checks user agents against these patterns.
## Features
- **Bot Detection**: Identify whether a given user agent string matches known bot patterns.
- **Customizable**: Use your own bot patterns by providing a JSON file.
- **Efficient**: Uses the `pcre2` crate for high-performance regex matching.
- **Error Handling**: Robust error handling with detailed messages.
## Installation
To use this library in your project, add the following to your `Cargo.toml`:
```toml
[dependencies]
botagent = "0.1"
```
Or by run `cargo add botagent` command.
## Usage
### Check if User Agent is a Bot
To check if a given user agent string matches any known bot patterns:
```rust
use botagent::is_bot;
fn main() {
let is_bot = is_bot("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "patterns.json").unwrap();
println!("Is bot: {}", is_bot);
}
```
### Find the Matching Bot Pattern
If you want to know which bot pattern matched the user agent:
```rust
use botagent::is_bot_match;
fn main() {
let matched_pattern = is_bot_match("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "patterns.json").unwrap();
if let Some(pattern) = matched_pattern {
println!("Matched bot pattern: {}", pattern);
} else {
println!("No bot pattern matched.");
}
}
```
## Patterns File Format
The bot patterns are stored in a JSON file, with each pattern being a regular expression string. Here is an example `patterns.json`:
```json
[
"(?