src/eggs.rs

57 lines · 1891 bytes

 1use std::{fs::{self, File}, thread, time::Duration, process::Command, io::Write};
 2
 3use chrono::Datelike;
 4use home::home_dir;
 5
 6use crate::global::functions::fake_rand_range;
 7
 8pub fn egg() {
 9    let date_chrono = chrono::Local::now().date_naive();
10
11    let month = date_chrono.month();
12    let date = date_chrono.day();
13
14    let home_dir = home_dir().unwrap();
15    let displayed_store_path = home_dir.join(".local/share/youtube-tui/eggs/");
16    let video_cache_path = home_dir.join(".cache/youtube-tui/eggs/");
17
18    if !displayed_store_path.exists() {
19        let _ = fs::create_dir_all(displayed_store_path);
20    }
21
22    if !video_cache_path.exists() {
23        let _ = fs::create_dir_all(video_cache_path);
24    }
25
26    match (month, date) {
27        (12, 25) | (12, 26) => if !displayed("christmas") {
28            thread::spawn(|| {
29                thread::sleep(Duration::from_secs(fake_rand_range(30_i64, 250) as u64));
30
31                store("christmas", "happy christmas ;)");
32                let _ = Command::new("mpv").args(["--no-terminal", CHRISTMAS_VIDEOS[fake_rand_range(0_i64, CHRISTMAS_VIDEOS.len() as i64) as usize]]).output();
33            });
34        },
35        _ => {}
36    }
37}
38
39fn displayed(label: &str) -> bool {
40    let home_dir = home_dir().unwrap();
41    let year = chrono::Local::now().date_naive().year();
42    let file_path = home_dir.join(".local/share/youtube-tui/eggs/").join(format!("{label}{year}"));
43
44    file_path.exists()
45}
46
47fn store(label: &str, content: &str) {
48    let home_dir = home_dir().unwrap();
49    let year = chrono::Local::now().date_naive().year();
50    let file_path = home_dir.join(".local/share/youtube-tui/eggs/").join(format!("{label}{year}"));
51
52
53    let mut file = File::create(file_path).unwrap();
54    let _ =file.write_all(content.as_bytes());
55}
56
57const CHRISTMAS_VIDEOS: &[&str] = &["https://youtu.be/YH7LouNgIvE", "https://youtu.be/KsInh3VvE3s"];