Skip to content

Commit

Permalink
Update global value
Browse files Browse the repository at this point in the history
Signed-off-by: Lameur <[email protected]>
  • Loading branch information
Lameur committed Feb 17, 2025
1 parent 28a1499 commit b30fd0f
Showing 1 changed file with 8 additions and 95 deletions.
103 changes: 8 additions & 95 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
continue;
}

// Check for updates based on the source (CurseForge or Modrinth)
// Check for updates based on the source (Modrinth only)
match check_for_update(&mod_entry).await {
Ok(Some(new_version)) if new_version != mod_entry.version => {
println!(
Expand Down Expand Up @@ -75,7 +75,9 @@ async fn check_for_update(mod_entry: &ModEntry) -> Result<Option<String>, Box<dy
if is_modrinth_url(&mod_entry.url) {
check_for_update_on_modrinth(&mod_entry).await
} else if is_curseforge_url(&mod_entry.url) {
check_for_update_on_curseforge(&mod_entry).await
// Skip CurseForge mods since we don't have an API key
println!("Skipping {} (CurseForge mod, no API key)", mod_entry.name);
Ok(None)
} else {
Err("URL non reconnue (doit être CurseForge ou Modrinth)".into())
}
Expand Down Expand Up @@ -113,45 +115,14 @@ async fn check_for_update_on_modrinth(
Ok(None)
}

/// Checks for updates for a given mod on CurseForge.
async fn check_for_update_on_curseforge(
mod_entry: &ModEntry,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
let curseforge_id = extract_curseforge_id(&mod_entry.url)?;

// Fetch the latest version from CurseForge API
let client = reqwest::Client::new();
let response = client
.get(&format!(
"https://api.curseforge.com/v1/mods/{}",
curseforge_id
))
.header("x-api-key", "YOUR_CURSEFORGE_API_KEY") // Replace with your CurseForge API key
.send()
.await?;

if response.status().is_success() {
let json: serde_json::Value = response.json().await?;

// Get the latest version
if let Some(latest_version) = json["data"]["latestFiles"].as_array().and_then(|v| v.first()) {
let latest_version_number = latest_version["displayName"]
.as_str()
.unwrap_or("")
.to_string();
return Ok(Some(latest_version_number));
}
}

Ok(None)
}

/// Downloads a mod from the appropriate source (CurseForge or Modrinth).
/// Downloads a mod from the appropriate source (Modrinth only).
async fn download_mod(mod_entry: &ModEntry) -> Result<(), Box<dyn std::error::Error>> {
if is_modrinth_url(&mod_entry.url) {
download_from_modrinth(&mod_entry).await
} else if is_curseforge_url(&mod_entry.url) {
download_from_curseforge(&mod_entry).await
// Skip CurseForge mods since we don't have an API key
println!("Skipping {} (CurseForge mod, no API key)", mod_entry.name);
Ok(())
} else {
Err("URL non reconnue (doit être CurseForge ou Modrinth)".into())
}
Expand Down Expand Up @@ -203,53 +174,6 @@ async fn download_from_modrinth(mod_entry: &ModEntry) -> Result<(), Box<dyn std:
Err("Impossible de récupérer les informations de la version".into())
}

/// Downloads a mod from CurseForge.
async fn download_from_curseforge(mod_entry: &ModEntry) -> Result<(), Box<dyn std::error::Error>> {
let curseforge_id = extract_curseforge_id(&mod_entry.url)?;

// Fetch the latest version download URL from CurseForge API
let client = reqwest::Client::new();
let response = client
.get(&format!(
"https://api.curseforge.com/v1/mods/{}/files",
curseforge_id
))
.header("x-api-key", "YOUR_CURSEFORGE_API_KEY") // Replace with your CurseForge API key
.send()
.await?;

if response.status().is_success() {
let json: serde_json::Value = response.json().await?;

// Get the latest version's download URL
if let Some(latest_version) = json["data"].as_array().and_then(|v| v.first()) {
let download_url = latest_version["downloadUrl"]
.as_str()
.ok_or("URL de téléchargement introuvable")?;

// Download the mod file
let response = reqwest::get(download_url).await?;
if !response.status().is_success() {
return Err(format!("Échec du téléchargement: {}", response.status()).into());
}

let bytes = response.bytes().await?;
let mods_dir = PathBuf::from("mods");
if !mods_dir.exists() {
fs::create_dir(&mods_dir)?;
}

let mod_path = mods_dir.join(&mod_entry.filename);
fs::write(&mod_path, bytes)?;

println!("{} téléchargé avec succès depuis CurseForge!", mod_entry.name);
return Ok(());
}
}

Err("Impossible de récupérer les informations de la version".into())
}

/// Extracts the Modrinth project ID or slug from the URL.
fn extract_modrinth_id(url: &str) -> Result<String, Box<dyn std::error::Error>> {
// Example URL: https://modrinth.com/mod/ai-improvements
Expand All @@ -261,17 +185,6 @@ fn extract_modrinth_id(url: &str) -> Result<String, Box<dyn std::error::Error>>
}
}

/// Extracts the CurseForge project ID from the URL.
fn extract_curseforge_id(url: &str) -> Result<String, Box<dyn std::error::Error>> {
// Example URL: https://www.curseforge.com/minecraft/mc-mods/ai-improvements
let parts: Vec<&str> = url.trim_end_matches('/').split('/').collect();
if parts.len() >= 2 && parts[parts.len() - 2] == "mc-mods" {
Ok(parts[parts.len() - 1].to_string())
} else {
Err("URL de CurseForge invalide".into())
}
}

/// Checks if a URL is a Modrinth URL.
fn is_modrinth_url(url: &str) -> bool {
url.contains("modrinth.com")
Expand Down

0 comments on commit b30fd0f

Please sign in to comment.