Add option for generating mod files

This commit is contained in:
Tyler Hallada 2022-01-24 01:24:15 -05:00
parent e7fe7abfb7
commit 4a8e3cad6c
3 changed files with 74 additions and 1 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
.env
plugins.zip
plugins
cells
cells
mods

View File

@ -51,6 +51,10 @@ struct Args {
/// folder to output all cell data as json files
#[argh(option, short = 'c')]
cell_data: Option<String>,
/// folder to output all mod data as json files
#[argh(option, short = 'm')]
mod_data: Option<String>,
}
async fn extract_with_compress_tools(
@ -243,6 +247,26 @@ pub async fn main() -> Result<()> {
return Ok(());
}
if let Some(mod_data_dir) = args.mod_data {
let page_size = 20;
let mut page = 0;
loop {
let mods = game_mod::batched_get_with_cells(&pool, page_size, page).await?;
if mods.is_empty() {
break;
}
for mod_with_cells in mods {
let path = std::path::Path::new(&mod_data_dir);
std::fs::create_dir_all(&path)?;
let path = path.join(format!("{}.json", mod_with_cells.nexus_mod_id));
let mut file = std::fs::File::create(path)?;
write!(file, "{}", serde_json::to_string(&mod_with_cells)?)?;
}
page += 1;
}
return Ok(());
}
let mut page = args.page;
let mut has_next_page = true;

View File

@ -42,6 +42,26 @@ pub struct UnsavedMod<'a> {
pub first_upload_at: NaiveDateTime,
}
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct ModWithCells {
pub id: i32,
pub name: String,
pub nexus_mod_id: i32,
pub author_name: String,
pub author_id: i32,
pub category_name: Option<String>,
pub category_id: Option<i32>,
pub description: Option<String>,
pub thumbnail_link: Option<String>,
pub game_id: i32,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub last_update_at: NaiveDateTime,
pub first_upload_at: NaiveDateTime,
pub last_updated_files_at: Option<NaiveDateTime>,
pub cells: Option<serde_json::Value>,
}
#[instrument(level = "debug", skip(pool))]
pub async fn get_by_nexus_mod_id(
pool: &sqlx::Pool<sqlx::Postgres>,
@ -297,3 +317,31 @@ pub async fn update_from_api_response<'a>(
Ok(ret)
}
#[instrument(level = "debug", skip(pool))]
pub async fn batched_get_with_cells(
pool: &sqlx::Pool<sqlx::Postgres>,
page_size: i64,
page: i64,
) -> Result<Vec<ModWithCells>> {
let offset = page_size * page;
sqlx::query_as!(
ModWithCells,
"SELECT
mods.*,
json_agg(cells.*) AS cells
FROM mods
JOIN files ON files.mod_id = mods.id
JOIN plugins ON plugins.file_id = files.id
JOIN plugin_cells ON plugin_cells.plugin_id = plugins.id
JOIN cells ON cells.id = plugin_cells.cell_id
GROUP BY mods.id
LIMIT $1
OFFSET $2",
page_size,
offset,
)
.fetch_all(pool)
.await
.context("Failed to batch get with cells")
}