Create get_embedded_art function, and add some dependencies for config system

This commit is contained in:
Anas Elgarhy 2023-02-12 20:39:10 +02:00
parent cb5c02d0b9
commit c7be10cd61
No known key found for this signature in database
GPG key ID: 0501802A1D496528
6 changed files with 113 additions and 12 deletions

View file

@ -5,6 +5,12 @@
<value-preview-text-wrapping value="false" />
<value-preview-pinned value="false" />
</component>
<component name="DBNavigator.Project.DatabaseBrowserManager">
<autoscroll-to-editor value="false" />
<autoscroll-from-editor value="true" />
<show-object-properties value="true" />
<loaded-nodes />
</component>
<component name="DBNavigator.Project.DatabaseEditorStateManager">
<last-used-providers />
</component>
@ -14,6 +20,9 @@
<component name="DBNavigator.Project.ExecutionManager">
<retain-sticky-names value="false" />
</component>
<component name="DBNavigator.Project.ParserDiagnosticsManager">
<diagnostics-history />
</component>
<component name="DBNavigator.Project.Settings">
<connections />
<browser-settings>

50
Cargo.lock generated
View file

@ -206,9 +206,12 @@ name = "cmus-notify"
version = "0.1.0"
dependencies = [
"clap",
"confy",
"id3",
"lrc",
"notify-rust",
"regex",
"temp-file",
"test-context",
"typed-builder",
]
@ -228,6 +231,18 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "confy"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e37668cb35145dcfaa1931a5f37fde375eeae8068b4c0d2f289da28a270b2d2c"
dependencies = [
"directories",
"serde",
"thiserror",
"toml",
]
[[package]]
name = "cpufeatures"
version = "0.2.5"
@ -326,6 +341,15 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "directories"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs"
version = "4.0.0"
@ -676,6 +700,17 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "id3"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d7a833474b30425eb64132d1f9b727b4e39537418bcc3288497c8d2f5c8948"
dependencies = [
"bitflags",
"byteorder",
"flate2",
]
[[package]]
name = "image"
version = "0.24.5"
@ -1436,6 +1471,12 @@ dependencies = [
"windows",
]
[[package]]
name = "temp-file"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4479870ee948e4c5c3fc4774b46aa73eedf97d84503a9b90922c1bb19a73a8d"
[[package]]
name = "tempfile"
version = "3.3.0"
@ -1546,6 +1587,15 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
[[package]]
name = "toml"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"serde",
]
[[package]]
name = "toml_datetime"
version = "0.5.1"

View file

@ -6,9 +6,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
confy = "0.5.1"
id3 = "1.6.0"
lrc = { version = "0.1.7", optional = true }
notify-rust = { version = "4.7.0", features = ["images"] }
regex = "1.7.1"
temp-file = "0.1.7"
typed-builder = "0.12.0"
[dependencies.clap]

View file

@ -1,6 +1,6 @@
mod events;
mod player_settings;
mod query;
pub mod events;
pub mod player_settings;
pub mod query;
use crate::cmus::query::CmusQueryResponse;
use std::collections::HashMap;

View file

@ -4,8 +4,15 @@ mod arguments;
mod cmus;
mod utils;
use crate::cmus::query::CmusQueryResponse;
use clap::Parser;
macro_rules! sleep {
($time: expr) => {
std::thread::sleep(std::time::Duration::from_millis($time));
};
}
fn main() {
let args = arguments::Arguments::parse();
@ -19,12 +26,8 @@ fn main() {
&args.cmus_socket_password,
);
let sleep = || {
std::thread::sleep(std::time::Duration::from_millis(args.interval));
};
// Initialize the buffer to store the track info, to compare it with the next one.
let mut previous_track = cmus::Track::default();
// Initialize the buffer to store the response from cmus, to compare it with the next one.
let mut previous_response = CmusQueryResponse::default();
// Initialize the buffer to store the cover path, to compare it with the next one.
// This is used to speed up the main loop, because we don't need to process the template and search for the cover every time.
// We only need to do it when the track directory changes.
@ -32,12 +35,12 @@ fn main() {
loop {
// Get the track info, and compare it with the previous one.
let Ok(track) = cmus::ping_cmus(&mut query_command) else {
let Ok(response) = cmus::ping_cmus(&mut query_command) else {
if args.link {
std::process::exit(0)
} else {
// If the track info is the same as the previous one, just sleep for a while and try again.
sleep();
sleep!(args.interval);
continue;
}
};
@ -52,6 +55,6 @@ fn main() {
let changes = track.get_changes(&previous_track);
*/
sleep();
sleep!(args.interval);
}
}

View file

@ -1,6 +1,42 @@
use crate::cmus;
use std::path::Path;
/// Extracts the first embedded picture from an ID3 tag of an Audio file.
///
/// # Arguments
///
/// * `track_path` - The path to the Audio file.
///
/// # Returns
///
/// Returns a `Result` containing a `TempFile` object with the contents of the extracted picture, or `None` if the MP3 file doesn't have any embedded pictures.
/// In case of error, the `Result` will contain an error value of type `std::io::Error`.
///
/// # Example
///
/// ```
/// let result = get_embedded_art("/path/to/track.mp3");
///
/// match result {
/// Ok(Some(temp_file)) => {
/// // Use the temp file...
/// temp_file.path();
/// },
/// Ok(None) => println!("Track does not have an embedded picture"),
/// Err(error) => println!("Error: {}", error),
/// }
/// ```
pub fn get_embedded_art(track_path: &str) -> std::io::Result<Option<temp_file::TempFile>> {
let tags = id3::Tag::read_from_path(track_path)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let Some(picture) = tags.pictures().next() else { return Ok(None); };
let temp_file = temp_file::TempFile::new()?;
Ok(Some(temp_file.with_contents(&*picture.data).map_err(
|e| std::io::Error::new(std::io::ErrorKind::Other, e),
)?))
}
/// Search in the track directory for the cover image or the lyrics(depending on the `regx`).
/// If the cover image or the lyrics is not found, search in the parent directory, and so on, until the max depth is reached.
/// If the cover image or the lyrics is not found, return `None`.