mirror of
https://gitverse.ru/ot/DownOnSpot
synced 2025-12-19 18:04:16 +00:00
Lint code
This commit is contained in:
parent
e81e0da7e6
commit
7a86351998
5 changed files with 900 additions and 909 deletions
|
|
@ -7,7 +7,7 @@ use crate::error::SpotifyError::{LameConverterError, InvalidFormat};
|
|||
|
||||
/// Converts audio to MP3
|
||||
pub enum AudioConverter {
|
||||
OGG {
|
||||
Ogg {
|
||||
decoder: OggStreamReader<ReadWrap>,
|
||||
lame: lame::Lame,
|
||||
lame_end: bool,
|
||||
|
|
@ -48,10 +48,10 @@ impl AudioConverter {
|
|||
};
|
||||
|
||||
match format {
|
||||
AudioFormat::AAC => todo!(),
|
||||
AudioFormat::MP4 => todo!(),
|
||||
AudioFormat::Aac => todo!(),
|
||||
AudioFormat::Mp4 => todo!(),
|
||||
// Lewton decoder
|
||||
AudioFormat::OGG => {
|
||||
AudioFormat::Ogg => {
|
||||
let decoder = OggStreamReader::new(ReadWrap::new(Box::new(read)))?;
|
||||
let sample_rate = decoder.ident_hdr.audio_sample_rate;
|
||||
// Init lame
|
||||
|
|
@ -64,13 +64,13 @@ impl AudioConverter {
|
|||
Err(_) => return Err(LameConverterError("Init".to_string()))
|
||||
};
|
||||
|
||||
Ok(AudioConverter::OGG {
|
||||
Ok(AudioConverter::Ogg {
|
||||
lame,
|
||||
decoder,
|
||||
lame_end: false,
|
||||
})
|
||||
}
|
||||
AudioFormat::MP3 => panic!("No reencoding allowd!"),
|
||||
AudioFormat::Mp3 => panic!("No reencoding allowd!"),
|
||||
_ => Err(InvalidFormat),
|
||||
}
|
||||
}
|
||||
|
|
@ -79,7 +79,7 @@ impl AudioConverter {
|
|||
impl Read for AudioConverter {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
AudioConverter::OGG {
|
||||
AudioConverter::Ogg {
|
||||
decoder,
|
||||
lame,
|
||||
lame_end,
|
||||
|
|
|
|||
|
|
@ -413,7 +413,7 @@ impl DownloaderInternal {
|
|||
let mime = res
|
||||
.headers()
|
||||
.get("content-type")
|
||||
.ok_or(SpotifyError::Error("Missing cover mime!".into()))?
|
||||
.ok_or_else(|| SpotifyError::Error("Missing cover mime!".into()))?
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
|
@ -432,9 +432,8 @@ impl DownloaderInternal {
|
|||
) -> Result<(), SpotifyError> {
|
||||
let mut tag_wrap = TagWrap::new(path, format)?;
|
||||
// Format specific
|
||||
match &mut tag_wrap {
|
||||
TagWrap::ID3(id3) => id3.use_id3_v24(config.id3v24),
|
||||
_ => {}
|
||||
if let TagWrap::Id3(id3) = &mut tag_wrap {
|
||||
id3.use_id3_v24(config.id3v24)
|
||||
}
|
||||
|
||||
let tag = tag_wrap.get_tag();
|
||||
|
|
@ -521,7 +520,7 @@ impl DownloaderInternal {
|
|||
quality,
|
||||
)
|
||||
.boxed();
|
||||
audio_format = AudioFormat::MP3;
|
||||
audio_format = AudioFormat::Mp3;
|
||||
s
|
||||
}
|
||||
false => DownloaderInternal::download_track_stream(path_clone, encrypted, key).boxed(),
|
||||
|
|
@ -570,7 +569,7 @@ impl DownloaderInternal {
|
|||
// Custom reader loop for decrypting
|
||||
loop {
|
||||
// Blocking reader
|
||||
let (d, read, mut buf) = tokio::task::spawn_blocking(move || {
|
||||
let (d, read, buf) = tokio::task::spawn_blocking(move || {
|
||||
let mut buf = vec![0; 1024 * 64];
|
||||
match decrypted.read(&mut buf) {
|
||||
Ok(r) => Ok((decrypted, r, buf)),
|
||||
|
|
@ -581,7 +580,7 @@ impl DownloaderInternal {
|
|||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
file.write_all(&mut buf[0..read]).await?;
|
||||
file.write_all(&buf[0..read]).await?;
|
||||
yield read;
|
||||
}
|
||||
}
|
||||
|
|
@ -614,7 +613,7 @@ impl DownloaderInternal {
|
|||
// Custom reader loop for decrypting
|
||||
loop {
|
||||
// Blocking reader
|
||||
let (d, read, mut buf) = tokio::task::spawn_blocking(move || {
|
||||
let (d, read, buf) = tokio::task::spawn_blocking(move || {
|
||||
let mut buf = vec![0; 1024 * 64];
|
||||
match decrypted.read(&mut buf) {
|
||||
Ok(r) => Ok((decrypted, r, buf)),
|
||||
|
|
@ -625,7 +624,7 @@ impl DownloaderInternal {
|
|||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
file.write_all(&mut buf[0..read]).await?;
|
||||
file.write_all(& buf[0..read]).await?;
|
||||
yield read;
|
||||
}
|
||||
}
|
||||
|
|
@ -634,10 +633,10 @@ impl DownloaderInternal {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AudioFormat {
|
||||
OGG,
|
||||
AAC,
|
||||
MP3,
|
||||
MP4,
|
||||
Ogg,
|
||||
Aac,
|
||||
Mp3,
|
||||
Mp4,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
|
|
@ -645,10 +644,10 @@ impl AudioFormat {
|
|||
/// Get extension
|
||||
pub fn extension(&self) -> String {
|
||||
match self {
|
||||
AudioFormat::OGG => "ogg",
|
||||
AudioFormat::AAC => "m4a",
|
||||
AudioFormat::MP3 => "mp3",
|
||||
AudioFormat::MP4 => "mp4",
|
||||
AudioFormat::Ogg => "ogg",
|
||||
AudioFormat::Aac => "m4a",
|
||||
AudioFormat::Mp3 => "mp3",
|
||||
AudioFormat::Mp4 => "mp4",
|
||||
AudioFormat::Unknown => "",
|
||||
}
|
||||
.to_string()
|
||||
|
|
@ -658,19 +657,19 @@ impl AudioFormat {
|
|||
impl From<FileFormat> for AudioFormat {
|
||||
fn from(f: FileFormat) -> Self {
|
||||
match f {
|
||||
FileFormat::OGG_VORBIS_96 => Self::OGG,
|
||||
FileFormat::OGG_VORBIS_160 => Self::OGG,
|
||||
FileFormat::OGG_VORBIS_320 => Self::OGG,
|
||||
FileFormat::MP3_256 => Self::MP3,
|
||||
FileFormat::MP3_320 => Self::MP3,
|
||||
FileFormat::MP3_160 => Self::MP3,
|
||||
FileFormat::MP3_96 => Self::MP3,
|
||||
FileFormat::MP3_160_ENC => Self::MP3,
|
||||
FileFormat::MP4_128_DUAL => Self::MP4,
|
||||
FileFormat::OGG_VORBIS_96 => Self::Ogg,
|
||||
FileFormat::OGG_VORBIS_160 => Self::Ogg,
|
||||
FileFormat::OGG_VORBIS_320 => Self::Ogg,
|
||||
FileFormat::MP3_256 => Self::Mp3,
|
||||
FileFormat::MP3_320 => Self::Mp3,
|
||||
FileFormat::MP3_160 => Self::Mp3,
|
||||
FileFormat::MP3_96 => Self::Mp3,
|
||||
FileFormat::MP3_160_ENC => Self::Mp3,
|
||||
FileFormat::MP4_128_DUAL => Self::Mp4,
|
||||
FileFormat::OTHER3 => Self::Unknown,
|
||||
FileFormat::AAC_160 => Self::AAC,
|
||||
FileFormat::AAC_320 => Self::AAC,
|
||||
FileFormat::MP4_128 => Self::MP4,
|
||||
FileFormat::AAC_160 => Self::Aac,
|
||||
FileFormat::AAC_320 => Self::Aac,
|
||||
FileFormat::MP4_128 => Self::Mp4,
|
||||
FileFormat::OTHER5 => Self::Unknown,
|
||||
}
|
||||
}
|
||||
|
|
@ -737,13 +736,13 @@ pub struct Download {
|
|||
pub state: DownloadState,
|
||||
}
|
||||
|
||||
impl Into<Download> for aspotify::Track {
|
||||
fn into(self) -> Download {
|
||||
impl From<aspotify::Track> for Download {
|
||||
fn from(val: aspotify::Track) -> Self {
|
||||
Download {
|
||||
id: 0,
|
||||
track_id: self.id.unwrap(),
|
||||
title: self.name,
|
||||
subtitle: self
|
||||
track_id: val.id.unwrap(),
|
||||
title: val.name,
|
||||
subtitle: val
|
||||
.artists
|
||||
.first()
|
||||
.map(|a| a.name.to_owned())
|
||||
|
|
@ -753,13 +752,13 @@ impl Into<Download> for aspotify::Track {
|
|||
}
|
||||
}
|
||||
|
||||
impl Into<Download> for aspotify::TrackSimplified {
|
||||
fn into(self) -> Download {
|
||||
impl From<aspotify::TrackSimplified> for Download {
|
||||
fn from(val: aspotify::TrackSimplified) -> Self {
|
||||
Download {
|
||||
id: 0,
|
||||
track_id: self.id.unwrap(),
|
||||
title: self.name,
|
||||
subtitle: self
|
||||
track_id: val.id.unwrap(),
|
||||
title: val.name,
|
||||
subtitle: val
|
||||
.artists
|
||||
.first()
|
||||
.map(|a| a.name.to_owned())
|
||||
|
|
@ -769,11 +768,11 @@ impl Into<Download> for aspotify::TrackSimplified {
|
|||
}
|
||||
}
|
||||
|
||||
impl Into<DownloadJob> for Download {
|
||||
fn into(self) -> DownloadJob {
|
||||
impl From<Download> for DownloadJob {
|
||||
fn from(val: Download) -> Self {
|
||||
DownloadJob {
|
||||
id: self.id,
|
||||
track_id: self.track_id,
|
||||
id: val.id,
|
||||
track_id: val.track_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use aspotify::{
|
||||
Album, Client, ClientCredentials, Playlist, PlaylistItemType, Track, TrackSimplified, Artist
|
||||
Album, Artist, Client, ClientCredentials, Playlist, PlaylistItemType, Track, TrackSimplified,
|
||||
};
|
||||
use librespot::core::authentication::Credentials;
|
||||
use librespot::core::config::SessionConfig;
|
||||
|
|
@ -16,7 +16,6 @@ pub struct Spotify {
|
|||
}
|
||||
|
||||
impl Spotify {
|
||||
|
||||
/// Create new instance
|
||||
pub async fn new(
|
||||
username: &str,
|
||||
|
|
@ -41,7 +40,7 @@ impl Spotify {
|
|||
pub fn parse_uri(uri: &str) -> Result<String, SpotifyError> {
|
||||
// Already URI
|
||||
if uri.starts_with("spotify:") {
|
||||
if uri.split(':').collect::<Vec<&str>>().len() < 3 {
|
||||
if uri.split(':').count() < 3 {
|
||||
return Err(SpotifyError::InvalidUri);
|
||||
}
|
||||
return Ok(uri.to_string());
|
||||
|
|
@ -53,7 +52,7 @@ impl Spotify {
|
|||
if url.host_str() == Some("open.spotify.com") {
|
||||
let path = url
|
||||
.path_segments()
|
||||
.ok_or(SpotifyError::Error("Missing URL path".into()))?
|
||||
.ok_or_else(|| SpotifyError::Error("Missing URL path".into()))?
|
||||
.collect::<Vec<&str>>();
|
||||
if path.len() < 2 {
|
||||
return Err(SpotifyError::InvalidUri);
|
||||
|
|
@ -104,16 +103,12 @@ impl Spotify {
|
|||
.data
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|i| {
|
||||
if let Some(item) = &i.item {
|
||||
if let PlaylistItemType::Track(t) = item {
|
||||
.filter_map(|i| -> Option<Track> {
|
||||
if let Some(PlaylistItemType::Track(t)) = &i.item {
|
||||
Some(t.to_owned())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
|
|
@ -157,10 +152,7 @@ impl Spotify {
|
|||
.get_artist_albums(id, None, 50, offset, None)
|
||||
.await?;
|
||||
|
||||
for album in &mut page
|
||||
.data
|
||||
.items
|
||||
.iter() {
|
||||
for album in &mut page.data.items.iter() {
|
||||
items.append(&mut self.full_album(&album.id).await?)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,22 +5,22 @@ use crate::downloader::AudioFormat;
|
|||
use crate::error::SpotifyError;
|
||||
|
||||
use self::id3::ID3Tag;
|
||||
use ogg::OGGTag;
|
||||
use ogg::OggTag;
|
||||
|
||||
mod id3;
|
||||
mod ogg;
|
||||
|
||||
pub enum TagWrap {
|
||||
OGG(OGGTag),
|
||||
ID3(ID3Tag),
|
||||
Ogg(OggTag),
|
||||
Id3(ID3Tag),
|
||||
}
|
||||
|
||||
impl TagWrap {
|
||||
/// Load from file
|
||||
pub fn new(path: impl AsRef<Path>, format: AudioFormat) -> Result<TagWrap, SpotifyError> {
|
||||
match format {
|
||||
AudioFormat::OGG => Ok(TagWrap::OGG(OGGTag::open(path)?)),
|
||||
AudioFormat::MP3 => Ok(TagWrap::ID3(ID3Tag::open(path)?)),
|
||||
AudioFormat::Ogg => Ok(TagWrap::Ogg(OggTag::open(path)?)),
|
||||
AudioFormat::Mp3 => Ok(TagWrap::Id3(ID3Tag::open(path)?)),
|
||||
_ => Err(SpotifyError::Error("Invalid format!".into())),
|
||||
}
|
||||
}
|
||||
|
|
@ -28,8 +28,8 @@ impl TagWrap {
|
|||
/// Get Tag trait
|
||||
pub fn get_tag(&mut self) -> Box<&mut dyn Tag> {
|
||||
match self {
|
||||
TagWrap::OGG(tag) => Box::new(tag),
|
||||
TagWrap::ID3(tag) => Box::new(tag),
|
||||
TagWrap::Ogg(tag) => Box::new(tag),
|
||||
TagWrap::Id3(tag) => Box::new(tag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,24 +6,24 @@ use std::path::{Path, PathBuf};
|
|||
use super::Field;
|
||||
use crate::error::SpotifyError;
|
||||
|
||||
pub struct OGGTag {
|
||||
pub struct OggTag {
|
||||
path: PathBuf,
|
||||
tag: CommentHeader,
|
||||
}
|
||||
|
||||
impl OGGTag {
|
||||
impl OggTag {
|
||||
/// Load tag from file
|
||||
pub fn open(path: impl AsRef<Path>) -> Result<OGGTag, SpotifyError> {
|
||||
pub fn open(path: impl AsRef<Path>) -> Result<OggTag, SpotifyError> {
|
||||
let mut file = File::open(&path)?;
|
||||
let tag = read_comment_header(&mut file);
|
||||
Ok(OGGTag {
|
||||
Ok(OggTag {
|
||||
path: path.as_ref().to_owned(),
|
||||
tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Tag for OGGTag {
|
||||
impl super::Tag for OggTag {
|
||||
fn set_separator(&mut self, _separator: &str) {}
|
||||
|
||||
fn set_field(&mut self, field: Field, value: Vec<String>) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue