swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
348 lines · 11.0 KB
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
/**
 * 22chan HTML Scraper Commands
 * 
 * Backend commands for fetching data from 22chan.org
 * 
 * 22chan uses a custom Django/Python stack with no JSON API,
 * so we use HTML scraping to extract data.
 */

use reqwest::Client;
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};

const TWENTYTWOCHAN_BASE: &str = "https://22chan.org";

/// 22chan boards - hardcoded since there's no API
const TWENTYTWOCHAN_BOARDS: &[(&str, &str, bool)] = &[
    ("a", "Anime & Manga", false),
    ("b", "Random", true),
    ("cat", "Cinema", false),
    ("co", "Comics & Cartoons", false),
    ("fit", "Fitness", false),
    ("k", "Weapons", false),
    ("lit", "Literature", false),
    ("meta", "Meta", false),
    ("mu", "Music", false),
    ("out", "Outdoors", false),
    ("pol", "Politics", true),
    ("sci", "Science", false),
    ("tech", "Technology", false),
    ("v", "Video Games General", false),
    ("vg", "Video Games", false),
    ("w", "Wallpapers", false),
];

#[derive(Debug, Serialize, Deserialize)]
pub struct TwentyTwoChanBoard {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub nsfw: bool,
    pub category: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TwentyTwoChanPost {
    pub no: u64,
    pub resto: u64,
    pub time: u64,
    pub name: Option<String>,
    pub trip: Option<String>,
    pub sub: Option<String>,
    pub com: Option<String>,
    pub tim: Option<String>,
    pub ext: Option<String>,
    pub filename: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TwentyTwoChanThread {
    pub posts: Vec<TwentyTwoChanPost>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TwentyTwoChanCatalogThread {
    pub no: u64,
    pub sub: Option<String>,
    pub com: Option<String>,
    pub tim: Option<String>,
    pub ext: Option<String>,
    pub replies: u32,
    pub images: u32,
    pub time: u64,
    pub name: Option<String>,
}

/// Get list of available boards on 22chan
#[tauri::command]
pub async fn fetch_twentytwochan_boards() -> Result<Vec<TwentyTwoChanBoard>, String> {
    let boards: Vec<TwentyTwoChanBoard> = TWENTYTWOCHAN_BOARDS
        .iter()
        .map(|(id, name, nsfw)| TwentyTwoChanBoard {
            id: id.to_string(),
            name: name.to_string(),
            description: Some(format!("22chan /{}/", id)),
            nsfw: *nsfw,
            category: "22chan".to_string(),
        })
        .collect();
    
    Ok(boards)
}

/// Fetch catalog from 22chan by scraping the board page
#[tauri::command]
pub async fn fetch_twentytwochan_catalog(board: String) -> Result<Vec<TwentyTwoChanCatalogThread>, String> {
    let client = Client::builder()
        .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        .build()
        .map_err(|e| e.to_string())?;
    
    let url = format!("{}/{}/", TWENTYTWOCHAN_BASE, board);
    
    let response = client.get(&url).send().await.map_err(|e| e.to_string())?;
    
    if !response.status().is_success() {
        return Err(format!("Failed to fetch board: HTTP {}", response.status()));
    }
    
    let html = response.text().await.map_err(|e| e.to_string())?;
    let document = Html::parse_document(&html);
    
    // Selectors for 22chan
    let thread_selector = Selector::parse(".thread").map_err(|e| e.to_string())?;
    let subject_selector = Selector::parse(".subject").map_err(|e| e.to_string())?;
    let name_selector = Selector::parse(".name").map_err(|e| e.to_string())?;
    let timestamp_selector = Selector::parse(".timestamp").map_err(|e| e.to_string())?;
    let image_selector = Selector::parse("a[href*='/UserMedia/uploads/']").map_err(|e| e.to_string())?;
    let inner_selector = Selector::parse(".inner").map_err(|e| e.to_string())?;
    
    let mut threads = Vec::new();
    
    for thread_el in document.select(&thread_selector) {
        // Get thread ID from data-slug attribute
        let thread_id: u64 = thread_el
            .value()
            .attr("data-slug")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        if thread_id == 0 {
            continue;
        }
        
        // Get subject
        let subject = thread_el
            .select(&subject_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        // Get author name
        let name = thread_el
            .select(&name_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        // Get timestamp (convert to unix timestamp if possible)
        let timestamp_text = thread_el
            .select(&timestamp_selector)
            .next()
            .map(|el| el.text().collect::<String>());
        
        // Get image URL
        let (tim, ext) = thread_el
            .select(&image_selector)
            .next()
            .and_then(|el| el.value().attr("href"))
            .map(|href| {
                let filename = href.rsplit('/').next().unwrap_or("");
                let parts: Vec<&str> = filename.rsplitn(2, '.').collect();
                if parts.len() == 2 {
                    (Some(parts[1].to_string()), Some(format!(".{}", parts[0])))
                } else {
                    (None, None)
                }
            })
            .unwrap_or((None, None));
        
        // Get content preview
        let content = thread_el
            .select(&inner_selector)
            .next()
            .map(|el| {
                el.text()
                    .collect::<String>()
                    .lines()
                    .skip(1) // Skip header info
                    .take(5) // Take first few lines
                    .collect::<Vec<&str>>()
                    .join("\n")
                    .trim()
                    .to_string()
            });
        
        // Parse reply count from header text (looking for ★ N pattern)
        let header_text = thread_el.text().collect::<String>();
        let replies = header_text
            .split('★')
            .nth(1)
            .and_then(|s| s.trim().split_whitespace().next())
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        threads.push(TwentyTwoChanCatalogThread {
            no: thread_id,
            sub: subject,
            com: content,
            tim,
            ext,
            replies,
            images: 0,
            time: 0, // Would need to parse timestamp
            name,
        });
    }
    
    Ok(threads)
}

/// Fetch a thread from 22chan by scraping the thread page
#[tauri::command]
pub async fn fetch_twentytwochan_thread(board: String, thread_id: u64) -> Result<TwentyTwoChanThread, String> {
    let client = Client::builder()
        .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        .build()
        .map_err(|e| e.to_string())?;
    
    let url = format!("{}/{}/{}/", TWENTYTWOCHAN_BASE, board, thread_id);
    
    let response = client.get(&url).send().await.map_err(|e| e.to_string())?;
    
    if !response.status().is_success() {
        return Err(format!("Thread not found: HTTP {}", response.status()));
    }
    
    let html = response.text().await.map_err(|e| e.to_string())?;
    let document = Html::parse_document(&html);
    
    // Selectors - OP is .thread, replies are .reply
    let op_selector = Selector::parse(".thread").map_err(|e| e.to_string())?;
    let reply_selector = Selector::parse(".reply").map_err(|e| e.to_string())?;
    let name_selector = Selector::parse(".name").map_err(|e| e.to_string())?;
    let subject_selector = Selector::parse(".subject").map_err(|e| e.to_string())?;
    let image_selector = Selector::parse("a[href*='/UserMedia/uploads/']").map_err(|e| e.to_string())?;
    let inner_selector = Selector::parse(".inner").map_err(|e| e.to_string())?;
    
    let mut posts = Vec::new();
    
    // Parse OP
    if let Some(op_el) = document.select(&op_selector).next() {
        let post_id: u64 = op_el
            .value()
            .attr("data-slug")
            .and_then(|s| s.parse().ok())
            .unwrap_or(thread_id);
        
        let subject = op_el
            .select(&subject_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        let name = op_el
            .select(&name_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        let (tim, ext, filename) = op_el
            .select(&image_selector)
            .next()
            .and_then(|el| el.value().attr("href"))
            .map(|href| {
                let fname = href.rsplit('/').next().unwrap_or("");
                let parts: Vec<&str> = fname.rsplitn(2, '.').collect();
                if parts.len() == 2 {
                    (Some(parts[1].to_string()), Some(format!(".{}", parts[0])), Some(fname.to_string()))
                } else {
                    (None, None, None)
                }
            })
            .unwrap_or((None, None, None));
        
        let content = op_el
            .select(&inner_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        posts.push(TwentyTwoChanPost {
            no: post_id,
            resto: 0,
            time: 0,
            name,
            trip: None,
            sub: subject,
            com: content,
            tim,
            ext,
            filename,
        });
    }
    
    // Parse replies
    for reply_el in document.select(&reply_selector) {
        let post_id: u64 = reply_el
            .value()
            .attr("data-slug")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        
        if post_id == 0 {
            continue;
        }
        
        let name = reply_el
            .select(&name_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        let (tim, ext, filename) = reply_el
            .select(&image_selector)
            .next()
            .and_then(|el| el.value().attr("href"))
            .map(|href| {
                let fname = href.rsplit('/').next().unwrap_or("");
                let parts: Vec<&str> = fname.rsplitn(2, '.').collect();
                if parts.len() == 2 {
                    (Some(parts[1].to_string()), Some(format!(".{}", parts[0])), Some(fname.to_string()))
                } else {
                    (None, None, None)
                }
            })
            .unwrap_or((None, None, None));
        
        let content = reply_el
            .select(&inner_selector)
            .next()
            .map(|el| el.text().collect::<String>().trim().to_string());
        
        posts.push(TwentyTwoChanPost {
            no: post_id,
            resto: thread_id,
            time: 0,
            name,
            trip: None,
            sub: None,
            com: content,
            tim,
            ext,
            filename,
        });
    }
    
    if posts.is_empty() {
        return Err("Thread not found or empty".to_string());
    }
    
    Ok(TwentyTwoChanThread { posts })
}