Skip to content

Commit 3452461

Browse files
author
dkhodyriev
committed
Updates
1 parent 81a9cf7 commit 3452461

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/target
2-
/data/.
3-
data/mydatabase.db-shm
4-
data/mydatabase.db-wal
2+
/data
3+
/.idea

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ What you need to install the software:
3030
1. **Clone the repository**
3131

3232
```bash
33-
git clone https://github.com/yourusername/rust-web-scraper.git
34-
cd rust-web-scraper
33+
git clone git@github.com:dhodyrev/async-rust-scraper.git
34+
cd async-rust-scraper

data/mydatabase.db

20 KB
Binary file not shown.

src/main.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use sqlx::SqlitePool;
12
use dotenv::dotenv;
23
use env_logger::Env;
3-
use std::env;
4+
use std::{env, time::Duration};
45
use tokio::main;
56

67
mod config;
@@ -11,24 +12,39 @@ mod utils;
1112
use crate::scraper::client::fetch_data;
1213
use db::operations::insert_data;
1314
use sqlx::sqlite::SqlitePoolOptions;
15+
use utils::rate_limiter::rate_limited_operation;
1416

15-
async fn fetch_and_insert_data(pool: &sqlx::SqlitePool, base_url: &str, pages: usize) -> Result<(), Box<dyn std::error::Error>> {
17+
async fn fetch_and_insert_data(pool: &SqlitePool, base_url: &str, pages: usize) -> Result<(), Box<dyn std::error::Error>> {
1618
let mut tasks = vec![];
1719

1820
for page in 1..=pages {
1921
let url = format!("{}/page/{}", base_url, page);
2022
let pool = pool.clone();
2123

2224
let task = tokio::spawn(async move {
23-
match fetch_data(&url).await {
24-
Ok(data) => {
25-
if let Err(err) = insert_data(&pool, &data).await {
26-
log::error!("Error inserting data from {}: {}", url, err);
27-
} else {
28-
log::info!("Successfully inserted data from {}", url);
29-
}
25+
let result = rate_limited_operation(|| async {
26+
match fetch_data(&url).await {
27+
Ok(data) => {
28+
match insert_data(&pool, &data).await {
29+
Ok(_) => {
30+
log::info!("Successfully inserted data from {}", url);
31+
Ok(())
32+
},
33+
Err(err) => {
34+
log::error!("Error inserting data from {}: {}", url, err);
35+
Err(err)
36+
},
37+
}
38+
},
39+
Err(err) => {
40+
log::error!("Error fetching data from {}: {}", url, err);
41+
Err(sqlx::Error::Protocol(err.to_string()))
42+
},
3043
}
31-
Err(err) => log::error!("Error fetching data from {}: {}", url, err),
44+
}, Duration::from_secs(1)).await;
45+
46+
if let Err(err) = result {
47+
log::error!("Error in rate limited operation: {}", err);
3248
}
3349
});
3450

src/utils/rate_limiter.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// utils/rate_limiter.rs
21
use tokio::time::{sleep, Duration};
32

4-
pub async fn rate_limited_operation<F, Fut>(operation: F, delay: Duration)
3+
pub async fn rate_limited_operation<F, Fut>(operation: F, delay: Duration) -> Result<(), sqlx::Error>
54
where
65
F: Fn() -> Fut,
7-
Fut: std::future::Future<Output = ()>,
6+
Fut: std::future::Future<Output = Result<(), sqlx::Error>>,
87
{
9-
operation().await;
8+
let result = operation().await;
109
sleep(delay).await;
10+
result
1111
}
12-

0 commit comments

Comments
 (0)