1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#[cfg(target_arch = "wasm32")]
fn main() {}
#[cfg(not(target_arch = "wasm32"))]
mod cli {
use clap::{Parser, Subcommand, ValueEnum};
use storelib_rs::{DeviceFamily, DisplayCatalogHandler, IdentifierType};
#[derive(Parser)]
#[command(name = "storelib_rs", about = "Microsoft Store API client", version)]
pub struct Cli {
/// Logging verbosity (default: info)
#[arg(long, global = true, default_value = "info", value_name = "LEVEL")]
pub log_level: LogLevel,
#[command(subcommand)]
pub command: Command,
}
#[derive(ValueEnum, Clone)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}
impl From<LogLevel> for log::LevelFilter {
fn from(l: LogLevel) -> Self {
match l {
LogLevel::Error => log::LevelFilter::Error,
LogLevel::Warn => log::LevelFilter::Warn,
LogLevel::Info => log::LevelFilter::Info,
LogLevel::Debug => log::LevelFilter::Debug,
LogLevel::Trace => log::LevelFilter::Trace,
}
}
}
#[derive(Subcommand)]
pub enum Command {
/// Fetch direct download URLs for a product's packages
Packages {
/// Product ID (or other ID type, see --type)
id: String,
/// MSA authentication token
#[arg(long)]
token: Option<String>,
/// Identifier type
#[arg(long = "type", default_value = "product-id")]
id_type: IdType,
},
/// Query detailed product information
Query {
/// The product identifier
id: String,
/// Identifier type
#[arg(long = "type", default_value = "product-id")]
id_type: IdType,
/// MSA authentication token (for sandboxed/flighted listings)
#[arg(long)]
token: Option<String>,
},
/// Search the store catalog
Search {
/// Search query string
query: String,
/// Target device family
#[arg(long, default_value = "desktop")]
family: Family,
/// Number of results to skip (for pagination)
#[arg(long, default_value_t = 0)]
skip: u32,
},
}
#[derive(ValueEnum, Clone)]
pub enum IdType {
ProductId,
Pfn,
ContentId,
XboxTitleId,
LegacyPhone,
LegacyStore,
LegacyXbox,
}
impl From<IdType> for IdentifierType {
fn from(t: IdType) -> Self {
match t {
IdType::ProductId => IdentifierType::ProductId,
IdType::Pfn => IdentifierType::PackageFamilyName,
IdType::ContentId => IdentifierType::ContentId,
IdType::XboxTitleId => IdentifierType::XboxTitleId,
IdType::LegacyPhone => IdentifierType::LegacyWindowsPhoneProductId,
IdType::LegacyStore => IdentifierType::LegacyWindowsStoreProductId,
IdType::LegacyXbox => IdentifierType::LegacyXboxProductId,
}
}
}
#[derive(ValueEnum, Clone)]
pub enum Family {
Desktop,
Mobile,
Xbox,
Universal,
Holographic,
Iot,
Server,
Andromeda,
Wcos,
}
impl From<Family> for DeviceFamily {
fn from(f: Family) -> Self {
match f {
Family::Desktop => DeviceFamily::Desktop,
Family::Mobile => DeviceFamily::Mobile,
Family::Xbox => DeviceFamily::Xbox,
Family::Universal => DeviceFamily::Universal,
Family::Holographic => DeviceFamily::HoloLens,
Family::Iot => DeviceFamily::IotCore,
Family::Server => DeviceFamily::ServerCore,
Family::Andromeda => DeviceFamily::Andromeda,
Family::Wcos => DeviceFamily::Wcos,
}
}
}
pub async fn run(cli: Cli) {
match cli.command {
Command::Packages { id, token, id_type } => {
log::info!("Command: packages id={id}");
let mut handler = DisplayCatalogHandler::production();
if let Err(e) = handler.query_dcat(&id, id_type.into(), None).await {
log::error!("Error querying product: {e}");
return;
}
match handler.get_packages_for_product(token.as_deref()).await {
Ok(pkgs) if pkgs.is_empty() => log::info!("No packages found."),
Ok(pkgs) => {
log::info!("Found {} package(s):", pkgs.len());
for pkg in &pkgs {
log::info!(" {} [{:?}]", pkg.package_moniker, pkg.package_type);
if let Some(uri) = &pkg.package_uri {
log::info!(" URL: {uri}");
}
}
}
Err(e) => log::error!("Error fetching packages: {e}"),
}
}
Command::Query { id, id_type, token } => {
log::info!("Command: query id={id}");
let mut handler = DisplayCatalogHandler::production();
match handler
.query_dcat(&id, id_type.into(), token.as_deref())
.await
{
Ok(_) => {
let listing = handler.product_listing.as_ref().unwrap();
let product = listing
.products
.as_deref()
.and_then(|v| v.first())
.or(listing.product.as_ref());
match product {
Some(p) => {
let title = p
.localized_properties
.as_deref()
.and_then(|v| v.first())
.and_then(|lp| lp.product_title.as_deref())
.unwrap_or("<no title>");
let kind = p.product_kind.as_deref().unwrap_or("<unknown>");
let pfn = p
.properties
.as_ref()
.and_then(|pr| pr.package_family_name.as_deref())
.unwrap_or("<none>");
log::info!("Title: {title}");
log::info!("Kind: {kind}");
log::info!("PFN: {pfn}");
}
None => log::info!("Product found but no details available."),
}
}
Err(e) => log::error!("Error: {e}"),
}
}
Command::Search {
query,
family,
skip,
} => {
log::info!("Command: search query=\"{query}\"");
let mut handler = DisplayCatalogHandler::production();
let result = if skip > 0 {
handler.search_dcat_paged(&query, family.into(), skip).await
} else {
handler.search_dcat(&query, family.into()).await
};
match result {
Ok(results) => {
log::info!("Total results: {}", results.total_result_count.unwrap_or(0));
if let Some(groups) = &results.results {
for group in groups {
let fam =
group.product_family_name.as_deref().unwrap_or("<unknown>");
log::info!(" Family: {fam}");
if let Some(products) = &group.products {
for p in products.iter().take(10) {
let title = p
.localized_properties
.as_deref()
.and_then(|v| v.first())
.and_then(|lp| lp.product_title.as_deref())
.unwrap_or("<no title>");
log::info!(" - {title}");
}
}
}
}
}
Err(e) => log::error!("Search error: {e}"),
}
}
}
}
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() {
use clap::Parser;
let cli = cli::Cli::parse();
env_logger::Builder::new()
.filter_level(cli.log_level.clone().into())
.parse_default_env() // RUST_LOG still overrides --log-level
.format_timestamp_millis()
.init();
log::debug!("storelib_rs starting");
cli::run(cli).await;
}