feat: scrape all player record

This commit is contained in:
mokurin000
2025-08-03 17:54:31 +08:00
parent 9a6e414793
commit 81c8f21729
5 changed files with 42 additions and 8 deletions

View File

@@ -13,6 +13,7 @@ static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
pub const PLAYERS: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("players");
pub const PLAYER_B50: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("b50");
pub const PLAYER_RECORD: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("records");
pub fn write_txn() -> Result<WriteTransaction, redb::Error> {
Ok(DATABASE.begin_write()?)

View File

@@ -94,6 +94,17 @@ pub enum Commands {
#[arg(long, default_value_t = 16500)]
max_rating: i64,
},
#[cfg(feature = "fetchall")]
ScrapeAllRecord {
#[arg(short, long, default_value_t = 5)]
concurrency: usize,
#[arg(long, default_value_t = 10000)]
min_rating: i64,
#[arg(long, default_value_t = 16400)]
max_rating: i64,
},
#[cfg(feature = "fetchall")]
ListAllUserDump {},
#[cfg(feature = "fetchall")]

View File

@@ -231,6 +231,29 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
.await?;
}
#[cfg(feature = "fetchall")]
Commands::ScrapeAllRecord {
concurrency,
min_rating,
max_rating,
} => {
use crate::{
cache::{PLAYER_RECORD, PLAYERS},
utils::helpers::{cached_concurrent_fetch_userfn, read_cache},
};
let mut players: Vec<GetUserPreviewApiResp> = read_cache(PLAYERS)?;
players.retain(|p| p.player_rating >= min_rating && p.player_rating <= max_rating);
cached_concurrent_fetch_userfn(
players.iter().map(|p| p.user_id).collect::<Vec<u32>>(),
&client,
concurrency,
PLAYER_RECORD,
get_user_all_music,
)
.await?;
}
#[cfg(feature = "fetchall")]
Commands::ScrapeAllB50 {
concurrency,

View File

@@ -10,10 +10,7 @@ use serde::Serialize;
use spdlog::{error, info};
use sdgb_api::title::MaiVersionExt;
use sdgb_api::title::{
Sdgb1_50,
methods::{APIExt, HasUid},
};
use sdgb_api::title::{Sdgb1_50, methods::APIExt};
use sdgb_api::{ApiError, bincode};
use bincode::{BorrowDecode, Encode, borrow_decode_from_slice};
@@ -87,7 +84,7 @@ pub async fn cached_concurrent_fetch<A: APIExt>(
) -> Result<(), Box<dyn snafu::Error>>
where
A::Payload: From<u32>,
A::Response: Encode + for<'a> BorrowDecode<'a, ()> + HasUid,
A::Response: Encode + for<'a> BorrowDecode<'a, ()>,
{
cached_concurrent_fetch_userfn(
user_ids,
@@ -109,7 +106,7 @@ pub async fn cached_concurrent_fetch_userfn<R>(
scrape: impl AsyncFn(&AsyncClient, u32) -> Result<R, ApiError>,
) -> Result<(), Box<dyn snafu::Error>>
where
R: Encode + for<'a> BorrowDecode<'a, ()> + HasUid,
R: Encode + for<'a> BorrowDecode<'a, ()>,
{
let _ = cache::init_db();
@@ -150,7 +147,7 @@ where
if let Ok(mut table) = cache::open_table(&write, definition)
&& let Ok(encoded) = encode_to_vec(resp, config)
{
_ = table.insert(resp.get_uid(), encoded);
_ = table.insert(user_id, encoded);
}
}
Err(sdgb_api::ApiError::JSON { .. }) => {}