auto-ytdlp 1.1.2

Download videos with yt-dlp automatically. You can even download multiple videos at the same time!
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
use crate::app_state::{AppState, FileLockGuard, StateMessage};
use crate::errors::{AppError, Result};
use std::fs;

/// Default filename for the download queue
pub const LINKS_FILE: &str = "links.txt";

/// Internal function to remove a link from file while holding the file lock.
/// This prevents race conditions when multiple workers complete simultaneously.
fn remove_link_from_file_internal(_guard: &FileLockGuard<'_>, url: &str) -> Result<()> {
    let file_path = LINKS_FILE;
    let content = fs::read_to_string(file_path).map_err(AppError::Io)?;

    // Use a temporary file for atomic writes
    let temp_path = format!("{}.tmp", file_path);
    let new_content: Vec<&str> = content
        .lines()
        .filter(|line| line.trim() != url.trim())
        .collect();

    fs::write(&temp_path, new_content.join("\n") + "\n").map_err(AppError::Io)?;
    fs::rename(&temp_path, file_path).map_err(AppError::Io)?; // Atomic replace

    Ok(())
}

/// Removes a specific URL from the 'links.txt' file with thread-safe synchronization.
///
/// This function acquires the file lock from AppState before performing the operation,
/// preventing race conditions when multiple workers complete downloads simultaneously.
///
/// # Parameters
///
/// * `state` - Reference to the application state for file lock access
/// * `url` - The URL to remove from the file
///
/// # Returns
///
/// * `Result<()>` - Ok if the URL was removed successfully, or an Error
pub fn remove_link_from_file_sync(state: &AppState, url: &str) -> Result<()> {
    let guard = state.acquire_file_lock()?;
    remove_link_from_file_internal(&guard, url)
}

/// Loads URLs from the 'links.txt' file without requiring an AppState.
///
/// Reads all lines from the links.txt file into a vector of strings.
/// Creates an empty file if it doesn't exist.
/// Filters out any entries that aren't valid URLs.
///
/// # Returns
///
/// * `Result<Vec<String>>` - Vector containing all valid URLs from the file or an error
///
/// # Example
///
/// ```
/// let links = get_links_from_file()?;
/// state.send(StateMessage::LoadLinks(links))?;
/// ```
pub fn get_links_from_file() -> Result<Vec<String>> {
    let content = fs::read_to_string(LINKS_FILE).map_err(AppError::Io)?;

    Ok(content
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .filter(|l| url::Url::parse(l).is_ok())
        .collect())
}

/// Sanitizes the links.txt file by removing invalid URLs.
///
/// Reads the file, filters out invalid URLs, and writes the sanitized
/// content back to the file.
///
/// # Returns
///
/// * `Result<usize>` - The number of invalid entries that were removed, or an error
///
/// # Example
///
/// ```
/// let removed = sanitize_links_file()?;
/// println!("Removed {} invalid URLs", removed);
/// ```
pub fn sanitize_links_file() -> Result<usize> {
    let file_path = LINKS_FILE;
    let content = fs::read_to_string(file_path).map_err(AppError::Io)?;

    // Single pass: count total non-empty lines and collect valid URLs
    let mut total_non_empty = 0usize;
    let valid_lines: Vec<&str> = content
        .lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
        .inspect(|_| total_non_empty += 1)
        .filter(|l| url::Url::parse(l).is_ok())
        .collect();

    let removed_count = total_non_empty - valid_lines.len();

    if removed_count > 0 {
        let temp_path = format!("{}.tmp", file_path);
        fs::write(&temp_path, valid_lines.join("\n") + "\n").map_err(AppError::Io)?;
        fs::rename(&temp_path, file_path).map_err(AppError::Io)?;
    }

    Ok(removed_count)
}

/// Parses URLs from clipboard content and adds them to both the links.txt file
/// and the application state.
///
/// This function combines adding links to the file and updating the app state directly.
///
/// # Parameters
///
/// * `state` - Reference to the application state to update
/// * `clipboard_content` - String content from the clipboard to parse
///
/// # Returns
///
/// * `Result<usize>` - The number of new URLs that were added, or an error
///
/// # Example
///
/// ```
/// let ctx = ClipboardProvider::new()
///     .map_err(|e| AppError::Clipboard(e.to_string()))?;
///
/// if let Ok(contents) = ctx.get_contents() {
///     let links_added = add_clipboard_links(&state, &contents)?;
///     state.add_log(format!("Added {} links", links_added))?;
/// }
/// ```
pub fn add_clipboard_links(state: &AppState, clipboard_content: &str) -> Result<usize> {
    // Parse and deduplicate new links before writing to file
    let new_links: Vec<String> = {
        let guard = state.acquire_file_lock()?;
        let parsed: Vec<String> = clipboard_content
            .lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .filter(|l| url::Url::parse(l).is_ok())
            .collect();

        let current_links = get_links_from_file()?;
        let existing: std::collections::HashSet<_> = current_links.iter().collect();

        let new: Vec<String> = parsed
            .into_iter()
            .filter(|link| !existing.contains(link))
            .collect();

        if !new.is_empty() {
            let mut all_links = current_links;
            all_links.extend(new.iter().cloned());
            fs::write(LINKS_FILE, all_links.join("\n") + "\n").map_err(AppError::Io)?;
        }

        drop(guard);
        new
    };

    // Only send the new links to the queue
    for link in &new_links {
        state.send(StateMessage::AddToQueue(link.clone()))?;
    }

    Ok(new_links.len())
}

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

    /// Test helper to read links from a specific file path (not the hardcoded "links.txt")
    fn get_links_from_file_at_path(path: &std::path::Path) -> Result<Vec<String>> {
        let content = fs::read_to_string(path).map_err(AppError::Io)?;

        Ok(content
            .lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .filter(|l| url::Url::parse(l).is_ok())
            .collect())
    }

    /// Test helper to sanitize a links file at a specific path
    fn sanitize_links_file_at_path(path: &std::path::Path) -> Result<usize> {
        let content = fs::read_to_string(path).map_err(AppError::Io)?;

        let mut total_non_empty = 0usize;
        let valid_lines: Vec<&str> = content
            .lines()
            .map(|l| l.trim())
            .filter(|l| !l.is_empty())
            .inspect(|_| total_non_empty += 1)
            .filter(|l| url::Url::parse(l).is_ok())
            .collect();

        let removed_count = total_non_empty - valid_lines.len();

        if removed_count > 0 {
            fs::write(path, valid_lines.join("\n")).map_err(AppError::Io)?;
        }

        Ok(removed_count)
    }

    #[test]
    fn test_get_links_from_file_valid_urls() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://example.com/video1\nhttps://youtube.com/watch?v=abc123\n";
        fs::write(&links_path, content).unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert_eq!(links.len(), 2);
        assert!(links.contains(&"https://example.com/video1".to_string()));
        assert!(links.contains(&"https://youtube.com/watch?v=abc123".to_string()));
    }

    #[test]
    fn test_get_links_from_file_invalid_urls_filtered() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://example.com/video1\nnot-a-valid-url\nhttps://example.com/video2\n";
        fs::write(&links_path, content).unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert_eq!(links.len(), 2);
        assert!(!links.iter().any(|l| l == "not-a-valid-url"));
    }

    #[test]
    fn test_get_links_from_file_empty_file() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        fs::write(&links_path, "").unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert!(links.is_empty());
    }

    #[test]
    fn test_get_links_from_file_whitespace_and_blank_lines() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "  https://example.com/video1  \n\n\n   \nhttps://example.com/video2\n\n";
        fs::write(&links_path, content).unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert_eq!(links.len(), 2);
        assert_eq!(links[0], "https://example.com/video1");
        assert_eq!(links[1], "https://example.com/video2");
    }

    #[test]
    fn test_get_links_from_file_unicode_urls() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://example.com/video?title=%E4%B8%AD%E6%96%87\n";
        fs::write(&links_path, content).unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert_eq!(links.len(), 1);
    }

    #[test]
    fn test_sanitize_links_file_removes_invalid() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content =
            "https://example.com/video1\ninvalid-url\nhttps://example.com/video2\nalso-invalid\n";
        fs::write(&links_path, content).unwrap();

        let removed = sanitize_links_file_at_path(&links_path).unwrap();
        assert_eq!(removed, 2);

        // Verify file content
        let remaining = fs::read_to_string(&links_path).unwrap();
        assert!(remaining.contains("https://example.com/video1"));
        assert!(remaining.contains("https://example.com/video2"));
        assert!(!remaining.contains("invalid-url"));
        assert!(!remaining.contains("also-invalid"));
    }

    #[test]
    fn test_sanitize_links_file_no_invalid_urls() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://example.com/video1\nhttps://example.com/video2\n";
        fs::write(&links_path, content).unwrap();

        let removed = sanitize_links_file_at_path(&links_path).unwrap();
        assert_eq!(removed, 0);
    }

    #[test]
    fn test_sanitize_links_file_returns_count() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://valid.com\nbad1\nbad2\nbad3\nhttps://also-valid.com\n";
        fs::write(&links_path, content).unwrap();

        let removed = sanitize_links_file_at_path(&links_path).unwrap();
        assert_eq!(removed, 3);
    }

    #[test]
    fn test_get_links_file_not_found() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        // Don't create links.txt
        let result = get_links_from_file_at_path(&links_path);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_links_very_long_urls() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        // Create a very long but valid URL
        let long_path = "a".repeat(500);
        let long_url = format!("https://example.com/{}", long_path);
        fs::write(&links_path, &long_url).unwrap();

        let links = get_links_from_file_at_path(&links_path).unwrap();
        assert_eq!(links.len(), 1);
        assert_eq!(links[0], long_url);
    }

    #[test]
    fn test_sanitize_links_file_preserves_valid_content() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");

        let content = "https://example.com/a\nbad\nhttps://example.com/b\n";
        fs::write(&links_path, content).unwrap();

        let removed = sanitize_links_file_at_path(&links_path).unwrap();
        assert_eq!(removed, 1);

        // Original file should still contain valid URLs
        let result = fs::read_to_string(&links_path).unwrap();
        assert!(result.contains("https://example.com/a"));
        assert!(result.contains("https://example.com/b"));
        assert!(!result.contains("bad"));
    }

    #[test]
    fn test_sanitize_links_file_no_temp_file_left_behind() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let links_path = temp_dir.path().join("links.txt");
        let temp_path = temp_dir.path().join("links.txt.tmp");

        let content = "https://example.com/a\nbad\n";
        fs::write(&links_path, content).unwrap();

        sanitize_links_file_at_path(&links_path).unwrap();

        // No temp file should remain after the operation
        assert!(!temp_path.exists());
    }

    /// Helper that mirrors the dedup logic used in add_clipboard_links
    fn deduplicate_links(existing: &[String], clipboard: &str) -> Vec<String> {
        let parsed: Vec<String> = clipboard
            .lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .filter(|l| url::Url::parse(l).is_ok())
            .collect();

        let existing_set: std::collections::HashSet<_> = existing.iter().collect();

        parsed
            .into_iter()
            .filter(|link| !existing_set.contains(link))
            .collect()
    }

    #[test]
    fn test_deduplicate_links_no_duplicates() {
        let existing = vec!["https://example.com/a".to_string()];
        let clipboard = "https://example.com/b\nhttps://example.com/c\n";

        let new = deduplicate_links(&existing, clipboard);
        assert_eq!(new.len(), 2);
        assert!(new.contains(&"https://example.com/b".to_string()));
        assert!(new.contains(&"https://example.com/c".to_string()));
    }

    #[test]
    fn test_deduplicate_links_all_duplicates() {
        let existing = vec![
            "https://example.com/a".to_string(),
            "https://example.com/b".to_string(),
        ];
        let clipboard = "https://example.com/a\nhttps://example.com/b\n";

        let new = deduplicate_links(&existing, clipboard);
        assert!(new.is_empty());
    }

    #[test]
    fn test_deduplicate_links_mixed() {
        let existing = vec!["https://example.com/a".to_string()];
        let clipboard = "https://example.com/a\nhttps://example.com/b\n";

        let new = deduplicate_links(&existing, clipboard);
        assert_eq!(new.len(), 1);
        assert_eq!(new[0], "https://example.com/b");
    }

    #[test]
    fn test_deduplicate_links_filters_invalid_urls() {
        let existing: Vec<String> = vec![];
        let clipboard = "https://example.com/a\nnot-a-url\nhttps://example.com/b\n";

        let new = deduplicate_links(&existing, clipboard);
        assert_eq!(new.len(), 2);
        assert!(!new.iter().any(|l| l == "not-a-url"));
    }

    #[test]
    fn test_deduplicate_links_empty_clipboard() {
        let existing = vec!["https://example.com/a".to_string()];
        let clipboard = "";

        let new = deduplicate_links(&existing, clipboard);
        assert!(new.is_empty());
    }

    #[test]
    fn test_deduplicate_links_whitespace_only_clipboard() {
        let existing: Vec<String> = vec![];
        let clipboard = "   \n\n  \n";

        let new = deduplicate_links(&existing, clipboard);
        assert!(new.is_empty());
    }

    #[test]
    fn test_deduplicate_links_trims_whitespace() {
        let existing: Vec<String> = vec![];
        let clipboard = "  https://example.com/a  \n  https://example.com/b  \n";

        let new = deduplicate_links(&existing, clipboard);
        assert_eq!(new.len(), 2);
        assert_eq!(new[0], "https://example.com/a");
        assert_eq!(new[1], "https://example.com/b");
    }
}