1use anyhow::{anyhow, Result};
2use quick_xml::events::Event;
3use quick_xml::reader::Reader;
4use std::collections::{BTreeMap, HashSet};
5use std::path::Path;
6use tracing::info;
7
8use crate::config::{AppConfig, FeedConfig, FeedFilterConfig};
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct OpmlFeed {
12 pub title: String,
13 pub xml_url: String,
14 pub html_url: Option<String>,
15 pub category: Option<String>,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct ExportableFeed {
20 pub title: String,
21 pub xml_url: String,
22 pub html_url: Option<String>,
23 pub group: Option<String>,
24}
25
26pub fn parse_opml(xml_content: &str) -> Result<Vec<OpmlFeed>> {
27 let mut reader = Reader::from_str(xml_content);
28 reader.trim_text(true);
29
30 let mut feeds = Vec::new();
31 let mut folder_stack: Vec<String> = Vec::new();
32 let mut node_stack: Vec<bool> = Vec::new(); let mut buf = Vec::new();
35
36 loop {
37 match reader.read_event_into(&mut buf) {
38 Ok(Event::Start(ref e)) if e.name().as_ref() == b"outline" => {
39 let (feed, is_folder) = parse_outline_attributes(e, &folder_stack)?;
40 if let Some(f) = feed {
41 feeds.push(f);
42 node_stack.push(false);
43 } else if let Some(folder_name) = is_folder {
44 folder_stack.push(folder_name);
45 node_stack.push(true);
46 } else {
47 node_stack.push(false);
48 }
49 }
50 Ok(Event::Empty(ref e)) if e.name().as_ref() == b"outline" => {
51 let (feed, _) = parse_outline_attributes(e, &folder_stack)?;
52 if let Some(f) = feed {
53 feeds.push(f);
54 }
55 }
56 Ok(Event::End(ref e)) if e.name().as_ref() == b"outline" => {
57 if let Some(was_folder) = node_stack.pop() {
58 if was_folder {
59 folder_stack.pop();
60 }
61 }
62 }
63 Ok(Event::Eof) => break,
64 Err(e) => return Err(anyhow!("OPML XML parse error: {}", e)),
65 _ => {}
66 }
67 buf.clear();
68 }
69
70 Ok(feeds)
71}
72
73fn parse_outline_attributes(
74 e: &quick_xml::events::BytesStart,
75 folder_stack: &[String],
76) -> Result<(Option<OpmlFeed>, Option<String>)> {
77 let mut title = None;
78 let mut text = None;
79 let mut xml_url = None;
80 let mut html_url = None;
81
82 for attr in e.attributes() {
83 let attr = attr.map_err(|err| anyhow!("XML attribute error: {}", err))?;
84 let key = std::str::from_utf8(attr.key.as_ref())?.to_lowercase();
85 let raw_val = String::from_utf8_lossy(&attr.value);
86 let value = match quick_xml::escape::unescape(&raw_val) {
87 Ok(v) => v.into_owned(),
88 Err(_) => raw_val.into_owned(),
89 };
90
91 match key.as_str() {
92 "title" => title = Some(value),
93 "text" => text = Some(value),
94 "xmlurl" => xml_url = Some(value),
95 "htmlurl" => html_url = Some(value),
96 _ => {}
97 }
98 }
99
100 let feed_title = title
101 .clone()
102 .or(text.clone())
103 .unwrap_or_else(|| "Untitled Feed".to_string());
104
105 if let Some(url) = xml_url {
106 let category = folder_stack.last().cloned();
107 Ok((
108 Some(OpmlFeed {
109 title: feed_title,
110 xml_url: url,
111 html_url,
112 category,
113 }),
114 None,
115 ))
116 } else {
117 let folder_name = text.or(title).unwrap_or_else(|| "Folder".to_string());
119 Ok((None, Some(folder_name)))
120 }
121}
122
123pub fn slugify(title: &str) -> String {
124 let mut slug = String::new();
125 let mut prev_dash = false;
126
127 for ch in title.chars() {
128 if ch.is_alphanumeric() {
129 slug.push(ch.to_ascii_lowercase());
130 prev_dash = false;
131 } else if !prev_dash {
132 slug.push('-');
133 prev_dash = true;
134 }
135 }
136
137 let trimmed = slug.trim_matches('-').to_string();
138 if trimmed.is_empty() {
139 "feed".to_string()
140 } else {
141 trimmed
142 }
143}
144
145pub fn unique_slug(base: &str, existing: &HashSet<String>) -> String {
146 if !existing.contains(base) {
147 return base.to_string();
148 }
149 let mut counter = 2;
150 loop {
151 let candidate = format!("{}-{}", base, counter);
152 if !existing.contains(&candidate) {
153 return candidate;
154 }
155 counter += 1;
156 }
157}
158
159pub fn import_opml_into_config<P1: AsRef<Path>, P2: AsRef<Path>>(
160 opml_path: P1,
161 config_path: P2,
162 dry_run: bool,
163) -> Result<Vec<(String, OpmlFeed)>> {
164 let content = std::fs::read_to_string(opml_path.as_ref())
165 .map_err(|e| anyhow!("Failed to read OPML file: {}", e))?;
166
167 let feeds = parse_opml(&content)?;
168 if feeds.is_empty() {
169 return Ok(Vec::new());
170 }
171
172 let mut config = AppConfig::load_or_default(config_path.as_ref())?;
173 let mut existing_slugs: HashSet<String> = config.feeds.keys().cloned().collect();
174 let mut imported = Vec::new();
175
176 for feed in feeds {
177 let base_slug = slugify(&feed.title);
178 let slug = unique_slug(&base_slug, &existing_slugs);
179 existing_slugs.insert(slug.clone());
180
181 let feed_config = FeedConfig {
182 url: feed.xml_url.clone(),
183 title_override: Some(feed.title.clone()),
184 group: feed.category.clone(),
185 nop: false,
186 nop_reason: None,
187 filters: FeedFilterConfig {
188 clean_links: true,
189 ..Default::default()
190 },
191 };
192
193 config.feeds.insert(slug.clone(), feed_config);
194 imported.push((slug, feed));
195 }
196
197 if !dry_run {
198 let yaml = serde_yaml::to_string(&config)
199 .map_err(|e| anyhow!("Failed to serialize config to YAML: {}", e))?;
200 std::fs::write(config_path.as_ref(), yaml)
201 .map_err(|e| anyhow!("Failed to save updated config: {}", e))?;
202 info!("Updated config file at {:?}", config_path.as_ref());
203 }
204
205 Ok(imported)
206}
207
208pub fn generate_grouped_opml(title: &str, feeds: &[ExportableFeed]) -> String {
210 let mut out = String::new();
211 out.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
212 out.push_str("<opml version=\"2.0\">\n");
213 out.push_str(" <head>\n");
214 out.push_str(&format!(
215 " <title>{}</title>\n",
216 quick_xml::escape::escape(title)
217 ));
218 out.push_str(" </head>\n");
219 out.push_str(" <body>\n");
220
221 let mut grouped: BTreeMap<Option<String>, Vec<&ExportableFeed>> = BTreeMap::new();
223 for f in feeds {
224 grouped.entry(f.group.clone()).or_default().push(f);
225 }
226
227 for (group_opt, group_feeds) in &grouped {
229 if let Some(ref group_name) = group_opt {
230 let escaped_group = quick_xml::escape::escape(group_name);
231 out.push_str(&format!(
232 " <outline text=\"{}\" title=\"{}\">\n",
233 escaped_group, escaped_group
234 ));
235
236 for f in group_feeds {
237 write_feed_outline(&mut out, f, " ");
238 }
239
240 out.push_str(" </outline>\n");
241 }
242 }
243
244 if let Some(top_level) = grouped.get(&None) {
246 for f in top_level {
247 write_feed_outline(&mut out, f, " ");
248 }
249 }
250
251 out.push_str(" </body>\n");
252 out.push_str("</opml>\n");
253 out
254}
255
256fn write_feed_outline(out: &mut String, feed: &ExportableFeed, indent: &str) {
257 let escaped_title = quick_xml::escape::escape(&feed.title);
258 let escaped_xml = quick_xml::escape::escape(&feed.xml_url);
259 let html_attr = match feed.html_url {
260 Some(ref u) => format!(" htmlUrl=\"{}\"", quick_xml::escape::escape(u)),
261 None => String::new(),
262 };
263
264 out.push_str(&format!(
265 "{}<outline type=\"rss\" text=\"{}\" title=\"{}\" xmlUrl=\"{}\"{}/>\n",
266 indent, escaped_title, escaped_title, escaped_xml, html_attr
267 ));
268}