-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (57 loc) · 1.88 KB
/
main.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
package main
import (
"context"
"log"
"os"
"strings"
"sync"
"time"
"github.com/joho/godotenv"
"github.com/zmb3/spotify"
"golang.org/x/oauth2/clientcredentials"
"spotify-playlist-exporter/helpers/csvfiles"
"spotify-playlist-exporter/helpers/spotifyclient"
"spotify-playlist-exporter/helpers/timer"
)
func main() {
defer timer.FuncTimer("main")()
// Load environment variables in .env file
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
authConfig := &clientcredentials.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
TokenURL: spotify.TokenURL,
}
contextBackground := context.Background()
accessToken, err := authConfig.Token(contextBackground)
playlistUrls := []string{
// EXAMPLE:
"https://open.spotify.com/playlist/37i9dQZEVXbNG2KDcFcKOF?si=be29ccfd84a347b8",
"https://open.spotify.com/playlist/37i9dQZF1DWV7EzJMK2FUI?si=e81158c8b6744e58",
"https://open.spotify.com/playlist/37i9dQZF1DX4AyFl3yqHeK?si=b6d8550fba8642c5",
}
currentTime := time.Now()
currentTimeFormatted := strings.ReplaceAll(currentTime.Format("01-02-2006 15:04:05"), " ", "_")
var wg sync.WaitGroup
for _, element := range playlistUrls {
wg.Add(1)
playlistQueryString := strings.SplitAfter(element, "https://open.spotify.com/playlist/")[1]
playlistIdRaw := strings.SplitAfter(playlistQueryString, "?si=")[0]
playlistId := strings.ReplaceAll(playlistIdRaw, "?si=", "")
go func() {
defer wg.Done()
client := spotify.Authenticator{}.NewClient(accessToken)
playlist := spotifyclient.FetchSpotifyClient(accessToken, playlistId)
csvfiles.CreateCsv(client, accessToken, playlist, currentTimeFormatted)
}()
}
if err != nil {
log.Fatalf("error retrieve access token: %v", err)
}
log.Println("Waiting for goroutines to complete...")
wg.Wait()
log.Printf("Completed converting %v playlists to .csv", len(playlistUrls))
}