aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2024-01-20 15:52:39 +0100
committerSoispha <soispha@vhack.eu>2024-01-20 15:57:56 +0100
commitba3273c9adffee3d8538aecee18b39374bf6e195 (patch)
treeeb292946e997102f0340f85a26f65f7f0f839f67
parentfix(sys/nixpkgs/yt/{yt,ytc}): Ignore sponsor block API access errors (diff)
downloadnixos-config-ba3273c9adffee3d8538aecee18b39374bf6e195.zip
feat(sys/nixpkgs/yt/{yt,ytc}): Persist old selection file
-rw-r--r--sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs73
-rw-r--r--sys/nixpkgs/pkgs/yt/src/constants.rs13
-rw-r--r--sys/nixpkgs/pkgs/yt/src/lib.rs30
3 files changed, 70 insertions, 46 deletions
diff --git a/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs b/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
index 54d89daa..ae1bcacd 100644
--- a/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
+++ b/sys/nixpkgs/pkgs/yt/src/bin/yt/main.rs
@@ -1,14 +1,14 @@
use anyhow::{bail, Context, Result};
use std::{
- env,
+ env, fs,
io::{BufRead, BufReader, BufWriter, Write},
process::Command as StdCmd,
};
-use tempfile::NamedTempFile;
+use tempfile::Builder;
use yt::{
- constants::HELP_STR,
+ constants::{last_select, HELP_STR},
downloader::{Downloadable, Downloader},
- ytcc_drop, Duration, Line, LineCommand, YtccListData,
+ ytcc_drop, Line, LineCommand, YtccListData,
};
fn main() -> Result<()> {
@@ -31,46 +31,43 @@ fn main() -> Result<()> {
.context("Failed to deserialize json output")?
};
- let temp_file = NamedTempFile::new().context("Failed to get tempfile")?;
- let mut edit_file = BufWriter::new(&temp_file);
+ let temp_file = Builder::new()
+ .prefix("yt_video_select")
+ .suffix(".yts")
+ .rand_bytes(6)
+ .tempfile()
+ .context("Failed to get tempfile")?;
- json_map
- .iter()
- .map(|line| {
- format!(
- r#"pick {} "{}" "{}" "{}" "{}" "{}"{}"#,
- line.id,
- line.title.replace('"', "'"),
- line.publish_date,
- line.playlists
- .iter()
- .map(|p| p.name.replace('"', "'"))
- .collect::<Vec<String>>()
- .join(", "),
- Duration::from(line.duration.trim()),
- line.url.replace('"', "'"),
- "\n"
- )
- })
- .for_each(|line| {
- edit_file
- .write(line.as_bytes())
- .expect("This write should not fail");
- });
+ {
+ let mut edit_file = BufWriter::new(&temp_file);
- edit_file.write(HELP_STR.as_bytes())?;
- edit_file.flush().context("Failed to flush edit file")?;
+ json_map
+ .iter()
+ .map(|line| line.to_string())
+ .for_each(|line| {
+ edit_file
+ .write(line.as_bytes())
+ .expect("This write should not fail");
+ });
- let read_file = temp_file.reopen()?;
-
- let mut nvim = StdCmd::new("nvim");
- nvim.arg(temp_file.path());
+ edit_file.write(HELP_STR.as_bytes())?;
+ edit_file.flush().context("Failed to flush edit file")?;
- let status = nvim.status().context("Falied to run nvim")?;
- if !status.success() {
- bail!("nvim exited with error status: {}", status)
+ let mut nvim = StdCmd::new("nvim");
+ nvim.arg(temp_file.path());
+ let status = nvim.status().context("Falied to run nvim")?;
+ if !status.success() {
+ bail!("nvim exited with error status: {}", status)
+ }
}
+ let read_file = temp_file.reopen()?;
+ fs::copy(
+ temp_file.path(),
+ last_select().context("Failed to get persistent the selection file path")?,
+ )
+ .context("Failed to persist selection file")?;
+
let mut watching = Vec::new();
let reader = BufReader::new(&read_file);
for line in reader.lines() {
diff --git a/sys/nixpkgs/pkgs/yt/src/constants.rs b/sys/nixpkgs/pkgs/yt/src/constants.rs
index 6965ce63..96995f08 100644
--- a/sys/nixpkgs/pkgs/yt/src/constants.rs
+++ b/sys/nixpkgs/pkgs/yt/src/constants.rs
@@ -25,7 +25,6 @@ pub const CONCURRENT: u32 = 5;
pub const DOWNLOAD_DIR: &str = "/tmp/ytcc";
const STATUS_PATH: &str = "ytcc/running";
-
pub fn status_path() -> anyhow::Result<PathBuf> {
let out: PathBuf = format!(
"{}/{}",
@@ -36,3 +35,15 @@ pub fn status_path() -> anyhow::Result<PathBuf> {
fs::create_dir_all(&out.parent().expect("Parent should exist"))?;
Ok(out)
}
+
+const LAST_SELECT: &str = "ytcc/selected";
+pub fn last_select() -> anyhow::Result<PathBuf> {
+ let out: PathBuf = format!(
+ "{}/{}",
+ env::var("XDG_RUNTIME_DIR").expect("This should always exist"),
+ LAST_SELECT
+ )
+ .into();
+ fs::create_dir_all(&out.parent().expect("Parent should exist"))?;
+ Ok(out)
+}
diff --git a/sys/nixpkgs/pkgs/yt/src/lib.rs b/sys/nixpkgs/pkgs/yt/src/lib.rs
index a08b32db..2571b6b6 100644
--- a/sys/nixpkgs/pkgs/yt/src/lib.rs
+++ b/sys/nixpkgs/pkgs/yt/src/lib.rs
@@ -18,6 +18,27 @@ pub struct YtccListData {
pub id: u32,
pub playlists: Vec<YtccPlaylistData>,
}
+
+impl std::fmt::Display for YtccListData {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
+ write!(
+ f,
+ r#"pick {} "{}" "{}" "{}" "{}" "{}"{}"#,
+ self.id,
+ self.title.replace('"', "'"),
+ self.publish_date,
+ self.playlists
+ .iter()
+ .map(|p| p.name.replace('"', "'"))
+ .collect::<Vec<String>>()
+ .join(", "),
+ Duration::from(self.duration.trim()),
+ self.url.replace('"', "'"),
+ "\n"
+ )
+ }
+}
+
#[derive(Deserialize)]
pub struct YtccPlaylistData {
pub name: String,
@@ -80,13 +101,8 @@ impl From<&str> for Duration {
fn from(v: &str) -> Self {
let buf: Vec<_> = v.split(':').take(2).collect();
Self {
- time: (buf[0]
- .parse::<u32>()
- .expect("Should be a number")
- * 60)
- + buf[1]
- .parse::<u32>()
- .expect("Should be a number"),
+ time: (buf[0].parse::<u32>().expect("Should be a number") * 60)
+ + buf[1].parse::<u32>().expect("Should be a number"),
}
}
}