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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#![doc = include_str!("../README.md")]
mod errors;
mod parser;
mod subparsers;
pub use crate::errors::{ParseNzbError, ParseNzbFileError};
use crate::parser::{parse_files, parse_metadata, sabnzbd_is_obfuscated, sanitize_xml};
use chrono::{DateTime, Utc};
use flate2::read::GzDecoder;
use itertools::Itertools;
use lazy_regex::regex;
use roxmltree::Document;
use std::fs;
use std::io::Read;
use std::path::Path;
use std::str::FromStr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Represents optional creator-definable metadata in an NZB.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Meta {
pub title: Option<String>,
pub passwords: Vec<String>,
pub tags: Vec<String>,
pub category: Option<String>,
}
impl Meta {
/// Creates a new `Meta` instance.
pub fn new(
title: Option<impl Into<String>>,
passwords: impl IntoIterator<Item = impl Into<String>>,
tags: impl IntoIterator<Item = impl Into<String>>,
category: Option<impl Into<String>>,
) -> Self {
Self {
title: title.map(Into::into),
passwords: passwords.into_iter().map(Into::into).collect(),
tags: tags.into_iter().map(Into::into).collect(),
category: category.map(Into::into),
}
}
}
/// Represents a single segment of a file in an NZB.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Segment {
/// Size of the segment in bytes.
pub size: u32,
/// Number of the segment.
pub number: u32,
/// Message ID of the segment.
pub message_id: String,
}
impl Segment {
/// Creates a new `Segment` instance.
pub fn new(size: impl Into<u32>, number: impl Into<u32>, message_id: impl Into<String>) -> Self {
Self {
size: size.into(),
number: number.into(),
message_id: message_id.into(),
}
}
}
/// Represents a complete file, consisting of segments that make up a file.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct File {
/// The poster of the file.
pub poster: String,
/// The date and time when the file was posted, in UTC.
pub posted_at: DateTime<Utc>,
/// The subject of the file.
pub subject: String,
/// Groups that reference the file.
pub groups: Vec<String>,
/// Segments that make up the file.
pub segments: Vec<Segment>,
}
impl File {
/// Creates a new `File` instance.
pub fn new(
poster: impl Into<String>,
posted_at: impl Into<DateTime<Utc>>,
subject: impl Into<String>,
groups: impl IntoIterator<Item = impl Into<String>>,
segments: impl IntoIterator<Item = Segment>,
) -> Self {
Self {
poster: poster.into(),
posted_at: posted_at.into(),
subject: subject.into(),
groups: groups.into_iter().map(Into::into).collect(),
segments: segments.into_iter().collect(),
}
}
/// Size of the file calculated from the sum of segment sizes.
pub fn size(&self) -> u64 {
self.segments.iter().map(|x| u64::from(x.size)).sum::<u64>()
}
/// Complete name of the file with it's extension extracted from the subject.
/// May return [`None`] if it fails to extract the name.
pub fn name(&self) -> Option<&str> {
subparsers::extract_filename_from_subject(&self.subject)
}
/// Base name of the file without it's extension extracted from the [`File::name`].
/// May return [`None`] if it fails to extract the stem.
pub fn stem(&self) -> Option<&str> {
self.name()
.and_then(|name| Path::new(name).file_stem().and_then(|f| f.to_str().map(|f| f.trim())))
}
/// Extension of the file extracted from the [`File::name`].
/// May return [`None`] if it fails to extract the extension.
pub fn extension(&self) -> Option<&str> {
self.name()
.and_then(|name| Path::new(name).extension().and_then(|f| f.to_str().map(|f| f.trim())))
}
/// Return [`true`] if the file has the specified extension, [`false`] otherwise.
///
/// This method ensures consistent extension comparison
/// by normalizing the extension (removing any leading dot) and handling case-folding.
pub fn has_extension(&self, ext: impl AsRef<str>) -> bool {
let ext = ext.as_ref().strip_prefix(".").unwrap_or_else(|| ext.as_ref()).trim();
self.extension()
.is_some_and(|file_ext| file_ext.eq_ignore_ascii_case(ext))
}
/// Return [`true`] if the file is a `.par2` file, [`false`] otherwise.
pub fn is_par2(&self) -> bool {
let re = regex!(r"\.par2$"i);
self.name().is_some_and(|name| re.is_match(name))
}
/// Return [`true`] if the file is a `.rar` file, [`false`] otherwise.
pub fn is_rar(&self) -> bool {
// https://github.com/sabnzbd/sabnzbd/blob/02b4a116dd4b46b2d2f33f7bbf249f2294458f2e/sabnzbd/nzbstuff.py#L107
let re = regex!(r"(\.rar|\.r\d\d|\.s\d\d|\.t\d\d|\.u\d\d|\.v\d\d)$"i);
self.name().is_some_and(|name| re.is_match(name))
}
/// Return [`true`] if the file is obfuscated, [`false`] otherwise.
pub fn is_obfuscated(&self) -> bool {
if let Some(stem) = self.stem() {
return sabnzbd_is_obfuscated(stem);
}
// Definitely obfuscated if we can't even extract the stem.
true
}
}
/// Represents an NZB.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Nzb {
/// Optional creator-definable metadata for the contents of the NZB.
pub meta: Meta,
/// File objects representing the files included in the NZB.
pub files: Vec<File>,
}
impl FromStr for Nzb {
type Err = ParseNzbError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let xml = sanitize_xml(s);
let nzb = Document::parse(xml)?;
let meta = parse_metadata(&nzb);
let files = parse_files(&nzb)?;
Ok(Self { meta, files })
}
}
impl Nzb {
/// Parses a string into an [`Nzb`] instance.
///
/// # Errors
///
/// This function returns an [`ParseNzbError`] in the following cases:
/// - If the XML is malformed and cannot be parsed.
/// - If the NZB structure is invalid or missing required components.
///
/// # Example
///
/// ```rust
/// use nzb_rs::{ParseNzbError, Nzb};
///
/// fn main() -> Result<(), ParseNzbError> {
/// let xml = r#"
/// <?xml version="1.0" encoding="UTF-8"?>
/// <!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">
/// <nzb
/// xmlns="http://www.newzbin.com/DTD/2003/nzb">
/// <file poster="John <nzb@nowhere.example>" date="1706440708" subject="[1/1] - "Big Buck Bunny - S01E01.mkv" yEnc (1/2) 1478616">
/// <groups>
/// <group>alt.binaries.boneless</group>
/// </groups>
/// <segments>
/// <segment bytes="739067" number="1">9cacde4c986547369becbf97003fb2c5-9483514693959@example</segment>
/// <segment bytes="739549" number="2">70a3a038ce324e618e2751e063d6a036-7285710986748@example</segment>
/// </segments>
/// </file>
/// </nzb>
/// "#;
/// let nzb = Nzb::parse(xml)?;
/// println!("{:#?}", nzb);
/// assert_eq!(nzb.file().name(), Some("Big Buck Bunny - S01E01.mkv"));
/// Ok(())
/// }
/// ```
pub fn parse(nzb: impl AsRef<str>) -> Result<Self, ParseNzbError> {
nzb.as_ref().parse()
}
/// Parse a file into an [`Nzb`] instance.
/// Handles both regular and gzipped NZB files.
///
/// # Errors
///
/// This function returns an [`ParseNzbFileError`] in the following cases:
/// - If the file cannot be read.
/// - If the contents of the file are malformed and cannot be parsed.
///
/// # Example
///
/// ```rust
/// use nzb_rs::{ParseNzbFileError, Nzb};
///
/// fn main() -> Result<(), ParseNzbFileError> {
/// let nzb = Nzb::parse_file("tests/nzbs/big_buck_bunny.nzb")?;
/// println!("{:#?}", nzb);
/// assert_eq!(nzb.file().name(), Some("Big Buck Bunny - S01E01.mkv"));
/// Ok(())
/// }
/// ```
pub fn parse_file(nzb: impl AsRef<Path>) -> Result<Self, ParseNzbFileError> {
let file =
dunce::canonicalize(nzb.as_ref()).map_err(|source| ParseNzbFileError::from_io_err(source, nzb.as_ref()))?;
let content = if file
.extension()
.and_then(|f| f.to_str())
.is_some_and(|f| f.trim().eq_ignore_ascii_case("gz"))
{
let gzipped = fs::read(&file).map_err(|source| ParseNzbFileError::from_gzip_err(source, file.clone()))?;
let mut decoder = GzDecoder::new(&gzipped[..]);
let mut content = String::new();
decoder
.read_to_string(&mut content)
.map_err(|source| ParseNzbFileError::from_gzip_err(source, file))?;
content
} else {
fs::read_to_string(&file).map_err(|source| ParseNzbFileError::from_io_err(source, file.clone()))?
};
Ok(Self::parse(content)?)
}
/// The main content file (episode, movie, etc) in the NZB.
/// This is determined by finding the largest non `par2` file in the NZB
/// and may not always be accurate.
pub fn file(&self) -> &File {
// self.files is guranteed to have atleast one file, so we can safely unwrap().
self.files
.iter()
.filter(|file| !file.is_par2())
.max_by_key(|file| file.size())
.unwrap()
}
/// Total size of all the files in the NZB.
pub fn size(&self) -> u64 {
self.files.iter().map(|file| file.size()).sum()
}
/// Vector of unique file names across all the files in the NZB.
pub fn filenames(&self) -> Vec<&str> {
self.files.iter().filter_map(|f| f.name()).unique().sorted().collect()
}
/// Vector of unique posters across all the files in the NZB.
pub fn posters(&self) -> Vec<&str> {
self.files.iter().map(|f| f.poster.as_str()).unique().sorted().collect()
}
/// Vector of unique groups across all the files in the NZB.
pub fn groups(&self) -> Vec<&str> {
self.files
.iter()
.flat_map(|f| f.groups.iter().map(|f| f.as_str()))
.unique()
.sorted()
.collect()
}
/// Vector of `.par2` files in the NZB.
pub fn par2_files(&self) -> Vec<&File> {
self.files.iter().filter(|f| f.is_par2()).collect()
}
/// Total size of all the `.par2` files.
pub fn par2_size(&self) -> u64 {
self.files
.iter()
.filter_map(|f| if f.is_par2() { Some(f.size()) } else { None })
.sum()
}
/// Percentage of the size of all the `.par2` files relative to the total size.
pub fn par2_percentage(&self) -> f64 {
(self.par2_size() as f64 / self.size() as f64) * 100.0
}
/// Return [`true`] if any file in the NZB has the specified extension, [`false`] otherwise.
///
/// This method ensures consistent extension comparison
/// by normalizing the extension (removing any leading dot) and handling case-folding.
pub fn has_extension(&self, ext: impl AsRef<str>) -> bool {
self.files.iter().any(|f| f.has_extension(ext.as_ref()))
}
/// Return [`true`] if there's at least one `.par2` file in the NZB, [`false`] otherwise.
pub fn has_par2(&self) -> bool {
self.files.iter().any(|file| file.is_par2())
}
/// Return [`true`] if any file in the NZB is a `.rar` file, [`false`] otherwise.
pub fn has_rar(&self) -> bool {
self.files.iter().any(|file| file.is_rar())
}
/// Return [`true`] if every file in the NZB is a `.rar` file, [`false`] otherwise.
pub fn is_rar(&self) -> bool {
self.files.iter().all(|file| file.is_rar())
}
/// Return [`true`] if any file in the NZB is obfuscated, [`false`] otherwise.
pub fn is_obfuscated(&self) -> bool {
self.files.iter().any(|file| file.is_obfuscated())
}
}