I want my listening history and my playlists from Spotify in Sonda, so that Mixes, Discover and the smart playlists know what I listened to before. - POST /me/import/spotify takes the zip of a Spotify data export, or the JSON files in it (extended streaming history, the older StreamingHistory files, the playlists), as a multipart form. A request may be up to 512 MB; the zip is limited in how much it may inflate and how many files it may hold, and only the history and playlist files are opened. - Everything is read on the server and nothing is sent anywhere. The files are not kept, and of each play only the song, artist, album, time and how it ended are read: never the IP addresses, the country or the platform. - A play is matched to a song of the library by artist and title (no case, accents or punctuation; a "Remastered" note on either side; any credited artist; a compilation by its album). A match is recorded as a listen at the time of the play: complete, skip or play. Nothing is posted to Mastodon. - Importing the same files again records nothing twice, and the same play in the two shapes of history file is counted once. One import per person at a time. - A playlist becomes a playlist of mine with the songs that were found; one with a name I already have is left alone, and one with none of its songs found is not made. - The report counts what was added, what was already there and what is not in the library, and lists the missing artists, most played first. - Settings gets an Import from Spotify section; the API client can send files. - English and Italian.
111 lines
3.7 KiB
Go
111 lines
3.7 KiB
Go
package spotifyimport
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// Candidate is a song of the library that can be played: what a Spotify entry is compared with.
|
|
type Candidate struct {
|
|
TrackID int64
|
|
Title string
|
|
Album string // empty when it has none
|
|
Artists []string
|
|
DurationMs int64
|
|
}
|
|
|
|
// Index finds the song of the library that a Spotify entry is. It is built once for an import, from every playable
|
|
// song, and answers from memory.
|
|
type Index struct {
|
|
key func(string) string
|
|
byArtist map[string][]entry // artist key + title key
|
|
byBase map[string][]entry // artist key + title key without a remaster note
|
|
byAlbum map[string][]entry // album key + title key, for a compilation whose artist is "Various Artists"
|
|
lengths map[int64]int64
|
|
}
|
|
|
|
type entry struct {
|
|
id int64
|
|
albumKey string
|
|
}
|
|
|
|
// NewIndex builds the index. key makes the comparable form of a name (the same one the library search uses: no case,
|
|
// no accents, no punctuation). Candidates come in order of id, and the first of equal ones wins.
|
|
func NewIndex(key func(string) string, songs []Candidate) *Index {
|
|
ix := &Index{
|
|
key: key,
|
|
byArtist: map[string][]entry{},
|
|
byBase: map[string][]entry{},
|
|
byAlbum: map[string][]entry{},
|
|
lengths: map[int64]int64{},
|
|
}
|
|
for _, s := range songs {
|
|
ix.lengths[s.TrackID] = s.DurationMs
|
|
title, base, album := key(s.Title), key(baseTitle(s.Title)), key(s.Album)
|
|
e := entry{id: s.TrackID, albumKey: album}
|
|
for _, artist := range s.Artists {
|
|
a := key(artist)
|
|
ix.byArtist[a+"\x00"+title] = append(ix.byArtist[a+"\x00"+title], e)
|
|
ix.byBase[a+"\x00"+base] = append(ix.byBase[a+"\x00"+base], e)
|
|
}
|
|
if album != "" {
|
|
ix.byAlbum[album+"\x00"+title] = append(ix.byAlbum[album+"\x00"+title], e)
|
|
}
|
|
}
|
|
return ix
|
|
}
|
|
|
|
// Length is how long a song is, in milliseconds (0 when the library does not know).
|
|
func (ix *Index) Length(trackID int64) int64 { return ix.lengths[trackID] }
|
|
|
|
// Find gives the song of the library that an artist, a title and (when known) an album name, or false. The exact
|
|
// title comes first, then the title without a remaster note on either side; when several songs are equal the one of
|
|
// the album that was asked wins, else the first. A play of a compilation is credited to "Various Artists" on Spotify,
|
|
// which no song of the library is by: for that the album and the title are enough, when that names exactly one song.
|
|
func (ix *Index) Find(artist, title, album string) (int64, bool) {
|
|
a, t, b := ix.key(artist), ix.key(title), ix.key(album)
|
|
if t == "" {
|
|
return 0, false
|
|
}
|
|
if a != "" {
|
|
if id, ok := pick(ix.byArtist[a+"\x00"+t], b); ok {
|
|
return id, true
|
|
}
|
|
if id, ok := pick(ix.byBase[a+"\x00"+ix.key(baseTitle(title))], b); ok {
|
|
return id, true
|
|
}
|
|
}
|
|
if b != "" {
|
|
if matches := ix.byAlbum[b+"\x00"+t]; len(matches) == 1 {
|
|
return matches[0].id, true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
func pick(matches []entry, albumKey string) (int64, bool) {
|
|
if len(matches) == 0 {
|
|
return 0, false
|
|
}
|
|
if albumKey != "" {
|
|
for _, m := range matches {
|
|
if m.albumKey == albumKey {
|
|
return m.id, true
|
|
}
|
|
}
|
|
}
|
|
return matches[0].id, true
|
|
}
|
|
|
|
// remaster is a note that says a song was remastered, in brackets or after a dash: "(Remastered 2011)",
|
|
// "- 2017 Remaster", "[Remaster]". Only the note at the end of the title goes.
|
|
var remaster = regexp.MustCompile(`(?i)\s*(?:[-–—]\s*[^()\[\]]*remaster(?:ed)?[^()\[\]]*|\([^()]*remaster(?:ed)?[^()]*\)|\[[^\[\]]*remaster(?:ed)?[^\[\]]*\])\s*$`)
|
|
|
|
// baseTitle is a title without its remaster note, which is the same song as far as a listener is concerned.
|
|
func baseTitle(s string) string {
|
|
out := strings.TrimSpace(remaster.ReplaceAllString(s, ""))
|
|
if out == "" {
|
|
return strings.TrimSpace(s)
|
|
}
|
|
return out
|
|
}
|