-
Notifications
You must be signed in to change notification settings - Fork 0
/
lister.go
215 lines (183 loc) · 5.31 KB
/
lister.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/pkg/browser"
"github.com/zmb3/spotify/v2"
"github.com/zmb3/spotify/v2/auth"
)
import _ "github.com/joho/godotenv/autoload"
const redirectURI = "http://localhost:8080/callback"
var (
auth = spotifyauth.New(
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(
spotifyauth.ScopePlaylistReadPrivate,
spotifyauth.ScopePlaylistReadCollaborative,
),
)
ch = make(chan *spotify.Client)
state = "wowastate"
ctx = context.Background()
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
startLogFile()
http.HandleFunc("/callback", completeAuth)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Got request for:", r.URL.String())
})
go func() {
err := http.ListenAndServe(":8080", nil)
check(err)
}()
url := auth.AuthURL(state)
fmt.Println("Please login to Spotify with the following link:", url)
browser.OpenURL(url)
client := <-ch
user, err := client.CurrentUser(ctx)
check(err)
fmt.Println("Logged in as:", user.ID)
// ask user to input link to playlist
// read from 'playlist/' to '?' or until EOL, whichever comes first
playlistID := ""
for playlistID == "" {
fmt.Print("Enter playlist share URL: ")
var playlistShareURL string
fmt.Scanln(&playlistShareURL)
playlistID = getStringBetween(playlistShareURL, "playlist/", "?")
if playlistID == "" {
fmt.Println("Please check the link and try again.")
}
}
// get playlist stuff
getPlaylistInfo(playlistID, *client)
}
func startLogFile() {
f, err := os.Create("./latest.log")
check(err)
defer f.Close()
log.SetOutput(f)
log.Println("BGN ERR LOG")
}
func completeAuth(w http.ResponseWriter, r *http.Request) {
token, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token.", http.StatusForbidden)
log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s/n", st, state)
}
client := spotify.New(auth.Client(r.Context(), token))
fmt.Fprintf(w, "Login Success. Please return to program.")
ch <- client
}
func getStringBetween(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
return ""
}
s += len(start)
e := strings.Index(str[s:], end)
if e == -1 {
e = len(str[s:])
}
return str[s : s+e]
}
func getPlaylistInfo(id string, client spotify.Client) {
playlist, err := client.GetPlaylist(
ctx,
spotify.ID(id),
)
check(err)
tracks, err := client.GetPlaylistItems(
ctx,
spotify.ID(id),
)
check(err)
fmt.Println("Please Select Output Type:")
fmt.Println("1. Console\n2. File")
var console bool
for finish := false; !finish; {
fmt.Print("Select Number: ")
var input string
fmt.Scanln(&input)
if strings.Contains(input, "1") {
console, finish = true, true
} else if strings.Contains(input, "2") {
console, finish = false, true
} else {
fmt.Println("Invalid Option. Please try again.")
}
}
// save info somehow
exportPlaylistInfo(id, client, tracks, playlist, err, console)
}
func exportPlaylistInfo(
id string,
client spotify.Client,
tracks *spotify.PlaylistItemPage,
playlist *spotify.FullPlaylist,
err error,
console bool,
) {
ioWriter := os.Stdout
// switch between console output and file
if console != true {
file, fileErr := os.Create("./" + playlist.Name + ".txt")
check(fileErr)
defer file.Close()
ioWriter = file
fmt.Printf("Saved to file: \"%s\"\n", file.Name())
}
w := bufio.NewWriter(ioWriter)
fmt.Fprintln(w, "+")
fmt.Fprintf(w, "| Playlist Name: \"%s\"\n", playlist.Name)
fmt.Fprintf(w, "| Owner: \"%s\"\n", playlist.Owner.DisplayName)
fmt.Fprintf(w, "| Open Playlist: \"%s\"\n", "https://open.spotify.com/playlist/" + id)
fmt.Fprintln(w, "+")
fmt.Fprintf(w, "| Playlist has %d total tracks.\n", tracks.Total)
// page
for page := 1; ; page++ {
fmt.Fprintf(w, "| Page %d has %d tracks.\n", page, len(tracks.Items))
fmt.Fprintln(w, "+")
// track
for i := 0; i < len(tracks.Items); i++ {
name := tracks.Items[i].Track.Track.Name
artists := ""
artistsArr := tracks.Items[i].Track.Track.Artists
for i := 0; i < len(artistsArr); i++ {
inter := ""
if artists != "" {
inter = ", "
}
artists += inter + artistsArr[i].Name
}
fmt.Fprintf(
w,
"%d)\n| Name: \"%-s\" \n| > Artist(s): \"%-s\" \n",
i+1,
name,
artists,
)
}
fmt.Fprintln(w, "+")
err = client.NextPage(ctx, tracks)
if err == spotify.ErrNoMorePages {
break
}
check(err)
}
w.Flush()
}