Skip to content

Commit 0327550

Browse files
committed
Added fetch-segment
1 parent cbd8515 commit 0327550

File tree

4 files changed

+100
-34
lines changed

4 files changed

+100
-34
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ You can also specify an output file as with `fetch`.
3939
cargo run -- -o builds.json fetch-latest 20 # Fetches the twenty latest public builds and saves them to builds.json
4040
```
4141

42+
### Get a batch of build orders
43+
44+
```Bash
45+
cargo run fetch-segment 141 145 # Fetches build orders 141-145 (both included)
46+
```
47+
4248
## Roadmap
4349

4450
* [ ] Better CLI interface, with progress bars, styling and more detailed progress

src/handlers.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::build_order::{BuildOrder, BuildOrderError};
2+
use crate::build_parser::fetch_build_order;
3+
use crate::index_manager::{LOWEST_INDEX, get_st_highest_index};
4+
5+
pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
6+
let highest_index = get_st_highest_index();
7+
let mut end_index = if count > highest_index - LOWEST_INDEX {
8+
LOWEST_INDEX
9+
} else {
10+
highest_index - count + 1
11+
};
12+
let mut increment = 0;
13+
let mut current_id = highest_index;
14+
let mut build_orders = Vec::new();
15+
while current_id >= end_index && build_orders.len() < count as usize {
16+
current_id = highest_index - increment;
17+
match fetch_build_order(current_id) {
18+
Ok(build_order) => {
19+
build_orders.push(build_order);
20+
increment += 1;
21+
}
22+
Err(e) => {
23+
if e.eq(&BuildOrderError::Cloaked) {
24+
eprintln!("Build order {} is cloaked, skipping.", current_id);
25+
end_index = if end_index > LOWEST_INDEX {
26+
end_index - 1
27+
} else {
28+
LOWEST_INDEX
29+
} // Decrease end_index to compensate for the skipped build
30+
} else {
31+
eprintln!("Error fetching build order {}: {}", current_id, e);
32+
}
33+
increment += 1; // Increment to avoid infinite loop
34+
continue; // Some builds might not be available, continue fetching
35+
}
36+
}
37+
}
38+
build_orders
39+
}
40+
41+
pub fn fetch_segment(start: u32, end: u32) -> Vec<BuildOrder> {
42+
let highest_index = get_st_highest_index();
43+
let start = if start < LOWEST_INDEX {
44+
LOWEST_INDEX
45+
} else {
46+
start
47+
};
48+
let end = if end > highest_index {
49+
highest_index
50+
} else {
51+
end
52+
};
53+
54+
if start > end {
55+
eprintln!(
56+
"Start index {} is greater than end index {}. Returning empty segment.",
57+
start, end
58+
);
59+
return Vec::new();
60+
}
61+
62+
let mut build_orders = Vec::new();
63+
for id in (start..=end).rev() {
64+
match fetch_build_order(id) {
65+
Ok(build_order) => build_orders.push(build_order),
66+
Err(e) => eprintln!("Error fetching build order {}: {}", id, e),
67+
}
68+
}
69+
build_orders
70+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod build_order;
22
pub mod build_parser;
33
pub mod build_regex;
4+
pub mod handlers;
45
pub mod http_client;
56
pub mod index_manager;

src/main.rs

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use build_warren::index_manager::{LOWEST_INDEX, get_st_highest_index};
2-
use build_warren::{build_order::BuildOrderError, build_parser::fetch_build_order};
1+
use build_warren::build_parser::fetch_build_order;
2+
use build_warren::handlers::{fetch_latest, fetch_segment};
3+
use build_warren::index_manager::get_st_highest_index;
34
use clap::{Parser, Subcommand};
45
use serde_json;
56
use std::fs;
@@ -31,6 +32,14 @@ enum Commands {
3132
#[arg(default_value_t = 1)]
3233
count: u32,
3334
},
35+
36+
/// Fetch a segment of build orders
37+
FetchSegment {
38+
/// The starting index of the segment
39+
start: u32,
40+
/// The ending index of the segment
41+
end: u32,
42+
},
3443
}
3544

3645
fn main() {
@@ -60,38 +69,7 @@ fn main() {
6069
Err(e) => eprintln!("Error fetching build order: {}", e),
6170
},
6271
Some(Commands::FetchLatest { count }) => {
63-
let highest_index = get_st_highest_index();
64-
let mut end_index = if *count > highest_index - LOWEST_INDEX {
65-
LOWEST_INDEX
66-
} else {
67-
highest_index - *count + 1
68-
};
69-
let mut increment = 0;
70-
let mut current_id = highest_index;
71-
let mut build_orders = Vec::new();
72-
while current_id >= end_index && build_orders.len() < *count as usize {
73-
current_id = highest_index - increment;
74-
match fetch_build_order(current_id) {
75-
Ok(build_order) => {
76-
build_orders.push(build_order);
77-
increment += 1;
78-
}
79-
Err(e) => {
80-
if e.eq(&BuildOrderError::Cloaked) {
81-
eprintln!("Build order {} is cloaked, skipping.", current_id);
82-
end_index = if end_index > LOWEST_INDEX {
83-
end_index - 1
84-
} else {
85-
LOWEST_INDEX
86-
} // Decrease end_index to compensate for the skipped build
87-
} else {
88-
eprintln!("Error fetching build order {}: {}", current_id, e);
89-
}
90-
increment += 1; // Increment to avoid infinite loop
91-
continue; // Some builds might not be available, continue fetching
92-
}
93-
}
94-
}
72+
let build_orders = fetch_latest(*count);
9573
let json_output = serde_json::to_string_pretty(&build_orders)
9674
.expect("Failed to serialize build orders to JSON");
9775
if let Some(output_file) = &cli.output {
@@ -104,5 +82,16 @@ fn main() {
10482
None => {
10583
eprintln!("No command provided. Use --help to see available commands.");
10684
}
85+
Some(Commands::FetchSegment { start, end }) => {
86+
let build_orders = fetch_segment(*start, *end);
87+
let json_output = serde_json::to_string_pretty(&build_orders)
88+
.expect("Failed to serialize build orders to JSON");
89+
if let Some(output_file) = &cli.output {
90+
fs::write(output_file, json_output)
91+
.expect("Failed to write build orders to output file");
92+
} else {
93+
println!("{}", json_output);
94+
}
95+
}
10796
}
10897
}

0 commit comments

Comments
 (0)