decruft 0.1.4

Extract clean, readable content from web pages
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
//! X/Twitter extractor.
//!
//! Handles two page types:
//! - **Articles**: long-form content at `x.com/user/article/ID`,
//!   extracted from static HTML containers.
//! - **Tweets**: individual posts at `x.com/user/status/ID`. Static
//!   HTML rarely contains the tweet text, so we fall back to the
//!   Twitter oEmbed API (`publish.twitter.com/oembed`).

use scraper::Html;

use crate::dom;

use super::ExtractorResult;

const ARTICLE_CONTAINER_SEL: &str = "[data-testid=\"twitterArticleRichTextView\"]";
const ARTICLE_READ_VIEW_SEL: &str = "[data-testid=\"twitterArticleReadView\"]";
const TITLE_SEL: &str = "[data-testid=\"twitter-article-title\"]";
const AUTHOR_SEL: &str = "[itemprop=\"author\"]";
const AUTHOR_NAME_SEL: &str = "meta[itemprop=\"name\"]";
const AUTHOR_HANDLE_SEL: &str = "meta[itemprop=\"additionalName\"]";
const IMAGES_SEL: &str = "[data-testid=\"tweetPhoto\"] img";

/// Detect whether this page is an X/Twitter article.
#[must_use]
pub fn is_x_article(html: &Html, url: Option<&str>) -> bool {
    let is_x_domain = url.is_some_and(|u| u.contains("x.com/") || u.contains("twitter.com/"));
    if !is_x_domain {
        return false;
    }
    has_selector(html, ARTICLE_CONTAINER_SEL)
}

/// Check whether a URL points to an individual tweet.
#[must_use]
pub fn is_tweet_url(url: Option<&str>) -> bool {
    use std::sync::LazyLock;
    static TWEET_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"(?:x\.com|twitter\.com)/[a-zA-Z0-9_]{1,15}/status/\d+")
            .expect("tweet url regex is valid")
    });
    url.is_some_and(|u| TWEET_RE.is_match(u))
}

/// Extract content from an X/Twitter page (article or tweet).
///
/// Tries the article extractor first. If the URL is a tweet and
/// article extraction fails, falls back to the oEmbed API.
#[must_use]
pub fn extract_x_article(html: &Html, url: Option<&str>) -> Option<ExtractorResult> {
    if let Some(result) = try_article(html, url) {
        return Some(result);
    }
    if let Some(u) = url
        && is_tweet_url(Some(u))
    {
        if let Some(result) = try_oembed(u) {
            return Some(result);
        }
        // Fallback: extract metadata from HTML; only use if it
        // produced meaningful content (otherwise let generic pipeline run)
        return try_tweet_from_html(html, url)
            .filter(|r| !r.content.trim().is_empty() || r.title.is_some());
    }
    None
}

/// Try extracting a long-form X/Twitter article.
fn try_article(html: &Html, url: Option<&str>) -> Option<ExtractorResult> {
    if !is_x_article(html, url) {
        return None;
    }

    let container_ids = dom::select_ids(html, ARTICLE_CONTAINER_SEL);
    let container_id = container_ids.first().copied()?;

    let title = extract_title(html);
    let author = extract_author(html, url);
    let header_image = extract_header_image(html, container_id);
    let body = dom::inner_html(html, container_id);

    let content = format!(
        "<article class=\"x-article\">{header_image}{}</article>",
        body.trim()
    );

    Some(ExtractorResult {
        content,
        title: Some(title),
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("X (Twitter)".to_string()),
        published: None,
        image: None,
        description: None,
    })
}

/// Fetch tweet content via the Twitter oEmbed API.
///
/// The API returns JSON with `html` (a blockquote), `author_name`,
/// and `author_url`. We use `html` as content and `author_name` as
/// author.
fn try_oembed(tweet_url: &str) -> Option<ExtractorResult> {
    let api_url = format!(
        "https://publish.twitter.com/oembed?url={}",
        urlencoding(tweet_url),
    );

    let body = crate::http::get(&api_url)?;
    let json: serde_json::Value = serde_json::from_str(&body).ok()?;

    let html = json.get("html")?.as_str()?;
    if html.trim().is_empty() {
        return None;
    }

    let author_name = json
        .get("author_name")
        .and_then(|v| v.as_str())
        .map(String::from);

    Some(ExtractorResult {
        content: html.to_string(),
        title: None,
        author: author_name,
        site: Some("X (Twitter)".to_string()),
        published: None,
        image: None,
        description: None,
    })
}

/// Percent-encode a URL for use as a query parameter value.
fn urlencoding(s: &str) -> String {
    let mut result = String::with_capacity(s.len() * 2);
    for byte in s.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                result.push(byte as char);
            }
            _ => {
                use std::fmt::Write;
                let _ = write!(result, "%{byte:02X}");
            }
        }
    }
    result
}

/// Extract tweet data from HTML when oEmbed is unavailable.
/// Cleans og:title "(N) Username on X: "content" / X" → "content"
/// Extracts author from URL handle.
fn try_tweet_from_html(html: &Html, url: Option<&str>) -> Option<ExtractorResult> {
    use std::sync::LazyLock;
    static OG_TITLE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        // Character class [\u{201c}\u{201d}"] matches straight and curly quotes
        regex::Regex::new(
            r#"(?:\(\d+\)\s+)?[^:]+\s+on\s+X:\s*["""\u{201c}\u{201d}](.+?)["""\u{201c}\u{201d}]"#,
        )
        .expect("x og:title regex is valid")
    });

    let og_title = dom::select_ids(html, "meta[property=\"og:title\"]")
        .first()
        .and_then(|&id| dom::get_attr(html, id, "content"))?;

    let title = OG_TITLE_RE
        .captures(&og_title)
        .and_then(|caps| caps.get(1))
        .map_or_else(
            || {
                og_title
                    .strip_suffix(" / X")
                    .unwrap_or(&og_title)
                    .to_string()
            },
            |m| m.as_str().to_string(),
        );

    // Try display name from <title>, fall back to URL handle
    let author = tweet_author_from_title(html)
        .or_else(|| author_from_url(url))
        .unwrap_or_default();

    Some(ExtractorResult {
        content: String::new(), // tweet content not available in static HTML
        title: Some(title),
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("X (Twitter)".to_string()),
        published: None,
        image: None,
        description: None,
    })
}

/// Extract author display name from the `<title>` element.
/// Matches "(N) Name on X: ..." → "Name".
fn tweet_author_from_title(html: &Html) -> Option<String> {
    use std::sync::LazyLock;
    static NAME_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"(?:\(\d+\)\s+)?(.+?)\s+on\s+X:").expect("x name regex is valid")
    });

    let sel = scraper::Selector::parse("title").ok()?;
    let el = html.select(&sel).next()?;
    let title_text = dom::text_content(html, el.id());

    NAME_RE
        .captures(title_text.trim())
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

fn extract_title(html: &Html) -> String {
    let ids = dom::select_ids(html, TITLE_SEL);
    ids.first().map_or_else(
        || "Untitled X Article".to_string(),
        |&id| dom::text_content(html, id).trim().to_string(),
    )
}

fn extract_author(html: &Html, url: Option<&str>) -> String {
    let author_ids = dom::select_ids(html, AUTHOR_SEL);
    if let Some(&author_id) = author_ids.first() {
        let name = extract_meta_content(html, author_id, AUTHOR_NAME_SEL);
        let handle = extract_meta_content(html, author_id, AUTHOR_HANDLE_SEL);
        if !name.is_empty() && !handle.is_empty() {
            return format!("{name} (@{handle})");
        }
        if !name.is_empty() {
            return name;
        }
        if !handle.is_empty() {
            return handle;
        }
    }
    author_from_url(url).unwrap_or_default()
}

fn extract_meta_content(html: &Html, container_id: ego_tree::NodeId, sel: &str) -> String {
    let ids = dom::select_within(html, container_id, sel);
    ids.first()
        .and_then(|&id| dom::get_attr(html, id, "content"))
        .unwrap_or_default()
}

fn author_from_url(url: Option<&str>) -> Option<String> {
    use std::sync::LazyLock;
    static AUTHOR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"/([a-zA-Z0-9_]{1,15})/(article|status)/\d+")
            .expect("x author regex is valid")
    });

    let u = url?;
    AUTHOR_RE
        .captures(u)
        .and_then(|caps| caps.get(1))
        .map(|m| format!("@{}", m.as_str()))
}

fn extract_header_image(html: &Html, container_id: ego_tree::NodeId) -> String {
    let read_view_ids = dom::select_ids(html, ARTICLE_READ_VIEW_SEL);
    let Some(&read_view_id) = read_view_ids.first() else {
        return String::new();
    };

    let img_ids = dom::select_within(html, read_view_id, IMAGES_SEL);
    let Some(&img_id) = img_ids.first() else {
        return String::new();
    };

    // Skip if image is inside the article container
    if dom::is_ancestor(html, img_id, container_id) {
        return String::new();
    }

    let Some(src) = dom::get_attr(html, img_id, "src") else {
        return String::new();
    };

    let alt = dom::get_attr(html, img_id, "alt").unwrap_or_else(|| "Image".to_string());
    let upgraded_src = upgrade_image_src(&src);

    format!(
        "<img src=\"{}\" alt=\"{}\">",
        html_attr_escape(&upgraded_src),
        html_attr_escape(&alt)
    )
}

fn upgrade_image_src(src: &str) -> String {
    if src.contains("&name=") {
        use std::sync::LazyLock;
        static NAME_RE: LazyLock<regex::Regex> =
            LazyLock::new(|| regex::Regex::new(r"&name=\w+").expect("name param regex is valid"));
        NAME_RE.replace(src, "&name=large").into_owned()
    } else if src.contains('?') {
        format!("{src}&name=large")
    } else {
        format!("{src}?name=large")
    }
}

fn has_selector(html: &Html, sel: &str) -> bool {
    scraper::Selector::parse(sel)
        .ok()
        .is_some_and(|s| html.select(&s).next().is_some())
}

fn html_attr_escape(s: &str) -> String {
    dom::html_attr_escape(s)
}

#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn detect_x_article_by_url_and_selector() {
        let doc = r#"<html><body>
            <div data-testid="twitterArticleRichTextView">
                <p>Article content here</p>
            </div>
        </body></html>"#;
        let html = Html::parse_document(doc);
        assert!(is_x_article(
            &html,
            Some("https://x.com/user/article/12345")
        ));
        assert!(is_x_article(
            &html,
            Some("https://twitter.com/user/article/12345")
        ));
        // Status URLs with article container ARE articles
        assert!(is_x_article(&html, Some("https://x.com/user/status/12345")));
    }

    #[test]
    fn no_detect_without_container() {
        let html = Html::parse_document("<html><body><p>Hello</p></body></html>");
        assert!(!is_x_article(
            &html,
            Some("https://x.com/user/article/12345")
        ));
    }

    #[test]
    fn extract_basic_article() {
        let doc = r#"<html><body>
            <div data-testid="twitter-article-title">My Article</div>
            <div data-testid="twitterArticleRichTextView">
                <p>Article body text</p>
            </div>
        </body></html>"#;
        let html = Html::parse_document(doc);
        let result = extract_x_article(&html, Some("https://x.com/testuser/article/999"));
        let result = result.unwrap();
        assert_eq!(result.title.as_deref(), Some("My Article"));
        assert!(result.content.contains("Article body text"));
        assert!(result.content.contains("x-article"));
        assert_eq!(result.site.as_deref(), Some("X (Twitter)"));
        assert_eq!(result.author.as_deref(), Some("@testuser"));
    }

    #[test]
    fn author_from_url_extraction() {
        assert_eq!(
            author_from_url(Some("https://x.com/johndoe/article/123")),
            Some("@johndoe".to_string())
        );
        assert_eq!(author_from_url(Some("https://example.com")), None);
    }

    #[test]
    fn upgrade_image_src_variants() {
        assert_eq!(
            upgrade_image_src("https://pbs.twimg.com/img?fmt=jpg&name=small"),
            "https://pbs.twimg.com/img?fmt=jpg&name=large"
        );
        assert_eq!(
            upgrade_image_src("https://pbs.twimg.com/img?fmt=jpg"),
            "https://pbs.twimg.com/img?fmt=jpg&name=large"
        );
        assert_eq!(
            upgrade_image_src("https://pbs.twimg.com/img"),
            "https://pbs.twimg.com/img?name=large"
        );
    }

    #[test]
    fn is_tweet_url_detection() {
        assert!(is_tweet_url(Some("https://x.com/user/status/123456")));
        assert!(is_tweet_url(Some("https://twitter.com/user/status/123456")));
        assert!(!is_tweet_url(Some("https://x.com/user/article/123456")));
        assert!(!is_tweet_url(Some("https://example.com")));
        assert!(!is_tweet_url(None));
    }

    #[test]
    fn urlencoding_basics() {
        assert_eq!(
            urlencoding("https://x.com/user/status/123"),
            "https%3A%2F%2Fx.com%2Fuser%2Fstatus%2F123"
        );
        assert_eq!(urlencoding("hello"), "hello");
    }

    #[test]
    #[ignore = "X/Twitter oEmbed API is frequently rate-limited"]
    fn oembed_on_live_tweet() {
        let Some(r) = try_oembed("https://x.com/elikiris/status/1925627023102992830") else {
            panic!("oEmbed API returned None — network unavailable?");
        };
        assert!(!r.content.is_empty(), "oEmbed should return content");
        assert!(r.author.is_some(), "oEmbed should return author");
        assert_eq!(r.site.as_deref(), Some("X (Twitter)"));
    }
}