Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't exit if the cookies file doesn't exist #51

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub enum Error {
#[cfg(unix)]
#[error("Failed to get config directory")]
FailedGetConfigDir,
#[error("Cookies file \"{0}\" doesn't exist")]
CookiesFileNotFound(String),
#[error("Player exited by error")]
PlayerExited(u8),
#[error("Failed to run player. {0}")]
Expand Down
45 changes: 17 additions & 28 deletions src/plugins/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {

// Append cookies option
if let Some(v) = proto.cookies {
option_cookies = cookies(v)?;
options.push(&option_cookies);
if let Some(v) = cookies(v) {
option_cookies = v;
options.push(&option_cookies);
}
}

// Append profile option
Expand All @@ -29,8 +31,10 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {

// Append formats option
if proto.quality.is_some() || proto.v_codec.is_some() {
option_formats = formats(proto.quality, proto.v_codec)?;
options.push(&option_formats);
if let Some(v) = formats(proto.quality, proto.v_codec) {
option_formats = v;
options.push(&option_formats);
}
}

// Append subfile option
Expand Down Expand Up @@ -87,15 +91,12 @@ pub fn exec(proto: &Protocol, config: &Config) -> Result<(), Error> {
}

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

#[cfg(unix)]
{
p = match dirs::config_dir() {
Some(path) => path,
None => return Err(Error::FailedGetConfigDir),
};
p = dirs::config_dir().unwrap();
p.push("mpv-handler");
p.push("cookies");
p.push(cookies);
Expand All @@ -109,12 +110,11 @@ fn cookies(cookies: &str) -> Result<String, Error> {
p.push(cookies);
}

let cookies = p.display();

if p.exists() {
Ok(format!("{PREFIX_COOKIES}{cookies}"))
let cookies = p.display();
Some(format!("{PREFIX_COOKIES}{cookies}"))
} else {
Err(Error::CookiesFileNotFound(cookies.to_string()))
None
}
}

Expand All @@ -124,24 +124,13 @@ fn profile(profile: &str) -> String {
}

/// Return formats option
fn formats(quality: Option<&str>, v_codec: Option<&str>) -> Result<String, Error> {
fn formats(quality: Option<&str>, v_codec: Option<&str>) -> Option<String> {
let mut f: Vec<String> = Vec::new();
let formats: String;

if let Some(v) = quality {
let i = match v {
"2160p" => 2160,
"1440p" => 1440,
"1080p" => 1080,
"720p" => 720,
"480p" => 480,
"360p" => 360,
_ => -1,
};

if i != -1 {
f.push(format!("res:{i}"));
}
let i: String = v.matches(char::is_numeric).collect();
f.push(format!("res:{i}"));
}

if let Some(v) = v_codec {
Expand All @@ -150,7 +139,7 @@ fn formats(quality: Option<&str>, v_codec: Option<&str>) -> Result<String, Error

formats = f.join(",");

Ok(format!("{PREFIX_FORMATS}{formats}"))
Some(format!("{PREFIX_FORMATS}{formats}"))
}

/// Return subfile option
Expand Down