-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtrack.rs
More file actions
319 lines (274 loc) · 9.1 KB
/
Copy pathtrack.rs
File metadata and controls
319 lines (274 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::pin::Pin;
use std::sync::Arc;
use anyhow::Result;
use bytes::Bytes;
use lazy_static::lazy_static;
use librespot::core::session::Session;
use librespot::core::spotify_id::SpotifyId;
use librespot::metadata::Metadata;
use librespot::metadata::image::Image;
use regex::Regex;
use crate::encoder::tags::Tags;
use crate::utils::clean_invalid_characters;
pub type AsyncFn<T> =
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = Option<T>> + Send>> + Send + Sync>;
#[async_trait::async_trait]
trait TrackCollection {
async fn get_tracks(&self, session: &Session) -> Vec<Track>;
}
#[tracing::instrument(name = "get_tracks", skip(session), level = "debug")]
pub async fn get_tracks(spotify_ids: Vec<String>, session: &Session) -> Result<Vec<Track>> {
let mut tracks: Vec<Track> = Vec::new();
for id in spotify_ids {
tracing::debug!("Getting tracks for: {}", id);
let id = parse_uri_or_url(&id).ok_or(anyhow::anyhow!("Invalid track"))?;
let new_tracks = match id.item_type {
librespot::core::spotify_id::SpotifyItemType::Track => vec![Track::from_id(id)],
librespot::core::spotify_id::SpotifyItemType::Episode => vec![Track::from_id(id)],
librespot::core::spotify_id::SpotifyItemType::Album => {
Album::from_id(id).get_tracks(session).await
}
librespot::core::spotify_id::SpotifyItemType::Playlist => {
Playlist::from_id(id).get_tracks(session).await
}
_ => {
tracing::warn!("Unsupported item type: {:?}", id.item_type);
vec![]
}
};
tracks.extend(new_tracks);
}
tracing::debug!("Got tracks: {:?}", tracks);
Ok(tracks)
}
fn parse_uri_or_url(track: &str) -> Option<SpotifyId> {
parse_uri(track).or_else(|| parse_url(track))
}
fn parse_uri(track_uri: &str) -> Option<SpotifyId> {
let res = SpotifyId::from_uri(track_uri);
tracing::info!("Parsed URI: {:?}", res);
res.ok()
}
fn parse_url(track_url: &str) -> Option<SpotifyId> {
let results = SPOTIFY_URL_REGEX.captures(track_url)?;
let uri = format!(
"spotify:{}:{}",
results.get(1)?.as_str(),
results.get(2)?.as_str()
);
SpotifyId::from_uri(&uri).ok()
}
#[derive(Clone, Debug)]
pub struct Track {
pub id: SpotifyId,
pub position: Option<usize>,
}
lazy_static! {
static ref SPOTIFY_URL_REGEX: Regex =
Regex::new(r"https://open\.spotify\.com(?:/intl-[a-z]{2})?/(\w+)/([a-zA-Z0-9]+)").unwrap();
}
impl Track {
pub fn new(track: &str) -> Result<Self> {
let id = parse_uri_or_url(track).ok_or(anyhow::anyhow!("Invalid track"))?;
Ok(Track { id, position: None })
}
pub fn from_id(id: SpotifyId) -> Self {
Track { id, position: None }
}
pub fn from_id_with_position(id: SpotifyId, position: usize) -> Self {
Track { id, position: Some(position) }
}
pub async fn metadata(&self, session: &Session) -> Result<TrackMetadata> {
let metadata = librespot::metadata::Track::get(session, &self.id)
.await
.map_err(|_| anyhow::anyhow!("Failed to get metadata"))?;
let mut artists = Vec::new();
for artist in metadata.artists.iter() {
artists.push(
librespot::metadata::Artist::get(session, &artist.id)
.await
.map_err(|_| anyhow::anyhow!("Failed to get artist"))?,
);
}
let album = librespot::metadata::Album::get(session, &metadata.album.id)
.await
.map_err(|_| anyhow::anyhow!("Failed to get album"))?;
let covers = album.covers.clone();
let session = session.clone();
let image_retriever: AsyncFn<Bytes> = Arc::new(move || {
let covers = covers.clone();
let session = session.clone();
Box::pin(async move {
let cover = covers.first()?;
session.spclient().get_image(&cover.id).await.ok()
})
});
let position = self.position.or(Some(metadata.number as usize));
Ok(TrackMetadata::from(
metadata,
artists,
album,
position,
image_retriever,
))
}
}
#[async_trait::async_trait]
impl TrackCollection for Track {
async fn get_tracks(&self, _session: &Session) -> Vec<Track> {
vec![self.clone()]
}
}
pub struct Album {
id: SpotifyId,
}
impl Album {
pub fn new(album: &str) -> Result<Self> {
let id = parse_uri_or_url(album).ok_or(anyhow::anyhow!("Invalid album"))?;
Ok(Album { id })
}
pub fn from_id(id: SpotifyId) -> Self {
Album { id }
}
pub async fn is_album(id: SpotifyId, session: &Session) -> bool {
librespot::metadata::Album::get(session, &id).await.is_ok()
}
}
#[async_trait::async_trait]
impl TrackCollection for Album {
async fn get_tracks(&self, session: &Session) -> Vec<Track> {
let album = librespot::metadata::Album::get(session, &self.id)
.await
.expect("Failed to get album");
album.tracks().map(|track| Track::from_id(*track)).collect()
}
}
pub struct Playlist {
id: SpotifyId,
}
impl Playlist {
pub fn new(playlist: &str) -> Result<Self> {
let id = parse_uri_or_url(playlist).ok_or(anyhow::anyhow!("Invalid playlist"))?;
Ok(Playlist { id })
}
pub fn from_id(id: SpotifyId) -> Self {
Playlist { id }
}
pub async fn is_playlist(id: SpotifyId, session: &Session) -> bool {
librespot::metadata::Playlist::get(session, &id)
.await
.is_ok()
}
}
#[async_trait::async_trait]
impl TrackCollection for Playlist {
async fn get_tracks(&self, session: &Session) -> Vec<Track> {
let playlist = librespot::metadata::Playlist::get(session, &self.id)
.await
.expect("Failed to get playlist");
playlist
.tracks()
.enumerate()
.map(|(i, track)| Track::from_id_with_position(*track, i + 1))
.collect()
}
}
#[derive(Clone)]
pub struct TrackMetadata {
pub artists: Vec<ArtistMetadata>,
pub track_name: String,
pub album: AlbumMetadata,
pub duration: i32,
pub position: Option<usize>,
image_retriever: AsyncFn<Bytes>,
}
impl TrackMetadata {
pub fn from(
track: librespot::metadata::Track,
artists: Vec<librespot::metadata::Artist>,
album: librespot::metadata::Album,
position: Option<usize>,
image_retriever: AsyncFn<Bytes>,
) -> Self {
let artists = artists
.iter()
.map(|artist| ArtistMetadata::from(artist.clone()))
.collect();
let album = AlbumMetadata::from(album);
TrackMetadata {
artists,
track_name: track.name.clone(),
album,
duration: track.duration,
position,
image_retriever,
}
}
pub fn approx_size(&self) -> usize {
let duration = self.duration / 1000;
let sample_rate = 44100;
let channels = 2;
let bits_per_sample = 32;
let bytes_per_sample = bits_per_sample / 8;
(duration as usize) * sample_rate * channels * bytes_per_sample
}
pub async fn tags(&self) -> Result<Tags> {
let tags = Tags {
title: self.track_name.clone(),
artists: self.artists.iter().map(|a| a.name.clone()).collect(),
album_title: self.album.name.clone(),
album_cover: (self.image_retriever)().await,
position: self.position,
};
Ok(tags)
}
}
impl ToString for TrackMetadata {
fn to_string(&self) -> String {
let position_string = self.position.map_or(String::from(""), |i| format!("{:02}: ", i));
if self.artists.len() > 3 {
let artists_name = self
.artists
.iter()
.take(3)
.map(|artist| artist.name.clone())
.collect::<Vec<String>>()
.join(", ");
return clean_invalid_characters(format!(
"{}{}, ... - {}",
position_string, artists_name, self.track_name
));
}
let artists_name = self
.artists
.iter()
.map(|artist| artist.name.clone())
.collect::<Vec<String>>()
.join(", ");
clean_invalid_characters(format!("{}{} - {}", position_string, artists_name, self.track_name))
}
}
#[derive(Clone, Debug)]
pub struct ArtistMetadata {
pub name: String,
}
impl From<librespot::metadata::Artist> for ArtistMetadata {
fn from(artist: librespot::metadata::Artist) -> Self {
ArtistMetadata {
name: artist.name.clone(),
}
}
}
#[derive(Clone, Debug)]
pub struct AlbumMetadata {
pub name: String,
pub cover: Option<Image>,
}
impl From<librespot::metadata::Album> for AlbumMetadata {
fn from(album: librespot::metadata::Album) -> Self {
AlbumMetadata {
name: album.name.clone(),
cover: album.covers.first().cloned(),
}
}
}