Skip to content

Commit

Permalink
fix: wrong cookies path on Windows
Browse files Browse the repository at this point in the history
And add missing custom ytdl execute file path function.
  • Loading branch information
akiirui committed Jul 15, 2024
1 parent 1d647d4 commit 9a15ee5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 80 deletions.
80 changes: 38 additions & 42 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use std::path::PathBuf;
pub struct Config {
#[serde(default = "default_mpv")]
pub mpv: String,
#[serde(default = "default_ytdl")]
pub ytdl: String,
pub ytdl: Option<String>,
pub proxy: Option<String>,
}

Expand All @@ -21,51 +20,56 @@ impl Config {
///
/// If config file doesn't exists, returns default value
pub fn load() -> Result<Config, Error> {
let path = config_path()?;
if let Some(path) = get_config_file() {
if path.exists() {
let data: String = std::fs::read_to_string(&path)?;
let config: Config = toml::from_str(&data)?;

if path.exists() {
let data: String = std::fs::read_to_string(&path)?;
let config: Config = toml::from_str(&data)?;

return Ok(config);
return Ok(config);
}
}

Ok(default_config())
}
}

#[cfg(unix)]
/// Returns a path of config on Unix
fn config_path() -> Result<PathBuf, Error> {
let mut path: PathBuf;
/// Returns config directory path of mpv-handler
pub fn get_config_dir() -> Option<PathBuf> {
#[cfg(unix)]
{
if let Some(mut v) = dirs::config_dir() {
v.push("mpv-handler");
return Some(v);
}
}

path = match dirs::config_dir() {
Some(path) => path,
None => return Err(Error::FailedGetConfigDir),
};
path.push("mpv-handler");
path.push("config.toml");
#[cfg(windows)]
{
if let Ok(mut v) = std::env::current_exe() {
v.pop();
return Some(v);
}
}

Ok(path)
eprintln!("Failed to get config directory");
None
}

#[cfg(windows)]
/// Returns a path of config on Windows
fn config_path() -> Result<PathBuf, Error> {
let mut path: PathBuf;

path = std::env::current_exe()?;
path.pop();
path.push("config.toml");

Ok(path)
/// Returns a path of config
fn get_config_file() -> Option<PathBuf> {
match get_config_dir() {
Some(mut p) => {
p.push("config.toml");
Some(p)
}
None => None,
}
}

/// The defalut value of `Config`
fn default_config() -> Config {
Config {
mpv: default_mpv(),
ytdl: default_ytdl(),
ytdl: None,
proxy: None,
}
}
Expand All @@ -78,14 +82,6 @@ fn default_mpv() -> String {
return "mpv.com".to_string();
}

/// The default value of `Config.ytdl`
fn default_ytdl() -> String {
#[cfg(unix)]
return "yt-dlp".to_string();
#[cfg(windows)]
return "yt-dlp.exe".to_string();
}

#[test]
fn test_config_parse() {
let config: Config = toml::from_str(
Expand All @@ -98,7 +94,7 @@ fn test_config_parse() {
.unwrap();

assert_eq!(config.mpv, "/usr/bin/mpv");
assert_eq!(config.ytdl, "/usr/bin/yt-dlp");
assert_eq!(config.ytdl, Some("/usr/bin/yt-dlp".to_string()));
assert_eq!(config.proxy, Some("http://example.com:8080".to_string()));

let config: Config = toml::from_str(
Expand All @@ -113,13 +109,13 @@ fn test_config_parse() {
#[cfg(unix)]
{
assert_eq!(config.mpv, "mpv");
assert_eq!(config.ytdl, "yt-dlp");
assert_eq!(config.ytdl, None);
assert_eq!(config.proxy, None);
}
#[cfg(windows)]
{
assert_eq!(config.mpv, "mpv.com");
assert_eq!(config.ytdl, "yt-dlp.exe");
assert_eq!(config.ytdl, None);
assert_eq!(config.proxy, None);
}
}
5 changes: 1 addition & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ pub enum Error {
IncorrectVideoURL(String),
#[error("Dangerous video protocol \"{0}\"")]
DangerousVideoProtocol(String),
#[cfg(unix)]
#[error("Failed to get config directory")]
FailedGetConfigDir,
#[error("Player exited by error")]
PlayerExited(u8),
#[error("Failed to run player. {0}")]
#[error("Failed to run player ({0})")]
PlayerRunFailed(std::io::Error),
#[error("Failed to decode ({0})")]
FromBase64Error(#[from] base64::DecodeError),
Expand Down
16 changes: 5 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,11 @@ fn run() -> Result<(), Error> {
_ => return Err(Error::TooManyArgs),
};

match arg {
"-v" | "--version" => Ok(print_usage()),
_ => {
let proto = Protocol::parse(arg)?;
let config = Config::load()?;
let proto = Protocol::parse(arg)?;
let config = Config::load()?;

match proto.plugin {
Plugins::Play => crate::plugins::play::exec(&proto, &config),
}
}
match proto.plugin {
Plugins::Play => crate::plugins::play::exec(&proto, &config),
}
}

Expand All @@ -49,8 +44,7 @@ fn print_usage() {
let version: &str = option_env!("MPV_HANDLER_VERSION").unwrap_or(env!("CARGO_PKG_VERSION"));

println!("mpv-handler {}\n", version);
println!("Usage:\n {}\n", "mpv-handler [options] <url>",);
println!("OPTIONS:\n {} {}", "-v, --version", "show version");
println!("Usage:\n {}\n", "mpv-handler <url>",);
}

/// Print error
Expand Down
50 changes: 27 additions & 23 deletions src/plugins/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PREFIX_COOKIES: &str = "--ytdl-raw-options-append=cookies=";
const PREFIX_PROFILE: &str = "--profile=";
const PREFIX_FORMATS: &str = "--ytdl-raw-options-append=format-sort=";
const PREFIX_SUBFILE: &str = "--sub-file=";
const PREFIX_YT_PATH: &str = "--script-opts=ytdl_hook-ytdl_path=";

/// Execute player with given options
pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {
Expand All @@ -14,6 +15,7 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {
let option_profile: String;
let option_formats: String;
let option_subfile: String;
let option_scripts: String;

// Append cookies option
if let Some(v) = proto.cookies {
Expand Down Expand Up @@ -49,6 +51,12 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {
#[cfg(unix)]
std::env::remove_var("LD_LIBRARY_PATH");

// Set custom ytdl execute file path
if let Some(v) = &config.ytdl {
option_scripts = scripts(v);
options.push(&option_scripts);
}

// Set HTTP(S) proxy environment variables
if let Some(proxy) = &config.proxy {
std::env::set_var("http_proxy", proxy);
Expand Down Expand Up @@ -92,29 +100,20 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {

/// Return cookies option
fn cookies(cookies: &str) -> Option<String> {
let mut p: std::path::PathBuf;

#[cfg(unix)]
{
p = dirs::config_dir().unwrap();
p.push("mpv-handler");
p.push("cookies");
p.push(cookies);
}

#[cfg(windows)]
{
p = std::env::current_exe().unwrap();
p.push("cookies");
p.push(cookies);
}

if p.exists() {
let cookies = p.display();
Some(format!("{PREFIX_COOKIES}{cookies}"))
} else {
eprintln!("Cookies file not found: {}", p.display());
None
match crate::config::get_config_dir() {
Some(mut p) => {
p.push("cookies");
p.push(cookies);

if p.exists() {
let cookies = p.display();
return Some(format!("{PREFIX_COOKIES}{cookies}"));
} else {
eprintln!("Cookies file not found \"{}\"", p.display());
return None;
}
}
None => None,
}
}

Expand Down Expand Up @@ -147,6 +146,11 @@ fn subfile(subfile: &str) -> String {
format!("{PREFIX_SUBFILE}{subfile}")
}

/// Return scripts option
fn scripts(scripts: &str) -> String {
format!("{PREFIX_YT_PATH}{scripts}")
}

#[test]
fn test_profile_option() {
let option_profile = profile("low-latency");
Expand Down

0 comments on commit 9a15ee5

Please sign in to comment.