added nexus mod downloads
This commit is contained in:
@@ -17,4 +17,6 @@ pub enum Commands {
|
||||
Activate { instance: String, target: PathBuf },
|
||||
Add { instance: String, mod_id: String },
|
||||
LoadOrder { instance: String },
|
||||
ApiCheck,
|
||||
Download { url: String },
|
||||
}
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@@ -1,12 +1,13 @@
|
||||
use anyhow::anyhow;
|
||||
use clap::Parser;
|
||||
use log::debug;
|
||||
use log::{debug, error};
|
||||
use std::{error::Error, path::Path};
|
||||
|
||||
use crate::{
|
||||
activator::activate_instance,
|
||||
cli::Args,
|
||||
instance::{files_to_install_mod, insert_mod_to_instance},
|
||||
nexus::{NXMUrl, NexusAPI, download_mod},
|
||||
types::RootConfig,
|
||||
};
|
||||
|
||||
@@ -18,6 +19,7 @@ mod install_prompt;
|
||||
mod instance;
|
||||
mod load_order;
|
||||
mod mod_config_installer;
|
||||
mod nexus;
|
||||
mod types;
|
||||
mod utils;
|
||||
|
||||
@@ -61,10 +63,26 @@ fn command_order(root_config: &RootConfig, instance_id: &str) -> anyhow::Result<
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn command_download(api_key: &str, nxm_url: &str) -> anyhow::Result<()> {
|
||||
let api = NexusAPI::new(api_key);
|
||||
let Some(parsed_url) = NXMUrl::parse_url(nxm_url) else {
|
||||
error!("Failed to parse url");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let links = api.generate_download_link_for_file(&parsed_url)?;
|
||||
|
||||
download_mod(links.first().unwrap(), "./data/mod.7z")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn setup_logger() {
|
||||
env_logger::builder()
|
||||
.filter_level(log::LevelFilter::max())
|
||||
.format_timestamp(None)
|
||||
.filter_module("ureq_proto::util", log::LevelFilter::Debug)
|
||||
.filter_module("rustls::client::hs", log::LevelFilter::Debug)
|
||||
.init();
|
||||
}
|
||||
|
||||
@@ -85,6 +103,13 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
cli::Commands::LoadOrder { instance } => {
|
||||
command_order(&root_config, &instance)?;
|
||||
}
|
||||
cli::Commands::ApiCheck => {
|
||||
let api = NexusAPI::new(root_config.nexus_api_key().unwrap());
|
||||
api.validate_key()?;
|
||||
}
|
||||
cli::Commands::Download { url } => {
|
||||
command_download(root_config.nexus_api_key().unwrap(), &url)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
7
src/nexus.rs
Normal file
7
src/nexus.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod url;
|
||||
mod api;
|
||||
mod downloader;
|
||||
|
||||
pub use url::NXMUrl;
|
||||
pub use api::NexusAPI;
|
||||
pub use downloader::download_mod;
|
||||
66
src/nexus/api.rs
Normal file
66
src/nexus/api.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use serde::Deserialize;
|
||||
use ureq::http::Uri;
|
||||
use url::Url;
|
||||
|
||||
use crate::nexus::NXMUrl;
|
||||
|
||||
const NEXUS_ENDPOINT: &str = "https://api.nexusmods.com";
|
||||
|
||||
pub struct NexusAPI {
|
||||
api_key: String,
|
||||
}
|
||||
|
||||
impl NexusAPI {
|
||||
pub fn new(api_key: &str) -> Self {
|
||||
Self {
|
||||
api_key: api_key.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_key(&self) -> anyhow::Result<()> {
|
||||
let _body = ureq::get(format!("{}/v1/users/validate.json", NEXUS_ENDPOINT))
|
||||
.header("apikey", &self.api_key)
|
||||
.header("accept", "application/json")
|
||||
.call()?
|
||||
.body_mut()
|
||||
.read_to_string()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn generate_download_link_for_file(
|
||||
&self,
|
||||
url: &NXMUrl,
|
||||
) -> anyhow::Result<Vec<DownloadLocation>> {
|
||||
let mut req_url = Url::parse(NEXUS_ENDPOINT)?;
|
||||
|
||||
let path = format!(
|
||||
"/v1/games/{}/mods/{}/files/{}/download_link.json",
|
||||
url.game, url.mod_id, url.file
|
||||
);
|
||||
req_url.set_path(&path);
|
||||
|
||||
req_url
|
||||
.query_pairs_mut()
|
||||
.append_pair("key", &url.key)
|
||||
.append_pair("expires", &url.expires);
|
||||
|
||||
let body: Vec<DownloadLocation> = ureq::get(req_url.to_string())
|
||||
.header("apikey", &self.api_key)
|
||||
.header("accept", "application/json")
|
||||
.call()?
|
||||
.body_mut()
|
||||
.read_json()?;
|
||||
|
||||
Ok(body)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct DownloadLocation {
|
||||
pub name: String,
|
||||
pub short_name: String,
|
||||
#[serde(rename = "URI")]
|
||||
pub uri: String,
|
||||
}
|
||||
|
||||
29
src/nexus/downloader.rs
Normal file
29
src/nexus/downloader.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::{fs::File, io, path::Path};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use log::info;
|
||||
use urlencoding::encode;
|
||||
|
||||
use crate::nexus::api::DownloadLocation;
|
||||
|
||||
pub fn download_mod(
|
||||
mod_dl_link: &DownloadLocation,
|
||||
target: impl AsRef<Path>,
|
||||
) -> anyhow::Result<()> {
|
||||
let encoded = encode(&mod_dl_link.uri).to_string();
|
||||
|
||||
info!("Downloading file from {}", mod_dl_link.name);
|
||||
|
||||
let (response, body) = ureq::get(&encoded).call()?.into_parts();
|
||||
|
||||
if !response.status.is_success() {
|
||||
return Err(anyhow!("Got status {}", response.status));
|
||||
}
|
||||
|
||||
let mut file = File::create(target)?;
|
||||
let mut reader = body.into_reader();
|
||||
|
||||
io::copy(&mut reader, &mut file)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
44
src/nexus/url.rs
Normal file
44
src/nexus/url.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NXMUrl {
|
||||
pub game: String,
|
||||
pub mod_id: String,
|
||||
pub file: String,
|
||||
pub key: String,
|
||||
pub user_id: String,
|
||||
pub expires: String,
|
||||
}
|
||||
|
||||
impl NXMUrl {
|
||||
pub fn parse_url(raw: &str) -> Option<Self> {
|
||||
// Example url
|
||||
//nxm://skyrimspecialedition/mods/266/files/725705?key=xHxcFyx2_i5rhY1YdsSbyA&expires=1773086056&user_id=123456
|
||||
|
||||
let url = Url::parse(raw).ok()?;
|
||||
|
||||
let segs: Vec<&str> = url.path_segments()?.collect();
|
||||
|
||||
let game = url.host_str()?;
|
||||
let mod_id = segs.get(1)?.parse().ok()?;
|
||||
let file_id = segs.get(3)?.parse().ok()?;
|
||||
|
||||
let key = url.query_pairs().find(|(k, _)| k == "key")?.1;
|
||||
let expires = url
|
||||
.query_pairs()
|
||||
.find(|(k, _)| k == "expires")?
|
||||
.1
|
||||
.parse()
|
||||
.ok()?;
|
||||
let user_id = url.query_pairs().find(|(k, _)| k == "user_id")?.1;
|
||||
|
||||
Some(Self {
|
||||
game: game.to_owned(),
|
||||
mod_id,
|
||||
file: file_id,
|
||||
key: key.to_string(),
|
||||
user_id: user_id.parse().ok()?,
|
||||
expires,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user