Certainly! Below is a comprehensive Rust program that accomplishes the task you've described. This program:
- Reads a list of
placeIDs.
- For each
placeID:
- Reads the corresponding
index.txt file.
- Parses each line to extract
versionNumber and cdnURL.
- Downloads the asset from the
cdnURL.
- Saves the asset in the specified directory structure.
The program leverages asynchronous programming with the Tokio runtime and the Reqwest library for efficient and concurrent downloading.
Prerequisites
-
Rust Installation: Ensure you have Rust installed. If not, download it from rust-lang.org.
-
Create a New Cargo Project:
cargo new roblox_cdn_downloader
cd roblox_cdn_downloader
-
Add Dependencies:
Update your Cargo.toml to include the necessary dependencies.
[package]
name = "roblox_cdn_downloader"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json", "stream"] }
futures = "0.3"
- tokio: Asynchronous runtime for Rust.
- reqwest: HTTP client for making requests.
- futures: Utilities for asynchronous computations.
Complete main.rs Code
Replace the contents of your src/main.rs with the following code:
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::fs::{create_dir_all, File};
use tokio::io::AsyncWriteExt;
use reqwest::Client;
use futures::stream::{self, StreamExt};
#[tokio::main]
async fn main() {
// Base directory where the downloads will be saved
let base_dir = Path::new(r"C:\Users\Lukas\Downloads\lkjhgfd");
// List of placeIDs
let place_ids = vec![
"10789565990",
"11181971058",
"11181982436",
"11182034319",
"11182037286",
"11263735303",
"11263873956",
"12647442701",
"33564694",
"4568576845",
"4737314314",
"4748963513",
"4956810680",
"4998492434",
"5000291183",
"5063342777",
"5229799451",
"5400427674",
"5465723678",
"5914914682",
"5972110179",
"6087359275",
"6701464352",
"6767882272",
"6768240609",
"6783491168",
"7162161171",
"7730658758",
"9048939285",
];
// Initialize HTTP client with timeout and other configurations
let client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.expect("Failed to build HTTP client");
// Process each placeID concurrently with a limit on concurrency
let concurrency_limit = 5; // Adjust based on your network capability
stream::iter(place_ids)
.for_each_concurrent(concurrency_limit, |place_id| {
let client = client.clone();
let base_dir = base_dir.to_path_buf();
async move {
if let Err(e) = process_place_id(&client, &base_dir, place_id).await {
eprintln!("Error processing placeID {}: {}", place_id, e);
}
}
})
.await;
println!("All downloads completed.");
}
/// Processes a single placeID:
/// - Reads index.txt
/// - Parses entries
/// - Downloads assets
async fn process_place_id(client: &Client, base_dir: &PathBuf, place_id: &str) -> Result<(), Box<dyn std::error::Error>> {
println!("Processing placeID: {}", place_id);
// Path to index.txt
let index_path = base_dir.join(place_id).join("index.txt");
// Read index.txt content
let index_content = match fs::read_to_string(&index_path) {
Ok(content) => content,
Err(e) => {
eprintln!("Failed to read {}: {}", index_path.display(), e);
return Err(Box::new(e));
}
};
// Parse index.txt lines
let assets: Vec<(String, String)> = index_content
.lines()
.filter_map(|line| {
let parts: Vec<&str> = line.split('|').map(|s| s.trim()).collect();
if parts.len() == 2 {
Some((parts[0].to_string(), parts[1].to_string()))
} else {
None
}
})
.collect();
println!("Found {} assets for placeID {}", assets.len(), place_id);
// Define concurrency for asset downloads per placeID
let asset_concurrency = 10; // Adjust as needed
// Process asset downloads concurrently
stream::iter(assets)
.for_each_concurrent(asset_concurrency, |(version, url)| {
let client = client.clone();
let base_dir = base_dir.clone();
let place_id = place_id.to_string();
async move {
if let Err(e) = download_asset(&client, &base_dir, &place_id, &version, &url).await {
eprintln!("Failed to download asset {} for placeID {}: {}", url, place_id, e);
}
}
})
.await;
println!("Completed placeID: {}", place_id);
Ok(())
}
/// Downloads a single asset and saves it to the designated path
async fn download_asset(
client: &Client,
base_dir: &PathBuf,
place_id: &str,
version: &str,
url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Extract asset filename from URL
let asset_filename = match url.split('/').last() {
Some(name) => name,
None => {
return Err("Failed to extract asset filename from URL".into());
}
};
// Define the target directory and file path
let target_dir = base_dir.join(place_id).join(version);
let target_path = target_dir.join(asset_filename);
// Create directories if they don't exist
create_dir_all(&target_dir).await?;
// Check if the file already exists to avoid re-downloading
if target_path.exists() {
println!("Asset already exists: {:?}", target_path);
return Ok(());
}
// Make the HTTP GET request
let response = client.get(url).send().await?;
if !response.status().is_success() {
return Err(format!("Failed to download {}: HTTP {}", url, response.status()).into());
}
// Stream the response bytes to the file
let mut file = File::create(&target_path).await?;
let mut stream = response.bytes_stream();
while let Some(chunk) = stream.next().await {
let data = chunk?;
file.write_all(&data).await?;
}
println!("Downloaded: {:?}", target_path);
Ok(())
}
Explanation of the Code
-
Imports:
tokio::fs and tokio::io: For asynchronous file operations.
reqwest::Client: For making HTTP requests.
futures::stream: For handling asynchronous streams and controlling concurrency.
-
Main Function:
- Defines the base directory where all downloads will be saved.
- Lists all the
placeIDs.
- Initializes an HTTP client with a timeout.
- Processes each
placeID concurrently, limited by concurrency_limit to prevent overwhelming the network or server.
-
Processing Each placeID:
- Reads the corresponding
index.txt.
- Parses each line to extract
versionNumber and cdnURL.
- Downloads all assets concurrently for that
placeID, limited by asset_concurrency.
-
Downloading Each Asset:
- Extracts the asset filename from the
cdnURL.
- Constructs the target directory and file path.
- Checks if the asset already exists to avoid redundant downloads.
- Makes an HTTP GET request to download the asset.
- Streams the downloaded bytes directly to the file asynchronously.
-
Concurrency Control:
- Uses
for_each_concurrent from the futures crate to limit the number of concurrent tasks both at the placeID level and the asset download level.
-
Error Handling:
- Comprehensive error handling is implemented to catch and report issues like missing
index.txt, failed downloads, etc., without crashing the entire program.
-
Progress Reporting:
- The program prints messages to the console to inform about the progress, such as when it starts processing a
placeID, when it completes, and when each asset is downloaded.
Running the Program
-
Build the Project:
cargo build --release
-
Run the Executable:
cargo run --release
Ensure that the base_dir specified in the code (C:\Users\Lukas\Downloads\lkjhgfd) exists. If it doesn't, you can create it or modify the base_dir variable to point to a desired location.
Notes and Recommendations
-
Adjusting Concurrency:
- The
concurrency_limit and asset_concurrency variables control how many placeIDs and asset downloads are processed in parallel, respectively. Adjust these based on your system's capabilities and network bandwidth.
-
Extending Functionality:
- Retries: Implement retry logic for failed downloads to handle transient network issues.
- Logging: Integrate a logging library like
env_logger for more sophisticated logging capabilities.
- Configuration: Use command-line arguments or a configuration file to specify parameters like
base_dir, concurrency_limit, etc., for increased flexibility.
-
Error Logging:
- Currently, errors are printed to the standard error. For persistent logging, consider writing errors to a log file.
-
Platform Compatibility:
- The provided path is Windows-specific (
C:\Users\Lukas\Downloads\lkjhgfd). To make the program cross-platform, you can use environment variables or configuration files to set the base download directory dynamically.
Conclusion
This Rust program provides an efficient and concurrent way to download assets from Roblox Asset CDN URLs based on provided placeIDs and their respective index.txt files. With proper concurrency management and error handling, it ensures that the downloading process is both fast and reliable.
Feel free to extend or modify the program according to your specific needs!