From d91b5dd5ad0cbec931536681177e9ab4b2616b3e Mon Sep 17 00:00:00 2001 From: 0xNF <0xNF@users.noreply.github.com> Date: Thu, 11 Jul 2024 08:53:19 +0900 Subject: [PATCH] feat: Add Spotify URI as tag data (#90) Co-authored-by: 0xnf <0xnf@winetech.com> --- Cargo.toml | 2 +- src/downloader.rs | 13 ++++++++++++- src/tag/id3.rs | 9 ++++++++- src/tag/mod.rs | 4 +++- src/tag/ogg.rs | 4 ++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 743162b..f3cff0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ log = "0.4" url = "2.2" protobuf = "3.1" base64 = "0.22.0" -id3 = "1.3" +id3 = "1.14" dirs = "5.0.0" chrono = "0.4" lewton = "0.10" diff --git a/src/downloader.rs b/src/downloader.rs index 0cc8a0c..cf4f7f5 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -416,7 +416,15 @@ impl DownloaderInternal { // Write tags let config = config.clone(); tokio::task::spawn_blocking(move || { - DownloaderInternal::write_tags(path, format, tags, date, cover, config) + DownloaderInternal::write_tags( + path, + job.track_id.to_string(), + format, + tags, + date, + cover, + config, + ) }) .await??; @@ -445,6 +453,7 @@ impl DownloaderInternal { /// Write tags to file ( BLOCKING ) fn write_tags( path: impl AsRef, + track_id: String, format: AudioFormat, tags: Vec<(Field, Vec)>, date: NaiveDate, @@ -467,6 +476,8 @@ impl DownloaderInternal { if let Some((mime, data)) = cover { tag.add_cover(&mime, data); } + // UFID spotify track id + tag.add_unique_file_identifier(&track_id); tag.save()?; Ok(()) } diff --git a/src/tag/id3.rs b/src/tag/id3.rs index 6a436e1..5be13a1 100644 --- a/src/tag/id3.rs +++ b/src/tag/id3.rs @@ -1,5 +1,5 @@ use chrono::{Datelike, NaiveDate}; -use id3::frame::{Picture, PictureType, Timestamp}; +use id3::frame::{Picture, PictureType, Timestamp, UniqueFileIdentifier}; use id3::{Tag, TagLike, Version}; use std::path::{Path, PathBuf}; @@ -82,4 +82,11 @@ impl super::Tag for ID3Tag { second: None, }) } + + fn add_unique_file_identifier(&mut self, track_id: &str) { + self.tag.add_frame(UniqueFileIdentifier { + owner_identifier: "spotify.com".to_string(), + identifier: track_id.into(), + }); + } } diff --git a/src/tag/mod.rs b/src/tag/mod.rs index 968a884..c147771 100644 --- a/src/tag/mod.rs +++ b/src/tag/mod.rs @@ -36,12 +36,14 @@ impl TagWrap { } pub trait Tag { - // Set tag values separator + /// Set tag values separator fn set_separator(&mut self, separator: &str); fn set_raw(&mut self, tag: &str, value: Vec); fn set_field(&mut self, field: Field, value: Vec); fn set_release_date(&mut self, date: NaiveDate); fn add_cover(&mut self, mime: &str, data: Vec); + /// Adds the file identifier of the track + fn add_unique_file_identifier(&mut self, track_id: &str); fn save(&mut self) -> Result<(), SpotifyError>; } diff --git a/src/tag/ogg.rs b/src/tag/ogg.rs index 6364cd6..ec9419c 100644 --- a/src/tag/ogg.rs +++ b/src/tag/ogg.rs @@ -89,4 +89,8 @@ impl super::Tag for OggTag { &format!("{}-{:02}-{:02}", date.year(), date.month(), date.day()), ) } + + fn add_unique_file_identifier(&mut self, track_id: &str) { + self.tag.add_tag_single("SPOTIFY.COM_TRACKID", track_id); + } }