feat: scrape all player record
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use bincode::Decode;
|
||||||
|
use bincode::Encode;
|
||||||
use music_db::query_music;
|
use music_db::query_music;
|
||||||
use music_db::query_music_level;
|
use music_db::query_music_level;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -31,7 +33,7 @@ pub struct UserMusic {
|
|||||||
pub length: u32,
|
pub length: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct UserMusicDetail {
|
pub struct UserMusicDetail {
|
||||||
pub music_id: u32,
|
pub music_id: u32,
|
||||||
|
|||||||
1
sdgb-cli/src/cache/mod.rs
vendored
1
sdgb-cli/src/cache/mod.rs
vendored
@@ -13,6 +13,7 @@ static DATABASE: LazyLock<redb::Database> = LazyLock::new(|| {
|
|||||||
|
|
||||||
pub const PLAYERS: TableDefinition<'_, u32, Vec<u8>> = redb::TableDefinition::new("players");
|
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_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> {
|
pub fn write_txn() -> Result<WriteTransaction, redb::Error> {
|
||||||
Ok(DATABASE.begin_write()?)
|
Ok(DATABASE.begin_write()?)
|
||||||
|
|||||||
@@ -94,6 +94,17 @@ pub enum Commands {
|
|||||||
#[arg(long, default_value_t = 16500)]
|
#[arg(long, default_value_t = 16500)]
|
||||||
max_rating: i64,
|
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")]
|
#[cfg(feature = "fetchall")]
|
||||||
ListAllUserDump {},
|
ListAllUserDump {},
|
||||||
#[cfg(feature = "fetchall")]
|
#[cfg(feature = "fetchall")]
|
||||||
|
|||||||
@@ -231,6 +231,29 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
|||||||
.await?;
|
.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")]
|
#[cfg(feature = "fetchall")]
|
||||||
Commands::ScrapeAllB50 {
|
Commands::ScrapeAllB50 {
|
||||||
concurrency,
|
concurrency,
|
||||||
|
|||||||
@@ -10,10 +10,7 @@ use serde::Serialize;
|
|||||||
use spdlog::{error, info};
|
use spdlog::{error, info};
|
||||||
|
|
||||||
use sdgb_api::title::MaiVersionExt;
|
use sdgb_api::title::MaiVersionExt;
|
||||||
use sdgb_api::title::{
|
use sdgb_api::title::{Sdgb1_50, methods::APIExt};
|
||||||
Sdgb1_50,
|
|
||||||
methods::{APIExt, HasUid},
|
|
||||||
};
|
|
||||||
use sdgb_api::{ApiError, bincode};
|
use sdgb_api::{ApiError, bincode};
|
||||||
|
|
||||||
use bincode::{BorrowDecode, Encode, borrow_decode_from_slice};
|
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>>
|
) -> Result<(), Box<dyn snafu::Error>>
|
||||||
where
|
where
|
||||||
A::Payload: From<u32>,
|
A::Payload: From<u32>,
|
||||||
A::Response: Encode + for<'a> BorrowDecode<'a, ()> + HasUid,
|
A::Response: Encode + for<'a> BorrowDecode<'a, ()>,
|
||||||
{
|
{
|
||||||
cached_concurrent_fetch_userfn(
|
cached_concurrent_fetch_userfn(
|
||||||
user_ids,
|
user_ids,
|
||||||
@@ -109,7 +106,7 @@ pub async fn cached_concurrent_fetch_userfn<R>(
|
|||||||
scrape: impl AsyncFn(&AsyncClient, u32) -> Result<R, ApiError>,
|
scrape: impl AsyncFn(&AsyncClient, u32) -> Result<R, ApiError>,
|
||||||
) -> Result<(), Box<dyn snafu::Error>>
|
) -> Result<(), Box<dyn snafu::Error>>
|
||||||
where
|
where
|
||||||
R: Encode + for<'a> BorrowDecode<'a, ()> + HasUid,
|
R: Encode + for<'a> BorrowDecode<'a, ()>,
|
||||||
{
|
{
|
||||||
let _ = cache::init_db();
|
let _ = cache::init_db();
|
||||||
|
|
||||||
@@ -150,7 +147,7 @@ where
|
|||||||
if let Ok(mut table) = cache::open_table(&write, definition)
|
if let Ok(mut table) = cache::open_table(&write, definition)
|
||||||
&& let Ok(encoded) = encode_to_vec(resp, config)
|
&& let Ok(encoded) = encode_to_vec(resp, config)
|
||||||
{
|
{
|
||||||
_ = table.insert(resp.get_uid(), encoded);
|
_ = table.insert(user_id, encoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(sdgb_api::ApiError::JSON { .. }) => {}
|
Err(sdgb_api::ApiError::JSON { .. }) => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user