feedr 0.7.0

Feedr is a feature-rich terminal-based RSS/Atom feed reader written in Rust.
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use feed_rs::parser;
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::OnceLock;
use std::time::Duration;
use url::Url;
use uuid::Uuid;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Feed {
    pub url: String,
    pub title: String,
    pub items: Vec<FeedItem>,
    #[serde(skip)]
    pub title_lower: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FeedItem {
    pub title: String,
    pub link: Option<String>,
    pub description: Option<String>,
    pub pub_date: Option<String>,
    pub author: Option<String>,
    pub formatted_date: Option<String>,
    #[serde(skip)]
    pub parsed_date: Option<DateTime<Utc>>,
    #[serde(skip)]
    pub plain_text: Option<String>,
    #[serde(skip)]
    pub title_lower: String,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct FeedCategory {
    pub id: String,
    pub name: String,
    pub feeds: HashSet<String>, // URLs of feeds in this category, using HashSet for faster lookup
    pub expanded: bool,         // UI state: whether the category is expanded in the UI
}

impl FeedCategory {
    pub fn new(name: &str) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            name: name.to_string(),
            feeds: HashSet::new(),
            expanded: true,
        }
    }

    pub fn add_feed(&mut self, url: &str) {
        self.feeds.insert(url.to_string());
    }

    pub fn remove_feed(&mut self, url: &str) -> bool {
        self.feeds.remove(url)
    }

    pub fn contains_feed(&self, url: &str) -> bool {
        self.feeds.contains(url)
    }

    pub fn feed_count(&self) -> usize {
        self.feeds.len()
    }

    pub fn rename(&mut self, new_name: &str) {
        self.name = new_name.to_string();
    }

    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FeedType {
    Rss,
    Atom,
}

impl fmt::Display for FeedType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FeedType::Rss => write!(f, "RSS"),
            FeedType::Atom => write!(f, "Atom"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct DiscoveredFeed {
    pub url: String,
    pub title: String,
    pub feed_type: FeedType,
}

/// Result of fetching a URL that was expected to be a feed.
pub enum FeedFetchResult {
    /// Successfully parsed as an RSS/Atom feed.
    Feed(Feed),
    /// The URL returned an HTML page; any discovered feed links are included.
    DiscoveredFeeds {
        feeds: Vec<DiscoveredFeed>,
        page_url: String,
    },
}

impl FeedFetchResult {
    /// Convert to a `Feed`, returning an error if this was an HTML page.
    pub fn into_feed(self) -> Result<Feed> {
        match self {
            FeedFetchResult::Feed(feed) => Ok(feed),
            FeedFetchResult::DiscoveredFeeds { feeds, page_url } => {
                if feeds.is_empty() {
                    Err(anyhow::anyhow!(
                        "No RSS/Atom feed links found on this page: {}",
                        page_url
                    ))
                } else {
                    Err(anyhow::anyhow!(
                        "URL is an HTML page, not a feed. {} feed link(s) found on: {}",
                        feeds.len(),
                        page_url
                    ))
                }
            }
        }
    }
}

pub fn discover_feeds_from_html(html: &[u8], base_url: &Url) -> Vec<DiscoveredFeed> {
    static SELECTOR: OnceLock<Selector> = OnceLock::new();
    let selector = SELECTOR.get_or_init(|| Selector::parse("link[rel=alternate]").unwrap());

    let html_str = String::from_utf8_lossy(html);
    let document = Html::parse_document(&html_str);

    let mut seen_urls = HashSet::new();
    let mut feeds = Vec::new();

    for element in document.select(selector) {
        let link_type = match element.value().attr("type") {
            Some(t) => t.to_lowercase(),
            None => continue,
        };

        let feed_type = if link_type == "application/rss+xml" {
            FeedType::Rss
        } else if link_type == "application/atom+xml" {
            FeedType::Atom
        } else {
            continue;
        };

        let href = match element.value().attr("href") {
            Some(h) if !h.is_empty() => h,
            _ => continue,
        };

        let resolved = match base_url.join(href) {
            Ok(u) => u.to_string(),
            Err(_) => continue,
        };

        if !seen_urls.insert(resolved.clone()) {
            continue;
        }

        let title = element
            .value()
            .attr("title")
            .filter(|t| !t.is_empty())
            .unwrap_or(&resolved)
            .to_string();

        feeds.push(DiscoveredFeed {
            url: resolved,
            title,
            feed_type,
        });
    }

    feeds
}

impl Feed {
    /// Fetch and parse a feed from a URL with default timeout
    pub fn from_url(url: &str) -> Result<Self> {
        Self::from_url_with_config(url, 15, None, None)
    }

    /// Fetch and parse a feed from a URL with custom timeout
    pub fn from_url_with_timeout(url: &str, timeout_secs: u64) -> Result<Self> {
        Self::from_url_with_config(url, timeout_secs, None, None)
    }

    /// Fetch and parse a feed from a URL with custom timeout and user agent.
    /// Returns an error if the URL is an HTML page (use `fetch_url` for discovery support).
    pub fn from_url_with_config(
        url: &str,
        timeout_secs: u64,
        user_agent: Option<&str>,
        custom_headers: Option<&HashMap<String, String>>,
    ) -> Result<Self> {
        let client = Self::build_client(timeout_secs)?;
        Self::fetch_url(url, &client, user_agent, custom_headers)?.into_feed()
    }

    /// Build a shared HTTP client with the given timeout
    pub fn build_client(timeout_secs: u64) -> Result<reqwest::blocking::Client> {
        reqwest::blocking::Client::builder()
            .redirect(reqwest::redirect::Policy::limited(10))
            .timeout(Duration::from_secs(timeout_secs))
            .build()
            .context("Failed to create HTTP client")
    }

    /// Fetch a URL and return either a parsed feed or discovered feed links.
    pub fn fetch_url(
        url: &str,
        client: &reqwest::blocking::Client,
        user_agent: Option<&str>,
        custom_headers: Option<&HashMap<String, String>>,
    ) -> Result<FeedFetchResult> {
        let default_user_agent =
            "Mozilla/5.0 (compatible; Feedr/1.0; +https://github.com/bahdotsh/feedr)";
        let ua = user_agent.unwrap_or(default_user_agent);

        let mut request = client
            .get(url)
            .header("User-Agent", ua)
            .header(
                "Accept",
                "application/rss+xml, application/atom+xml, application/xml, text/xml, */*",
            )
            .header("Accept-Language", "en-US,en;q=0.9")
            .header("Accept-Encoding", "gzip, deflate")
            .header("Cache-Control", "no-cache")
            .header("Connection", "keep-alive");

        if let Some(headers) = custom_headers {
            for (key, value) in headers {
                request = request.header(key, value);
            }
        }

        let response = request.send().context("Failed to fetch feed")?;

        // Check if we got redirected or have an unusual status
        let final_url = response.url().clone();
        let status = response.status();
        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|ct| ct.to_str().ok())
            .unwrap_or("unknown")
            .to_lowercase();

        if !status.is_success() {
            return Err(anyhow::anyhow!(
                "HTTP error {}: Failed to fetch feed from {}",
                status,
                url
            ));
        }

        let content = response.bytes().context("Failed to read response body")?;

        // Reject suspiciously short responses (likely empty/error pages)
        if content.len() < 100 {
            return Err(anyhow::anyhow!(
                "Response too short ({} bytes), might be empty or an error page",
                content.len()
            ));
        }

        // Try parsing as feed first — some servers serve valid feeds with text/html content-type
        let feed = match parser::parse(&content[..]) {
            Ok(f) => f,
            Err(parse_err) => {
                // Parse failed — check if this looks like HTML and try feed discovery
                let content_start =
                    String::from_utf8_lossy(&content[..std::cmp::min(200, content.len())]);
                let trimmed_lower = content_start.trim_start().to_lowercase();
                if content_type.contains("text/html")
                    || trimmed_lower.starts_with("<!doctype html")
                    || trimmed_lower.starts_with("<html")
                {
                    let discovered = discover_feeds_from_html(&content, &final_url);
                    return Ok(FeedFetchResult::DiscoveredFeeds {
                        feeds: discovered,
                        page_url: final_url.to_string(),
                    });
                }

                let content_preview =
                    String::from_utf8_lossy(&content[..std::cmp::min(300, content.len())]);
                return Err(parse_err).with_context(|| {
                    format!(
                        "Failed to parse feed (RSS/Atom) from URL: {} (final URL: {}, {} bytes, content-type: {}, preview: {})",
                        url, final_url, content.len(), content_type, content_preview.trim()
                    )
                });
            }
        };

        let items = feed.entries.iter().map(FeedItem::from_feed_entry).collect();

        let title = feed
            .title
            .map(|t| t.content)
            .unwrap_or_else(|| "Untitled Feed".to_string());
        let title_lower = title.to_lowercase();

        Ok(FeedFetchResult::Feed(Feed {
            url: url.to_string(),
            title,
            items,
            title_lower,
        }))
    }
}

impl FeedItem {
    fn from_feed_entry(entry: &feed_rs::model::Entry) -> Self {
        // Extract publication date - try multiple date formats
        let (pub_date_string, formatted_date, parsed_date) =
            if let Some(published) = &entry.published {
                let pub_string = published.to_rfc3339();
                let formatted = format_date(*published);
                (Some(pub_string), Some(formatted), Some(*published))
            } else if let Some(updated) = &entry.updated {
                let pub_string = updated.to_rfc3339();
                let formatted = format_date(*updated);
                (Some(pub_string), Some(formatted), Some(*updated))
            } else {
                (None, None, None)
            };

        // Extract author information
        let author = entry.authors.first().map(|author| {
            if !author.name.is_empty() {
                author.name.clone()
            } else if let Some(email) = &author.email {
                email.clone()
            } else {
                "Unknown".to_string()
            }
        });

        // Extract content/description - prefer content over summary
        let description = if let Some(content) = entry.content.as_ref() {
            Some(content.body.clone().unwrap_or_default())
        } else {
            entry
                .summary
                .as_ref()
                .map(|summary| summary.content.clone())
        };

        // Cache plain text from description (avoids repeated HTML parsing)
        let plain_text = description
            .as_ref()
            .map(|desc| html2text::from_read(desc.as_bytes(), 80));

        // Extract the primary link
        let link = entry.links.first().map(|link| link.href.clone());

        let title = entry
            .title
            .as_ref()
            .map(|t| t.content.clone())
            .unwrap_or_else(|| "Untitled".to_string());
        let title_lower = title.to_lowercase();

        FeedItem {
            title,
            link,
            description,
            pub_date: pub_date_string,
            author,
            formatted_date,
            parsed_date,
            plain_text,
            title_lower,
        }
    }
}

fn format_date(dt: DateTime<Utc>) -> String {
    // Calculate how long ago the item was published
    let now = Utc::now();
    let diff = now.signed_duration_since(dt);

    if diff.num_minutes() < 60 {
        format!("{} minutes ago", diff.num_minutes())
    } else if diff.num_hours() < 24 {
        format!("{} hours ago", diff.num_hours())
    } else if diff.num_days() < 7 {
        format!("{} days ago", diff.num_days())
    } else {
        // For older items, show the actual date
        dt.format("%B %d, %Y").to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_discover_single_rss_feed() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" title="My Blog" href="/feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/blog/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds.len(), 1);
        assert_eq!(feeds[0].url, "https://example.com/feed.xml");
        assert_eq!(feeds[0].title, "My Blog");
        assert_eq!(feeds[0].feed_type, FeedType::Rss);
    }

    #[test]
    fn test_discover_multiple_feeds() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/rss.xml">
            <link rel="alternate" type="application/atom+xml" title="Atom Feed" href="/atom.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds.len(), 2);
        assert_eq!(feeds[0].feed_type, FeedType::Rss);
        assert_eq!(feeds[1].feed_type, FeedType::Atom);
    }

    #[test]
    fn test_discover_no_feeds() {
        let html = br#"<html><head><title>No feeds</title></head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert!(feeds.is_empty());
    }

    #[test]
    fn test_discover_relative_url_resolution() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" href="feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/blog/page").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds[0].url, "https://example.com/blog/feed.xml");
    }

    #[test]
    fn test_discover_absolute_url_preserved() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" href="https://feeds.example.com/rss">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds[0].url, "https://feeds.example.com/rss");
    }

    #[test]
    fn test_discover_missing_title_falls_back_to_url() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" href="/feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds[0].title, "https://example.com/feed.xml");
    }

    #[test]
    fn test_discover_deduplicates() {
        let html = br#"<html><head>
            <link rel="alternate" type="application/rss+xml" title="Feed" href="/feed.xml">
            <link rel="alternate" type="application/rss+xml" title="Same Feed" href="/feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds.len(), 1);
    }

    #[test]
    fn test_discover_case_insensitive_type() {
        let html = br#"<html><head>
            <link rel="alternate" type="Application/RSS+XML" href="/feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds.len(), 1);
    }

    #[test]
    fn test_discover_lowercase_doctype() {
        let html = br#"<!doctype html><html><head>
            <link rel="alternate" type="application/rss+xml" title="Feed" href="/feed.xml">
        </head><body></body></html>"#;
        let base = Url::parse("https://example.com/").unwrap();
        let feeds = discover_feeds_from_html(html, &base);
        assert_eq!(feeds.len(), 1);
        assert_eq!(feeds[0].url, "https://example.com/feed.xml");
    }

    #[test]
    fn test_into_feed_with_feed_variant() {
        let feed = Feed {
            url: "https://example.com/feed.xml".to_string(),
            title: "Test Feed".to_string(),
            items: vec![],
            title_lower: "test feed".to_string(),
        };
        let result = FeedFetchResult::Feed(feed);
        let feed = result.into_feed().unwrap();
        assert_eq!(feed.url, "https://example.com/feed.xml");
        assert_eq!(feed.title, "Test Feed");
    }

    #[test]
    fn test_into_feed_with_discovered_feeds_returns_error() {
        let result = FeedFetchResult::DiscoveredFeeds {
            feeds: vec![DiscoveredFeed {
                url: "https://example.com/rss".to_string(),
                title: "RSS Feed".to_string(),
                feed_type: FeedType::Rss,
            }],
            page_url: "https://example.com/".to_string(),
        };
        let err = result.into_feed().unwrap_err();
        assert!(err.to_string().contains("HTML page"));
        assert!(err.to_string().contains("1 feed link(s)"));
    }

    #[test]
    fn test_into_feed_with_empty_discovered_feeds_returns_error() {
        let result = FeedFetchResult::DiscoveredFeeds {
            feeds: vec![],
            page_url: "https://example.com/".to_string(),
        };
        let err = result.into_feed().unwrap_err();
        assert!(err.to_string().contains("No RSS/Atom feed links found"));
    }
}