src/structs/watchhistory.rs

9aedf4a98dfbe60cee47b4e673a37437e309f462
youtube-tui/src/structs/watchhistory.rs history · blame · raw

114 lines · 3974 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<ListItem>);
 11
 12impl Default for WatchHistory {
 13    fn default() -> Self {
 14        WatchHistory(LinkedList::new())
 15    }
 16}
 17
 18impl WatchHistory {
 19    pub fn load() -> Self {
 20        let mut dir = home::home_dir().expect("Cannot get your home directory");
 21        dir = dir.join(".local/share/youtube-tui/watch_history/watch_history.json");
 22
 23        if !dir.exists() {
 24            let out = Self::default();
 25
 26            fs::write(dir, serde_json::to_string(&out).unwrap())
 27                .expect("Cannot write watch history");
 28
 29            return out;
 30        }
 31
 32        let contents = fs::read_to_string(dir).expect("Cannot read watch history file");
 33
 34        serde_json::from_str(&contents).expect("Cannot parse watch history file")
 35    }
 36
 37    pub fn push(&mut self, item: String, listitem: ListItem, config: &Config) {
 38        if let Some(v) = self.0.front() {
 39            if *v.id() == item {
 40                return;
 41            }
 42        }
 43
 44        let home_dir = home::home_dir().expect("Cannot get your home directory");
 45
 46        let mut original_dir = home_dir.join(".cache/youtube-tui/thumbnails");
 47
 48        match &listitem {
 49            ListItem::FullVideo(video) => {
 50                let mut save_file = home_dir.join(".local/share/youtube-tui/watch_history/info");
 51                save_file.push(format!("{}.json", video.video_id));
 52                fs::write(save_file, serde_json::to_string(&listitem).unwrap()).unwrap();
 53
 54                let file_name = format!("{}.png", video.video_id);
 55                original_dir.push(file_name.clone());
 56
 57                if original_dir.exists() {
 58                    let mut new_dir =
 59                        home_dir.join(".local/share/youtube-tui/watch_history/thumbnails");
 60                    new_dir.push(file_name);
 61
 62                    if !new_dir.exists() {
 63                        fs::copy(original_dir, new_dir).expect("Cannot copy thumbnail");
 64                    }
 65                }
 66            }
 67
 68            ListItem::FullPlayList(playlist) => {
 69                let mut save_file = home_dir.join(".local/share/youtube-tui/watch_history/info");
 70                save_file.push(format!("{}.json", playlist.playlist_id));
 71                fs::write(save_file, serde_json::to_string(&listitem).unwrap()).unwrap();
 72
 73                let file_name = format!("{}.png", playlist.playlist_id);
 74                original_dir.push(file_name.clone());
 75
 76                if original_dir.exists() {
 77                    let mut new_dir =
 78                        home_dir.join(".local/share/youtube-tui/watch_history/thumbnails");
 79                    new_dir.push(file_name);
 80
 81                    if !new_dir.exists() {
 82                        fs::copy(original_dir, new_dir).expect("Cannot copy thumbnail");
 83                    }
 84                }
 85            }
 86            _ => {}
 87        }
 88
 89        self.0.push_front(listitem);
 90
 91        if self.0.len() > config.main.max_watch_history {
 92            let back = self.0.pop_back();
 93            if let Some(item) = back {
 94                let id = item.id();
 95                let _ = fs::remove_file(
 96                    home_dir
 97                        .join(".local/share/youtube-tui/watch_history/info")
 98                        .join(format!("{}.json", id.clone())),
 99                );
100                let _ = fs::remove_file(
101                    home_dir
102                        .join(".local/share/youtube-tui/watch_history/thumbnails")
103                        .join(format!("{}.png", id)),
104                );
105            }
106        }
107
108        fs::write(
109            home_dir.join(".local/share/youtube-tui/watch_history/watch_history.json"),
110            serde_json::to_string(&self).unwrap(),
111        )
112        .expect("Cannot write watch history");
113    }
114}