-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (127 loc) · 3.77 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
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/pkg/browser"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)
const redirectURI = "http://localhost:8080/callback"
// just making all things global
var (
auth = spotifyauth.New(spotifyauth.WithRedirectURL(redirectURI), spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate, spotifyauth.ScopePlaylistModifyPrivate, spotifyauth.ScopePlaylistModifyPublic))
ch = make(chan *spotify.Client)
state = "abc123"
)
// Library is just simple represntation of the dump file, which cares only about Tracks section
type Library struct {
Tracks []struct {
Artist string `json:"artist"`
Album string `json:"album"`
Track string `json:"track"`
URI string `json:"uri"`
} `json:"tracks"`
}
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, 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)
}
// use the token to get an authenticated client
client := spotify.New(auth.Client(r.Context(), tok))
fmt.Fprintf(w, "Login Completed! Get back to your terminal")
ch <- client
}
// addTracksToPlaylist adds tracks from Library to playlist using spotify API with 100 track batches
func AddTracksToPlaylist(client *spotify.Client, pl spotify.SimplePlaylist, lib Library) error {
trackCount := len(lib.Tracks)
for i := 0; i < trackCount; i += 100 {
upperBound := i + 100
if upperBound > trackCount {
upperBound = trackCount
}
var trackURIs []spotify.ID
for _, track := range lib.Tracks[i:upperBound] {
trackID := strings.TrimPrefix(track.URI, "spotify:track:")
trackURIs = append(trackURIs, spotify.ID(trackID))
}
_, err := client.AddTracksToPlaylist(context.Background(), pl.ID, trackURIs...)
if err != nil {
return err
}
log.Printf("Adding tracks %d to %d\n", i+1, upperBound)
}
log.Printf("Done, all %d were added", trackCount)
return nil
}
func main() {
// define flags for the CLI
playlist := flag.String("playlist", "restore", "Name of the playlist")
filePath := flag.String("filepath", "./YourLibrary.json", "Path of the file")
// parse the flags
flag.Parse()
jsonFile, err := ioutil.ReadFile(*filePath)
if err != nil {
fmt.Println(err)
}
var library Library
err = json.Unmarshal(jsonFile, &library)
if err != nil {
log.Fatal(err)
}
log.Printf("Tracks num: %d", len(library.Tracks))
// first start an HTTP server
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)
if err != nil {
log.Fatal(err)
}
}()
url := auth.AuthURL(state)
err = browser.OpenURL(url)
if err != nil {
log.Printf("failed to open browser with error: %s. please do it manually", err)
}
log.Println("Please log in to Spotify by visiting the following page in your browser:", url)
// wait for auth to complete
client := <-ch
// use the client to make calls that require authorization
user, err := client.CurrentUser(context.Background())
if err != nil {
log.Fatal(err)
}
log.Println("You are logged in as:", user.ID)
pls, err := client.CurrentUsersPlaylists(context.Background())
if err != nil {
log.Fatal(err)
}
var found bool
for _, pl := range pls.Playlists {
fmt.Println(pl.Name)
if pl.Name == *playlist {
found = true
err := AddTracksToPlaylist(client, pl, library)
if err != nil {
log.Fatal(err)
}
}
}
if !found {
log.Printf("No playlist with name %s were found, make sure you have one\n", *playlist)
}
}