Add support for open.spotify.com links (#392)
* No longer necessary * Add support for open.spotify.com links * Reuse struct for insert command * Formatting
This commit is contained in:
@@ -9,7 +9,6 @@ use cursive::view::ScrollBase;
|
||||
use cursive::{Cursive, Printer, Rect, Vec2};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::album::Album;
|
||||
use crate::artist::Artist;
|
||||
use crate::command::{
|
||||
Command, GotoMode, JumpMode, MoveAmount, MoveMode, SortDirection, SortKey, TargetMode,
|
||||
@@ -28,7 +27,7 @@ use crate::traits::{IntoBoxedViewExt, ListItem, ViewExt};
|
||||
use crate::ui::album::AlbumView;
|
||||
use crate::ui::artist::ArtistView;
|
||||
use crate::ui::contextmenu::ContextMenu;
|
||||
use regex::Regex;
|
||||
use crate::{album::Album, spotify::URIType, spotify_url::SpotifyURL};
|
||||
|
||||
pub type Paginator<I> = Box<dyn Fn(Arc<RwLock<Vec<I>>>) + Send + Sync>;
|
||||
|
||||
@@ -653,32 +652,28 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
|
||||
|
||||
let spotify = self.queue.get_spotify();
|
||||
|
||||
let re =
|
||||
Regex::new(r"https?://open\.spotify\.com/(user/[^/]+/)?(\S+)/(\S+)(\?si=\S+)?")
|
||||
.unwrap();
|
||||
let captures = re.captures(&url);
|
||||
let url = SpotifyURL::from_url(&url);
|
||||
|
||||
if let Some(captures) = captures {
|
||||
let target: Option<Box<dyn ListItem>> = match &captures[2] {
|
||||
"track" => spotify
|
||||
.track(&captures[3])
|
||||
if let Some(url) = url {
|
||||
let target: Option<Box<dyn ListItem>> = match url.uri_type {
|
||||
URIType::Track => spotify
|
||||
.track(&url.id)
|
||||
.map(|track| Track::from(&track).as_listitem()),
|
||||
"album" => spotify
|
||||
.album(&captures[3])
|
||||
URIType::Album => spotify
|
||||
.album(&url.id)
|
||||
.map(|album| Album::from(&album).as_listitem()),
|
||||
"playlist" => spotify
|
||||
.playlist(&captures[3])
|
||||
URIType::Playlist => spotify
|
||||
.playlist(&url.id)
|
||||
.map(|playlist| Playlist::from(&playlist).as_listitem()),
|
||||
"artist" => spotify
|
||||
.artist(&captures[3])
|
||||
URIType::Artist => spotify
|
||||
.artist(&url.id)
|
||||
.map(|artist| Artist::from(&artist).as_listitem()),
|
||||
"episode" => spotify
|
||||
.episode(&captures[3])
|
||||
URIType::Episode => spotify
|
||||
.episode(&url.id)
|
||||
.map(|episode| Episode::from(&episode).as_listitem()),
|
||||
"show" => spotify
|
||||
.get_show(&captures[3])
|
||||
URIType::Show => spotify
|
||||
.get_show(&url.id)
|
||||
.map(|show| Show::from(&show).as_listitem()),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let queue = self.queue.clone();
|
||||
|
||||
@@ -9,6 +9,7 @@ use crate::playlist::Playlist;
|
||||
use crate::queue::Queue;
|
||||
use crate::show::Show;
|
||||
use crate::spotify::{Spotify, URIType};
|
||||
use crate::spotify_url::SpotifyURL;
|
||||
use crate::track::Track;
|
||||
use crate::traits::{ListItem, ViewExt};
|
||||
use crate::ui::listview::{ListView, Pagination};
|
||||
@@ -380,22 +381,6 @@ impl SearchResultsView {
|
||||
|
||||
// is the query a Spotify URI?
|
||||
if let Some(uritype) = URIType::from_uri(&query) {
|
||||
// Clear the results if we are going to process a Spotify URI. We need
|
||||
// to do this since we are only calling the search function for the
|
||||
// given URI type which leaves the previous search results intact.
|
||||
let results_tracks = self.results_tracks.clone();
|
||||
*results_tracks.write().unwrap() = Vec::new();
|
||||
let results_albums = self.results_albums.clone();
|
||||
*results_albums.write().unwrap() = Vec::new();
|
||||
let results_artists = self.results_artists.clone();
|
||||
*results_artists.write().unwrap() = Vec::new();
|
||||
let results_playlists = self.results_playlists.clone();
|
||||
*results_playlists.write().unwrap() = Vec::new();
|
||||
let results_shows = self.results_shows.clone();
|
||||
*results_shows.write().unwrap() = Vec::new();
|
||||
let results_episodes = self.results_episodes.clone();
|
||||
*results_episodes.write().unwrap() = Vec::new();
|
||||
|
||||
match uritype {
|
||||
URIType::Track => {
|
||||
self.perform_search(
|
||||
@@ -452,6 +437,65 @@ impl SearchResultsView {
|
||||
self.tabs.move_focus_to(5);
|
||||
}
|
||||
}
|
||||
// Is the query a spotify URL?
|
||||
// https://open.spotify.com/track/4uLU6hMCjMI75M1A2tKUQC
|
||||
} else if let Some(url) = SpotifyURL::from_url(&query) {
|
||||
match url.uri_type {
|
||||
URIType::Track => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_track),
|
||||
&self.results_tracks,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(0);
|
||||
}
|
||||
URIType::Album => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_album),
|
||||
&self.results_albums,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(1);
|
||||
}
|
||||
URIType::Artist => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_artist),
|
||||
&self.results_artists,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(2);
|
||||
}
|
||||
URIType::Playlist => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_playlist),
|
||||
&self.results_playlists,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(3);
|
||||
}
|
||||
URIType::Show => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_show),
|
||||
&self.results_shows,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(4);
|
||||
}
|
||||
URIType::Episode => {
|
||||
self.perform_search(
|
||||
Box::new(Self::get_episode),
|
||||
&self.results_episodes,
|
||||
&url.id,
|
||||
None,
|
||||
);
|
||||
self.tabs.move_focus_to(5);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.perform_search(
|
||||
Box::new(Self::search_track),
|
||||
|
||||
Reference in New Issue
Block a user