createmodlist optimizations

This commit is contained in:
LordMZTE 2021-09-08 16:25:26 +02:00
parent d423765b51
commit 6d44b60182

View file

@ -9,9 +9,6 @@ use addonscript::manifest::{
RepositoryType,
};
use anyhow::Context;
use crossterm::style::Stylize;
use futures::{stream, StreamExt};
use indicatif::ProgressBar;
use reqwest::Client;
use serde::Serialize;
use std::{collections::HashMap, path::PathBuf, sync::Arc};
@ -21,7 +18,7 @@ use twitch::api::AddonInfoResponse;
const TEMPLATE: &str = include_str!("../../assets/modlist.html.tera");
pub async fn run(
(config, mut manifest): (Config, Manifest),
(_config, mut manifest): (Config, Manifest),
outfile: PathBuf,
) -> anyhow::Result<()> {
let version = manifest
@ -35,83 +32,59 @@ pub async fn run(
let http = Arc::new(Client::new());
let len = version.relations.len();
let mut futures = stream::iter(version.relations.into_iter())
.map(|rel| get_meta(rel, &repos))
.buffer_unordered(config.downloads.max_threads);
println!("{}", "Resolving metas.".info());
let mut metas = vec![];
let pb = ProgressBar::new(len as u64)
.with_style(util::progress_style())
.with_prefix("Resolving metadata");
let mut cf_rels = vec![];
while let Some(mi) = futures.next().await {
pb.inc(1);
let mi = mi?;
for rel in version.relations {
let mi = get_meta(rel, &repos)?;
match mi {
MetaInfo::Meta(meta) => {
pb.println(format!(
"{} {}",
"Got meta for".green(),
AsRef::<str>::as_ref(&meta.name).cyan().bold()
));
metas.push(meta);
},
MetaInfo::CfId(id) => {
pb.println(format!(
"{} {}",
"Found curseforge artifact with id".green(),
id.to_string().cyan().bold()
));
cf_rels.push(id);
},
MetaInfo::Meta(meta) => metas.push(meta),
MetaInfo::CfId(id) => cf_rels.push(id),
}
}
pb.finish();
println!("{}", "Querying CF metas.".info());
if !cf_rels.is_empty() {
println!("{}", "Querying CF metas.".info());
let res = http
.post("https://addons-ecs.forgesvc.net/api/v2/addon")
.body(
serde_json::to_string(&cf_rels)
.context("Failed to serialize curseforge relation IDs")?,
)
.header("Content-Type", "application/json")
.send()
.await
.context("Failed sending CF relation request")?
.bytes()
.await
.context("Failed getting CF relation response body")?;
let res = http
.post("https://addons-ecs.forgesvc.net/api/v2/addon")
.body(
serde_json::to_string(&cf_rels)
.context("Failed to serialize curseforge relation IDs")?,
)
.header("Content-Type", "application/json")
.send()
.await
.context("Failed sending CF relation request")?
.bytes()
.await
.context("Failed getting CF relation response body")?;
let cf_metas = serde_json::from_slice::<Vec<AddonInfoResponse>>(&res)
.context("Failed deserializing CF relation response")?;
let cf_metas = serde_json::from_slice::<Vec<AddonInfoResponse>>(&res)
.context("Failed deserializing CF relation response")?;
let cf_metas = cf_metas.into_iter().map(|m| Meta {
name: m.name,
contributors: m
.authors
.into_iter()
.map(|a| Contributor {
name: a.name,
roles: vec!["author".into()],
})
.collect(),
description: Some(m.summary),
icon_url: m
.attachments
.into_iter()
.find(|a| a.is_default)
.map(|a| a.url.to_string()),
website_url: Some(m.website_url),
});
let cf_metas = cf_metas.into_iter().map(|m| Meta {
name: m.name,
contributors: m
.authors
.into_iter()
.map(|a| Contributor {
name: a.name,
roles: vec!["author".into()],
})
.collect(),
description: Some(m.summary),
icon_url: m
.attachments
.into_iter()
.find(|a| a.is_default)
.map(|a| a.url.to_string()),
website_url: Some(m.website_url),
});
metas.extend(cf_metas);
metas.extend(cf_metas);
}
metas.sort_by_key(|m| m.name.to_ascii_lowercase());
@ -134,7 +107,7 @@ pub async fn run(
Ok(())
}
async fn get_meta(rel: Relation, repos: &HashMap<String, Repository>) -> anyhow::Result<MetaInfo> {
fn get_meta(rel: Relation, repos: &HashMap<String, Repository>) -> anyhow::Result<MetaInfo> {
if let Some(meta) = rel.meta {
return Ok(MetaInfo::Meta(meta));
}