|
@@ -1,16 +1,12 @@
|
|
use super::subprocess::ytdl;
|
|
use super::subprocess::ytdl;
|
|
|
|
|
|
-pub struct SongMetadata {
|
|
|
|
|
|
+pub struct Song {
|
|
|
|
+ pub url: String,
|
|
pub artist: Option<String>,
|
|
pub artist: Option<String>,
|
|
pub title: Option<String>,
|
|
pub title: Option<String>,
|
|
pub duration: Option<u64>,
|
|
pub duration: Option<u64>,
|
|
}
|
|
}
|
|
|
|
|
|
-pub struct Song {
|
|
|
|
- pub url: String,
|
|
|
|
- metadata: SongMetadata,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
impl Song {
|
|
impl Song {
|
|
pub async fn from_query(query: &str) -> Result<Song, std::io::Error> {
|
|
pub async fn from_query(query: &str) -> Result<Song, std::io::Error> {
|
|
let query = if query.contains("watch?v=") {
|
|
let query = if query.contains("watch?v=") {
|
|
@@ -19,26 +15,22 @@ impl Song {
|
|
format!("ytsearch:{}", query)
|
|
format!("ytsearch:{}", query)
|
|
};
|
|
};
|
|
|
|
|
|
- let (title, url) = ytdl(&query).await?;
|
|
|
|
|
|
+ let stdout = ytdl(&query).await?;
|
|
|
|
+ let mut lines = stdout.lines();
|
|
|
|
|
|
- let metadata = SongMetadata {
|
|
|
|
|
|
+ let song = Song {
|
|
artist: None,
|
|
artist: None,
|
|
- title: Some(title),
|
|
|
|
|
|
+ title: lines.next().map(|t| t.to_string()),
|
|
|
|
+ url: lines.next().unwrap().to_string(),
|
|
duration: None,
|
|
duration: None,
|
|
};
|
|
};
|
|
-
|
|
|
|
- let song = Song { url, metadata };
|
|
|
|
Ok(song)
|
|
Ok(song)
|
|
}
|
|
}
|
|
|
|
|
|
pub async fn get_string(&self) -> String {
|
|
pub async fn get_string(&self) -> String {
|
|
- let artist = self
|
|
|
|
- .metadata
|
|
|
|
- .artist
|
|
|
|
- .clone()
|
|
|
|
- .unwrap_or("unknown".to_string());
|
|
|
|
- let title = self.metadata.title.clone().unwrap_or("unknown".to_string());
|
|
|
|
- let duration = match self.metadata.duration {
|
|
|
|
|
|
+ let artist = self.artist.clone().unwrap_or("unknown".to_string());
|
|
|
|
+ let title = self.title.clone().unwrap_or("unknown".to_string());
|
|
|
|
+ let duration = match self.duration {
|
|
Some(duration) => {
|
|
Some(duration) => {
|
|
let mins = duration / 60;
|
|
let mins = duration / 60;
|
|
let secs = duration - mins * 60;
|
|
let secs = duration - mins * 60;
|