-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingest.go
157 lines (133 loc) · 4.23 KB
/
ingest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"log"
"github.com/zmb3/spotify"
)
type ingester struct {
client *SpotifyClient
cfg *config
}
type data struct {
artists []spotify.SimpleArtist
albums []spotify.SimpleAlbum
savedAlbums map[string]spotify.SavedAlbum
}
func (in *ingester) Ingest() (*data, error) {
log.Println("Fetching all followed artists")
artists, err := in.getArtists()
if err != nil {
return nil, err
}
log.Println("Fetched all followed artists")
log.Println("Getting albums for artists")
allAlbums, err := in.getAlbumsForArtists(artists)
if err != nil {
return nil, err
}
log.Println("Fetched albums for all artists")
log.Println("Getting saved albums for user")
savedAlbums, err := in.getSavedAlbums()
if err != nil {
return nil, err
}
log.Println("Got saved albums")
return &data{
artists: artists,
albums: allAlbums,
savedAlbums: savedAlbums,
}, nil
}
func (in *ingester) getArtists() ([]spotify.SimpleArtist, error) {
// I didn't try super hard, but I also didn't find any better/cleaner way to
// use this API because FullArtistCursorPage does not implement
// spotify.pageable.
after := ""
numArtists := 0
artists := make([]spotify.SimpleArtist, 0)
for {
followedArtists, err := in.client.CurrentUsersFollowedArtistsOpt(-1, after)
if err != nil {
return nil, fmt.Errorf("failed to get the followed artists: %w", err)
}
for _, artist := range followedArtists.Artists {
numArtists++
if _, ok := in.cfg.blacklistedArtists[artist.Name]; ok {
// If this is a blacklisted artist, then skip it.
log.Printf("\tSkipping blacklisted artist: %q", artist.Name)
continue
}
artists = append(artists, artist.SimpleArtist)
}
percentageDone := 100 * (float64(numArtists) / float64(followedArtists.Total))
log.Printf("\t(%f%% done) Fetching albums", percentageDone)
if numArtists >= followedArtists.Total {
break
}
after = followedArtists.Cursor.After
}
return artists, nil
}
func (in *ingester) getAlbumsForArtists(artists []spotify.SimpleArtist) ([]spotify.SimpleAlbum, error) {
countryCode := "US"
opts := spotify.Options{
Country: &countryCode,
}
allAlbums := make([]spotify.SimpleAlbum, 0)
// At this point we have a slice of artists. We want to, for each artist, get their albums.
for i, artist := range artists {
percentageDone := 100 * (float64(i) / float64(len(artists)))
log.Printf("(%f%% done) Getting albums for artist: %q", percentageDone, artist.Name)
simpleAlbumPage, err := in.client.GetArtistAlbumsOpt(
artist.ID,
&opts,
spotify.AlbumTypeAlbum,
spotify.AlbumTypeCompilation,
spotify.AlbumTypeSingle,
)
if err != nil {
return nil, fmt.Errorf("failed to get artist albums for %q: %w", artist.Name, err)
}
numAlbums := 0
for {
for _, album := range simpleAlbumPage.Albums {
numAlbums++
percentageDone := 100 * (float64(numAlbums) / float64(simpleAlbumPage.Total))
log.Printf("\tFetched %f%%", percentageDone)
allAlbums = append(allAlbums, album)
}
if err := in.client.NextSimpleAlbumPage(simpleAlbumPage); err == spotify.ErrNoMorePages {
break
} else if err != nil {
return nil, fmt.Errorf("failed to iterate to the next artist album page: %w", err)
}
}
}
return allAlbums, nil
}
func (in *ingester) getSavedAlbums() (map[string]spotify.SavedAlbum, error) {
// Before we get around to processing these albums we retrieved we need to
// get the albums that the user has already liked. This is going to be useful
// for determining if a released album has already been listened to by a
// user.
savedAlbumsPage, err := in.client.CurrentUsersAlbums()
if err != nil {
return nil, fmt.Errorf("failed to get the saved albums: %w", err)
}
savedAlbums := make(map[string]spotify.SavedAlbum, 0)
numAlbums := 0
for {
for _, album := range savedAlbumsPage.Albums {
savedAlbums[album.ID.String()] = album
numAlbums++
}
if err := in.client.NextSavedAlbumPage(savedAlbumsPage); err == spotify.ErrNoMorePages {
break
} else if err != nil {
return nil, fmt.Errorf("failed to iterate to the next saved albums page: %w", err)
}
percentageDone := 100 * (float64(numAlbums) / float64(savedAlbumsPage.Total))
log.Printf("\tFetched %f%%", percentageDone)
}
return savedAlbums, nil
}