first version
This commit is contained in:
1 parent
91c1d14815
commit
64c5bdba9a
8 files changed
+864
No files matched your search
+149
@@ -0,0 +1,149 @@
|
||||
|
||||
/// Extracts the filename from a given path.
|
||||
///
|
||||
/// Returns the filename: String if a filename exists and is valid UTF-8,
|
||||
/// otherwise returns "".
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let song_title: String = bluefunc::get_filename(&song_path);
|
||||
/// ```
|
||||
pub fn get_filename(path: &str) -> String {
|
||||
std::path::Path::new(path)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
}
|
||||
/// Extracts the filename without the extension from a given path.
|
||||
///
|
||||
/// Returns `Some(filename)` if a filename exists and is valid UTF-8,
|
||||
/// otherwise returns "".
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// let song_title = bluefunc::get_filename_without_extension(&song_path);
|
||||
/// ```
|
||||
pub fn get_filename_without_extension(path: &str) -> String {
|
||||
std::path::Path::new(path)
|
||||
.file_stem()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
}
|
||||
pub fn get_artist_and_title(song_path: &str) -> (String, String) {
|
||||
let song_name_without_extension: String = get_filename_without_extension(&song_path);
|
||||
let mut artist = String::new();
|
||||
let mut title = String::new();
|
||||
|
||||
//println!("song_name_without_extension: {}", &song_name_without_extension);
|
||||
if song_name_without_extension.contains(" - ") {
|
||||
artist = song_name_without_extension
|
||||
.split(" - ")
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
//println!("artist: {}", artist);
|
||||
|
||||
title = song_name_without_extension
|
||||
.split(" - ")
|
||||
.last()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
//println!("title: {}", title);
|
||||
}
|
||||
|
||||
//println!("artist: {} title: {}", artist, title);
|
||||
(artist, title)
|
||||
}
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub struct StringDatabase {
|
||||
entries: RwLock<HashSet<String>>,
|
||||
file: RwLock<File>,
|
||||
}
|
||||
|
||||
impl StringDatabase {
|
||||
pub fn open(path: &str) -> Self {
|
||||
let file = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.read(true)
|
||||
.open(path)
|
||||
.expect("Could not open database file");
|
||||
|
||||
let reader = BufReader::new(
|
||||
File::open(path).expect("Could not read database")
|
||||
);
|
||||
|
||||
let entries = reader
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect::<HashSet<String>>();
|
||||
|
||||
Self {
|
||||
entries: RwLock::new(entries),
|
||||
file: RwLock::new(file),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exists(&self, value: &str) -> bool {
|
||||
self.entries.read().unwrap().contains(value)
|
||||
}
|
||||
|
||||
pub fn save(&self, value: &str) -> bool {
|
||||
let mut entries = self.entries.write().unwrap();
|
||||
|
||||
if entries.contains(value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut file = self.file.write().unwrap();
|
||||
|
||||
writeln!(file, "{}", value)
|
||||
.expect("Could not write database");
|
||||
|
||||
entries.insert(value.to_string());
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn search_cover(artist: &str, song: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
println!("search_cover({}, {})", artist, song);
|
||||
let cover_path = format!("data/assets/{} - {}.jpg", artist, song);
|
||||
|
||||
if std::path::Path::new(&cover_path).exists() {
|
||||
//println!("File exists");
|
||||
Ok(cover_path)
|
||||
} else {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let term = url::form_urlencoded::Serializer::new(String::new())
|
||||
.append_pair("term", &format!("{} {}", artist, song))
|
||||
.append_pair("entity", "song")
|
||||
.append_pair("limit", "5")
|
||||
.finish();
|
||||
let url = format!("https://itunes.apple.com/search?{}", term);
|
||||
let response: serde_json::Value = client
|
||||
.get(&url)
|
||||
.send()?
|
||||
.error_for_status()?
|
||||
.json()?;
|
||||
let Some(artwork) = response["results"][0]["artworkUrl100"].as_str() else {
|
||||
return Ok("nocover.jpg".to_string());
|
||||
};
|
||||
let image = client
|
||||
.get(artwork)
|
||||
.send()?
|
||||
.error_for_status()?
|
||||
.bytes()?;
|
||||
if std::fs::write(&cover_path, image).is_err() {
|
||||
return Ok("data/nocover.jpg".to_string());
|
||||
}
|
||||
Ok(cover_path)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user