refactor: implement fetchall with generic type
This commit is contained in:
@@ -232,124 +232,16 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
|
||||
#[cfg(feature = "fetchall")]
|
||||
Commands::ListAllUser { concurrency } => {
|
||||
use futures_util::StreamExt;
|
||||
use sdgb_api::bincode::borrow_decode_from_slice;
|
||||
use std::io::{self, BufRead};
|
||||
use sdgb_api::title::methods::GetUserPreviewApi;
|
||||
|
||||
let mut user_ids = Vec::new();
|
||||
{
|
||||
let mut stdin = io::stdin().lock();
|
||||
let mut buf = String::new();
|
||||
|
||||
while stdin.read_line(&mut buf).is_ok_and(|size| size != 0) {
|
||||
if buf.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let user_id: u32 = buf.trim().parse()?;
|
||||
buf.clear();
|
||||
user_ids.push(user_id);
|
||||
}
|
||||
}
|
||||
|
||||
let _ = cache::init_db();
|
||||
let read = cache::read_txn()?;
|
||||
let write = cache::write_txn()?;
|
||||
let config = sdgb_api::bincode::config::Configuration::<
|
||||
sdgb_api::bincode::config::LittleEndian,
|
||||
>::default()
|
||||
.with_no_limit();
|
||||
|
||||
info!("number of user_id: {}", user_ids.len());
|
||||
|
||||
let collect = futures_util::stream::iter(user_ids)
|
||||
.map(async |user_id| {
|
||||
{
|
||||
let cache_table = cache::open_table_read(&read)?;
|
||||
let data = cache_table.get(user_id)?;
|
||||
if let Some(data) = data {
|
||||
let decoded: (GetUserPreviewApiResp, _) =
|
||||
borrow_decode_from_slice(&data.value(), config)?;
|
||||
|
||||
return Ok(decoded.0);
|
||||
}
|
||||
}
|
||||
|
||||
if EARLY_QUIT.load(Ordering::Relaxed) {
|
||||
return Err("early skip due to ctrl-c")?;
|
||||
}
|
||||
|
||||
let resp = Sdgb1_50::request::<_, GetUserPreviewApiResp>(
|
||||
&client,
|
||||
APIMethod::GetUserPreviewApi,
|
||||
user_id,
|
||||
GetUserPreviewApi { user_id },
|
||||
)
|
||||
.await;
|
||||
|
||||
match &resp {
|
||||
Ok(resp) => {
|
||||
use sdgb_api::bincode::encode_to_vec;
|
||||
|
||||
info!("found: {user_id}");
|
||||
|
||||
if let Ok(mut table) = cache::open_table(&write)
|
||||
&& let Ok(encoded) = encode_to_vec(resp, config)
|
||||
{
|
||||
_ = table.insert(resp.user_id, encoded);
|
||||
}
|
||||
}
|
||||
Err(sdgb_api::ApiError::JSON { .. }) => {}
|
||||
Err(e) => {
|
||||
error!("preview failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
Result::<_, Box<dyn snafu::Error>>::Ok(resp?)
|
||||
})
|
||||
.buffer_unordered(concurrency) // slower to avoid being banned
|
||||
.filter_map(async |r| r.ok())
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
drop(collect);
|
||||
|
||||
let _ = write.commit();
|
||||
use crate::{cache::PLAYERS, utils::helpers::cached_concurrent_fetch};
|
||||
cached_concurrent_fetch::<GetUserPreviewApi>(&client, concurrency, PLAYERS).await?;
|
||||
}
|
||||
#[cfg(feature = "fetchall")]
|
||||
Commands::ListAllUserDump { .. } => {
|
||||
use std::{fs::OpenOptions, io::BufWriter};
|
||||
Commands::ListAllUserDump {} => {
|
||||
use crate::{cache::PLAYERS, utils::helpers::dump_cache};
|
||||
|
||||
use redb::ReadableTable;
|
||||
use sdgb_api::bincode::{self, borrow_decode_from_slice};
|
||||
|
||||
use crate::cache::{open_table_read, read_txn};
|
||||
|
||||
let file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open("players.json")?;
|
||||
|
||||
#[cfg(file_lock_ready)]
|
||||
file.try_lock()?;
|
||||
|
||||
let txn = read_txn()?;
|
||||
let table = open_table_read(&txn)?;
|
||||
|
||||
let config = bincode::config::Configuration::<bincode::config::LittleEndian>::default()
|
||||
.with_no_limit();
|
||||
|
||||
let user_ids = table
|
||||
.iter()?
|
||||
.flatten()
|
||||
.map(|d| borrow_decode_from_slice(&d.1.value(), config))
|
||||
.flatten()
|
||||
.map(|(value, _)| value)
|
||||
.collect::<Vec<GetUserPreviewApiResp>>();
|
||||
|
||||
let writer = BufWriter::new(file);
|
||||
serde_json::to_writer(writer, &user_ids)?;
|
||||
info!("dumped {} user id", user_ids.len());
|
||||
dump_cache::<GetUserPreviewApiResp>("players.json", PLAYERS)?;
|
||||
}
|
||||
|
||||
Commands::Userdata { user_id } => {
|
||||
|
||||
Reference in New Issue
Block a user