tangled
alpha
login
or
join now
rocksky.app
/
rocksky
96
fork
atom
A decentralized music tracking and discovery platform built on AT Protocol 🎵
rocksky.app
spotify
atproto
lastfm
musicbrainz
scrobbling
listenbrainz
96
fork
atom
overview
issues
7
pulls
pipelines
Merge branch 'main' into feat/feed-generator
tsiry-sandratraina.com
2 months ago
80e039ad
df6c3f38
+73
-40
1 changed file
expand all
collapse all
unified
split
crates
spotify
src
lib.rs
+73
-40
crates/spotify/src/lib.rs
···
931
931
);
932
932
933
933
if changed {
934
934
-
scrobble(
935
935
-
cache.clone(),
936
936
-
&spotify_email,
937
937
-
&did,
938
938
-
&token,
939
939
-
&client_id,
940
940
-
&client_secret,
941
941
-
)
942
942
-
.await?;
934
934
+
cache.setex(
935
935
+
&format!("changed:{}:{}", spotify_email, data_item.id),
936
936
+
&data_item.duration_ms.to_string(),
937
937
+
3600 * 24,
938
938
+
)?;
939
939
+
}
940
940
+
941
941
+
let current_track =
942
942
+
match cache.get(&format!("changed:{}:{}", spotify_email, data_item.id)) {
943
943
+
Ok(x) => x.is_some(),
944
944
+
Err(_) => false,
945
945
+
};
943
946
944
944
-
thread::spawn(move || {
945
945
-
let rt = tokio::runtime::Runtime::new().unwrap();
946
946
-
match rt.block_on(async {
947
947
-
get_album_tracks(
948
948
-
cache.clone(),
949
949
-
&data_item.album.id,
950
950
-
&token,
951
951
-
&client_id,
952
952
-
&client_secret,
953
953
-
)
954
954
-
.await?;
955
955
-
get_album(
956
956
-
cache.clone(),
957
957
-
&data_item.album.id,
958
958
-
&token,
959
959
-
&client_id,
960
960
-
&client_secret,
961
961
-
)
962
962
-
.await?;
963
963
-
update_library(
947
947
+
if let Ok(Some(cached)) = cache.get(&format!("{}:current", spotify_email)) {
948
948
+
let current_song = serde_json::from_str::<CurrentlyPlaying>(&cached)?;
949
949
+
if let Some(item) = current_song.item {
950
950
+
let percentage = current_song.progress_ms.unwrap_or(0) as f32
951
951
+
/ data_item.duration_ms as f32
952
952
+
* 100.0;
953
953
+
if current_track && percentage >= 40.0 && item.id == data_item.id {
954
954
+
println!(
955
955
+
"{} Scrobbling track: {} {}",
956
956
+
format!("[{}]", spotify_email).bright_green(),
957
957
+
item.name.yellow(),
958
958
+
format!("{:.2}%", percentage)
959
959
+
);
960
960
+
scrobble(
964
961
cache.clone(),
965
962
&spotify_email,
966
963
&did,
···
969
966
&client_secret,
970
967
)
971
968
.await?;
972
972
-
Ok::<(), Error>(())
973
973
-
}) {
974
974
-
Ok(_) => {}
975
975
-
Err(e) => {
976
976
-
println!(
977
977
-
"{} {}",
978
978
-
format!("[{}]", spotify_email).bright_green(),
979
979
-
e.to_string().bright_red()
980
980
-
);
981
981
-
}
969
969
+
970
970
+
match cache.del(&format!("changed:{}:{}", spotify_email, data_item.id)) {
971
971
+
Ok(_) => {}
972
972
+
Err(_) => tracing::error!("Failed to delete cache entry"),
973
973
+
};
974
974
+
975
975
+
thread::spawn(move || {
976
976
+
let rt = tokio::runtime::Runtime::new().unwrap();
977
977
+
match rt.block_on(async {
978
978
+
get_album_tracks(
979
979
+
cache.clone(),
980
980
+
&data_item.album.id,
981
981
+
&token,
982
982
+
&client_id,
983
983
+
&client_secret,
984
984
+
)
985
985
+
.await?;
986
986
+
get_album(
987
987
+
cache.clone(),
988
988
+
&data_item.album.id,
989
989
+
&token,
990
990
+
&client_id,
991
991
+
&client_secret,
992
992
+
)
993
993
+
.await?;
994
994
+
update_library(
995
995
+
cache.clone(),
996
996
+
&spotify_email,
997
997
+
&did,
998
998
+
&token,
999
999
+
&client_id,
1000
1000
+
&client_secret,
1001
1001
+
)
1002
1002
+
.await?;
1003
1003
+
Ok::<(), Error>(())
1004
1004
+
}) {
1005
1005
+
Ok(_) => {}
1006
1006
+
Err(e) => {
1007
1007
+
println!(
1008
1008
+
"{} {}",
1009
1009
+
format!("[{}]", spotify_email).bright_green(),
1010
1010
+
e.to_string().bright_red()
1011
1011
+
);
1012
1012
+
}
1013
1013
+
}
1014
1014
+
});
982
1015
}
983
983
-
});
1016
1016
+
}
984
1017
}
985
1018
}
986
1019