56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
use std::str::FromStr;
|
|
|
|
use anyhow::bail;
|
|
|
|
use crate::{config::ConfigWrapper, downloader::Downloader, manifest::{song::Song, Manifest}, util::is_supported_host};
|
|
|
|
|
|
|
|
pub fn song(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &str, name: &String, playlist: &String) -> anyhow::Result<()> {
|
|
|
|
let mut playlists = manifest.get_playlists().keys().cloned().collect::<Vec<String>>();
|
|
|
|
playlists.sort();
|
|
|
|
if !is_supported_host(&url::Url::from_str(url)?) {
|
|
log::error!("Invalid or unsupported host name");
|
|
return Ok(());
|
|
}
|
|
|
|
|
|
|
|
let song = Song::from_url_str(url.to_string())?;
|
|
manifest.add_song(playlist, name.clone(), song.clone());
|
|
manifest.save(None)?;
|
|
|
|
let should_download = crate::prompt::yes_no("Download song now?", Some(false));
|
|
|
|
if should_download {
|
|
downloader.download_song(cfg, name, &song, playlist, manifest.get_format())?;
|
|
crate::process_manager::wait_for_procs_until(0)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn playlist(cfg: &ConfigWrapper, manifest: &mut Manifest, downloader: &mut Downloader, url: &str, name: &String) -> anyhow::Result<()> {
|
|
let songs = downloader.download_playlist_nb(cfg, url, name, manifest.get_format())?;
|
|
|
|
if manifest.get_playlist(name).is_some() {
|
|
log::error!("Playlist {name} already exists");
|
|
bail!("")
|
|
}
|
|
|
|
manifest.add_playlist(name.clone());
|
|
|
|
let playlist = manifest.get_playlist_mut(name).expect("Unreachable");
|
|
|
|
for (sname, song) in songs {
|
|
playlist.add_song(sname, song);
|
|
}
|
|
manifest.save(None)?;
|
|
|
|
while downloader.download_all_nb_poll(cfg)?.is_some() {};
|
|
Ok(())
|
|
}
|