@@ -14,7 +14,8 @@ var UriRegexp = regexp.MustCompile("^spotify:([a-z]+):([0-9a-zA-Z]{21,22})$")
1414func ContextTrackToProvidedTrack (track * connectpb.ContextTrack ) * connectpb.ProvidedTrack {
1515 var uri string
1616 if len (track .Gid ) > 0 {
17- uri = TrackId (track .Gid ).Uri ()
17+ // FIXME: this might not always be a track
18+ uri = SpotifyIdFromGid (SpotifyIdTypeTrack , track .Gid ).Uri ()
1819 } else if len (track .Uri ) > 0 {
1920 uri = track .Uri
2021 } else {
@@ -34,27 +35,51 @@ func ContextTrackToProvidedTrack(track *connectpb.ContextTrack) *connectpb.Provi
3435 }
3536}
3637
37- type TrackId [] byte
38+ type SpotifyIdType string
3839
39- func (id TrackId ) Hex () string {
40- return hex .EncodeToString (id )
40+ const (
41+ SpotifyIdTypeTrack SpotifyIdType = "track"
42+ SpotifyIdTypeEpisode SpotifyIdType = "episode"
43+ )
44+
45+ type SpotifyId struct {
46+ typ SpotifyIdType
47+ id []byte
48+ }
49+
50+ func (id SpotifyId ) Type () SpotifyIdType {
51+ return id .typ
52+ }
53+
54+ func (id SpotifyId ) Id () []byte {
55+ return id .id
4156}
4257
43- func (id TrackId ) Base62 () string {
44- s := new (big.Int ).SetBytes (id ).Text (62 )
58+ func (id SpotifyId ) Hex () string {
59+ return hex .EncodeToString (id .id )
60+ }
61+
62+ func (id SpotifyId ) Base62 () string {
63+ s := new (big.Int ).SetBytes (id .id ).Text (62 )
4564 return strings .Repeat ("0" , 22 - len (s )) + s
4665}
4766
48- func (id TrackId ) Uri () string {
49- return fmt .Sprintf ("spotify:track:%s" , id .Base62 ())
67+ func (id SpotifyId ) Uri () string {
68+ return fmt .Sprintf ("spotify:%s:%s" , id .Type (), id .Base62 ())
69+ }
70+
71+ func SpotifyIdFromGid (typ SpotifyIdType , id []byte ) SpotifyId {
72+ if len (id ) != 16 {
73+ panic (fmt .Sprintf ("invalid gid: %s" , hex .EncodeToString (id )))
74+ }
75+
76+ return SpotifyId {typ , id }
5077}
5178
52- func TrackIdFromUri (uri string ) TrackId {
79+ func SpotifyIdFromUri (uri string ) SpotifyId {
5380 matches := UriRegexp .FindStringSubmatch (uri )
5481 if len (matches ) == 0 {
5582 panic (fmt .Sprintf ("invalid uri: %s" , uri ))
56- } else if matches [1 ] != "track" {
57- panic (fmt .Sprintf ("invalid uri type for track: %s" , matches [1 ]))
5883 }
5984
6085 var i big.Int
@@ -63,5 +88,5 @@ func TrackIdFromUri(uri string) TrackId {
6388 panic ("failed decoding base62 track uri" )
6489 }
6590
66- return i .FillBytes (make ([]byte , 16 ))
91+ return SpotifyId { SpotifyIdType ( matches [ 1 ]), i .FillBytes (make ([]byte , 16 ))}
6792}
0 commit comments