src/structs/watchhistory.rs

46e7ed5b32a7417aabe4491e60a597344597d330
youtube-tui/src/structs/watchhistory.rs history · blame · raw

93 lines · 3009 bytes

 1use std::{collections::LinkedList, fs};
 2
 3use serde::{Deserialize, Serialize};
 4
 5use crate::app::config::Config;
 6
 7use super::ListItem;
 8
 9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct WatchHistory(pub LinkedList<String>);
11
12
13
14impl Default for WatchHistory {
15    fn default() -> Self {
16        WatchHistory(LinkedList::new())
17    }
18}
19
20impl WatchHistory {
21    pub fn load() -> Self {
22        let mut dir = home::home_dir().expect("Cannot get your home directory");
23        dir = dir.join(".local/share/youtube-tui/watch_history/watch_history.json");
24
25        if !dir.exists() {
26            let out = Self::default();
27
28            fs::write(dir, serde_json::to_string(&out).unwrap())
29                .expect("Cannot write watch history");
30
31            return out;
32        }
33
34        let contents = fs::read_to_string(dir).expect("Cannot read watch history file");
35
36        serde_json::from_str(&contents).expect("Cannot parse watch history file")
37    }
38
39    pub fn push(&mut self, item: String, listitem: ListItem, config: &Config) {
40        if let Some(v) = self.0.front() {
41            if *v == item {
42                return;
43            }
44        }
45        self.0.push_front(item);
46
47        let home_dir = home::home_dir().expect("Cannot get your home directory");
48
49        let mut original_dir = home_dir.join(".cache/youtube-tui/thumbnails");
50
51        match &listitem {
52            ListItem::FullVideo(video) => {
53                let mut save_file = home_dir.join(".local/share/youtube-tui/watch_history/info");
54                save_file.push(format!("{}.json", video.video_id));
55                fs::write(save_file, serde_json::to_string(&listitem).unwrap()).unwrap();
56
57                let file_name = format!("{}.png", video.video_id);
58                original_dir.push(file_name.clone());
59
60                if original_dir.exists() {
61                    let mut new_dir =
62                        home_dir.join(".local/share/youtube-tui/watch_history/thumbnails");
63                    new_dir.push(file_name);
64
65                    if !new_dir.exists() {
66                        fs::copy(original_dir, new_dir).expect("Cannot copy thumbnail");
67                    }
68                }
69            }
70            _ => {}
71        }
72
73        fs::write(
74            home_dir.join(".local/share/youtube-tui/watch_history/watch_history.json"),
75            serde_json::to_string(&self).unwrap(),
76        )
77        .expect("Cannot write watch history");
78
79        if self.0.len() > config.main.max_watch_history {
80            let back = self.0.pop_back();
81            let _ = fs::remove_file(
82                home_dir
83                    .join(".local/share/youtube-tui/watch_history/info")
84                    .join(format!("{}.json", back.clone().unwrap())),
85            );
86            let _ = fs::remove_file(
87                home_dir
88                    .join(".local/share/youtube-tui/watch_history/thumbnails")
89                    .join(format!("{}.png", back.unwrap())),
90            );
91        }
92    }
93}