Debian Codegraph @

podcasts-gtk/src/chapter_parser/extended_comment.rs

raw symbols SCIP
1 // extended_comment.rs
2 //
3 // Copyright 2025 nee <[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 regex::Regex;
21 use std::collections::HashMap;
22 use std::sync::LazyLock;
23
24 use crate::chapter_parser::Chapter;
25
26 /// Parses comment and writes updates into the chapters HashMap.
27 /// A chapter comment can either contain the title, or the timestamp.
28 /// So parsing of one chapter has to be done over multiple comment tags.
29 pub fn parse_extended_comment(
30 chapters: &mut HashMap<String, Chapter>,
31 comment: &str,
32 ) -> Option<()> {
33 // "CHAPTER002NAME=Prologue"
34 static RE_NAME: LazyLock<Regex> =
35 LazyLock::new(|| Regex::new(r"^CHAPTER([0-9]+)NAME=(.+)$").unwrap());
36 let matches = RE_NAME.captures_iter(comment).next();
37 if let Some(matches) = matches {
38 let id = matches.get(1)?.as_str().to_string();
39 let title = matches.get(2)?.as_str().to_string();
40 let chapter = get_or_init(chapters, id.clone());
41 chapter.id = id;
42 chapter.title = title;
43 return Some(());
44 }
45
46 // "CHAPTER002=00:00:14.640"
47 static RE_TIME: LazyLock<Regex> =
48 LazyLock::new(|| Regex::new(r"CHAPTER([0-9]+)=([0-9]+):([0-9]+):([0-9\.]+)").unwrap());
49 let matches = RE_TIME.captures_iter(comment).next();
50 if let Some(matches) = matches {
51 let id = matches.get(1)?.as_str().to_string();
52 let hours = matches.get(2)?.as_str().parse::<i64>().ok()?;
53 let minutes = matches.get(3)?.as_str().parse::<i64>().ok()?;
54 let seconds = matches.get(4)?.as_str().parse::<f64>().ok()?;
55
56 let chapter = get_or_init(chapters, id.clone());
57 chapter.id = id;
58 chapter.start = chrono::Duration::hours(hours)
59 + chrono::Duration::minutes(minutes)
60 + chrono::Duration::from_std(std::time::Duration::from_secs_f64(seconds)).ok()?;
61 return Some(());
62 }
63 None
64 }
65
66 /// Gets an existing chapter from the hashmap, or initalizes a new one in it.
67 fn get_or_init(chapters: &mut HashMap<String, Chapter>, key: String) -> &mut Chapter {
68 if chapters.contains_key(&key) {
69 return chapters.get_mut(&key).unwrap();
70 }
71 chapters.insert(key.clone(), Chapter::default());
72 chapters.get_mut(&key).unwrap()
73 }
74
75 #[cfg(test)]
76 mod tests {
77 use super::*;
78 use anyhow::Result;
79
80 #[test]
81 #[rustfmt::skip]
82 fn test_comment_newz() -> Result<()> {
83 let comments = [
84 "CHAPTER001=00:00:00.000",
85 "CHAPTER001NAME=Intro",
86 "CHAPTER002=00:00:14.640",
87 "CHAPTER002NAME=Prologue",
88 "CHAPTER003=00:03:47.967",
89 "CHAPTER003NAME=US president accepts reality of election. Kind of.",
90 "CHAPTER004=00:15:18.630",
91 "CHAPTER004NAME=What damage can trump still cause",
92 "CHAPTER005=00:22:11.746",
93 "CHAPTER005NAME=Biden starts choosing his cabinet and announcing his policies",
94 "CHAPTER006=00:33:43.575",
95 "CHAPTER006NAME=Georgia Senate Runoffs",
96 "CHAPTER007=00:45:02.492",
97 "CHAPTER007NAME=Supremes make a covid-religion ruling where conservative judges show their stripes",
98 "CHAPTER008=00:49:22.818",
99 "CHAPTER008NAME=Countries struggles with consipiracy theories around Covid",
100 "CHAPTER009=00:58:12.247",
101 "CHAPTER009NAME=Poland and Hungary block EU's Gender Action Plan",
102 "CHAPTER010=01:07:00.960",
103 "CHAPTER010NAME=Brexit in the end stage",
104 "CHAPTER011=01:16:29.643",
105 "CHAPTER011NAME=Epilog",
106 "CHAPTER012=01:17:06.308",
107 "CHAPTER012NAME=Bonus Track",
108 ];
109 let expected_chapters = [
110 Chapter {id: "001".to_string(),
111 title: "Intro".to_string(),
112 description: "".to_string(),
113 start: chrono::Duration::new(0, 0).unwrap(),
114 end: chrono::Duration::new(0, 0).unwrap(),
115 },
116 Chapter {id: "002".to_string(),
117 title: "Prologue".to_string(),
118 description: "".to_string(),
119 start: chrono::Duration::new(14, 640000000).unwrap(),
120 end: chrono::Duration::new(0, 0).unwrap(),
121 },
122 Chapter {id: "003".to_string(),
123 title: "US president accepts reality of election. Kind of.".to_string(),
124 description: "".to_string(),
125 start: chrono::Duration::new(227, 967000000).unwrap(),
126 end: chrono::Duration::new(0, 0).unwrap(),
127 },
128 Chapter {id: "004".to_string(),
129 title: "What damage can trump still cause".to_string(),
130 description: "".to_string(),
131 start: chrono::Duration::new(918, 630000000).unwrap(),
132 end: chrono::Duration::new(0, 0).unwrap(),
133 },
134 Chapter {id: "005".to_string(),
135 title: "Biden starts choosing his cabinet and announcing his policies".to_string(),
136 description: "".to_string(),
137 start: chrono::Duration::new(1331, 746000000).unwrap(),
138 end: chrono::Duration::new(0, 0).unwrap(),
139 },
140 Chapter {id: "006".to_string(),
141 title: "Georgia Senate Runoffs".to_string(),
142 description: "".to_string(),
143 start: chrono::Duration::new(2023, 575000000).unwrap(),
144 end: chrono::Duration::new(0, 0).unwrap(),
145 },
146 Chapter {id: "007".to_string(),
147 title: "Supremes make a covid-religion ruling where conservative judges show their stripes".to_string(),
148 description: "".to_string(),
149 start: chrono::Duration::new(2702, 492000000).unwrap(),
150 end: chrono::Duration::new(0, 0).unwrap(),
151 },
152 Chapter {id: "008".to_string(),
153 title: "Countries struggles with consipiracy theories around Covid".to_string(),
154 description: "".to_string(),
155 start: chrono::Duration::new(2962, 818000000).unwrap(),
156 end: chrono::Duration::new(0, 0).unwrap(),
157 },
158 Chapter {id: "009".to_string(),
159 title: "Poland and Hungary block EU's Gender Action Plan".to_string(),
160 description: "".to_string(),
161 start: chrono::Duration::new(3492, 247000000).unwrap(),
162 end: chrono::Duration::new(0, 0).unwrap(),
163 },
164 Chapter {id: "010".to_string(),
165 title: "Brexit in the end stage".to_string(),
166 description: "".to_string(),
167 start: chrono::Duration::new(4020, 960000000).unwrap(),
168 end: chrono::Duration::new(0, 0).unwrap(),
169 },
170 Chapter {id: "011".to_string(),
171 title: "Epilog".to_string(),
172 description: "".to_string(),
173 start: chrono::Duration::new(4589, 643000000).unwrap(),
174 end: chrono::Duration::new(0, 0).unwrap(),
175 },
176 Chapter {id: "012".to_string(),
177 title: "Bonus Track".to_string(),
178 description: "".to_string(),
179 start: chrono::Duration::new(4626, 308000000).unwrap(),
180 end: chrono::Duration::new(0, 0).unwrap(),
181 }
182 ];
183 let mut chapters_map: HashMap<String, Chapter> = HashMap::new();
184 for s in comments {
185 parse_extended_comment(&mut chapters_map, &s);
186 }
187
188 let mut result: Vec<_> = chapters_map.into_values().collect();
189 result.sort_by_key(|c| c.id.clone());
190
191 assert_eq!(expected_chapters.len(), result.len());
192
193 let mut i = 0;
194 for chapter in result {
195 assert_eq!(expected_chapters[i], chapter);
196 i = i+1;
197 }
198 Ok(())
199 }
200 }