Debian Codegraph @

podcasts-data/src/parser.rs

raw symbols SCIP
1 // parser.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use rss::extension::itunes::ITunesItemExtension;
21
22 /// Parses an Item Itunes extension and returns it's duration value in seconds.
23 // FIXME: Rafactor
24 #[allow(non_snake_case)]
25 pub(crate) fn parse_itunes_duration(item: Option<&ITunesItemExtension>) -> Option<i32> {
26 let duration = item.map(|s| s.duration())??;
27
28 // FOR SOME FUCKING REASON, IN THE APPLE EXTENSION SPEC
29 // THE DURATION CAN BE EITHER AN INT OF SECONDS OR
30 // A STRING OF THE FOLLOWING FORMATS:
31 // HH:MM:SS, H:MM:SS, MM:SS, M:SS
32 // LIKE WHO THE FUCK THOUGH THAT WOULD BE A GOOD IDEA.
33 if let Ok(NO_FUCKING_LOGIC) = duration.parse::<i32>() {
34 return Some(NO_FUCKING_LOGIC);
35 };
36
37 let mut seconds = 0;
38 let fk_apple = duration.split(':').collect::<Vec<_>>();
39 if fk_apple.len() == 3 {
40 seconds += fk_apple[0].parse::<i32>().unwrap_or(0) * 3600;
41 seconds += fk_apple[1].parse::<i32>().unwrap_or(0) * 60;
42 seconds += fk_apple[2].parse::<i32>().unwrap_or(0);
43 } else if fk_apple.len() == 2 {
44 seconds += fk_apple[0].parse::<i32>().unwrap_or(0) * 60;
45 seconds += fk_apple[1].parse::<i32>().unwrap_or(0);
46 }
47
48 Some(seconds)
49 }
50
51 #[cfg(test)]
52 mod tests {
53 use rss::extension::itunes::ITunesItemExtensionBuilder;
54
55 use super::*;
56
57 #[test]
58 fn test_itunes_duration() {
59 // Input is a String<Int>
60 let extension = ITunesItemExtensionBuilder::default()
61 .duration(Some("3370".into()))
62 .build();
63 let item = Some(&extension);
64 assert_eq!(parse_itunes_duration(item), Some(3370));
65
66 // Input is a String<M:SS>
67 let extension = ITunesItemExtensionBuilder::default()
68 .duration(Some("6:10".into()))
69 .build();
70 let item = Some(&extension);
71 assert_eq!(parse_itunes_duration(item), Some(370));
72
73 // Input is a String<MM:SS>
74 let extension = ITunesItemExtensionBuilder::default()
75 .duration(Some("56:10".into()))
76 .build();
77 let item = Some(&extension);
78 assert_eq!(parse_itunes_duration(item), Some(3370));
79
80 // Input is a String<H:MM:SS>
81 let extension = ITunesItemExtensionBuilder::default()
82 .duration(Some("1:56:10".into()))
83 .build();
84 let item = Some(&extension);
85 assert_eq!(parse_itunes_duration(item), Some(6970));
86
87 // Input is a String<HH:MM:SS>
88 let extension = ITunesItemExtensionBuilder::default()
89 .duration(Some("01:56:10".into()))
90 .build();
91 let item = Some(&extension);
92 assert_eq!(parse_itunes_duration(item), Some(6970));
93 }
94 }