-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (96 loc) · 3.19 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
package main
import (
"encoding/json"
"fmt"
"github.com/dmegyesi/spotifyImport/tracks"
log "github.com/sirupsen/logrus"
"github.com/zmb3/spotify"
"net/http"
"strings"
)
// redirectID is the OAuth redirect ID for the application.
// You must register an application at Spotify's developer portal
// and enter this value.
const redirectID = "http://localhost:8080/callback"
const LogLevel = log.DebugLevel //log.InfoLevel
const credentialsFile = "account.json"
var (
auth = spotify.NewAuthenticator(redirectID, spotify.ScopeUserReadPrivate, spotify.ScopePlaylistModifyPrivate)
ch = make(chan *spotify.Client)
state = "ignore-audience-pointless-actress-spinach-manhunt"
)
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(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 := auth.NewClient(tok)
fmt.Fprintf(w, "Login Completed!")
ch <- &client
}
func main() {
log.SetLevel(LogLevel)
credentials := parseSpotifyCredentials(credentialsFile)
auth.SetAuthInfo(credentials.SPOTIFY_ID, credentials.SPOTIFY_SECRET)
// 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 http.ListenAndServe(":8080", nil)
url := auth.AuthURL(state)
fmt.Println("Please log in to Spotify by visiting the following page in your browser:", url)
// wait for auth to complete
client := <-ch
// if we receive a rate limit from Spotify, don't give up
client.AutoRetry = true
var parsedTracks tracks.Tracks
var missingTracks tracks.Tracks
var playList tracks.PlayList
importTracksFromFile(&parsedTracks)
// Search songs with the Spotify API
for i, track := range parsedTracks.Tracks {
tracks.SearchSong(client, track, &playList, &missingTracks)
// If we're debugging, stop after 200 records
if LogLevel == log.DebugLevel {
if i == 200 {
break
}
}
}
log.Infof("Parse errors: %d songs", len(missingTracks.Tracks))
// For the missing tracks, try to do data cleaning and search again
var notFoundTracks int = 0
for _, track := range missingTracks.Tracks {
var artist string = track.Artist
var title string = track.Title
var album string = track.Album
// If the title or artist name contains a ( character, drop everything after it
if strings.Contains(title, "(") {
track.Title = strings.Split(title, "(")[0]
if strings.Contains(artist, "(") {
track.Artist = strings.Split(artist, "(")[0]
}
}
found := tracks.SearchSong(client, track, &playList, &missingTracks)
if !found {
log.WithFields(log.Fields{
"source": "spotify",
}).Errorf("I gave up on: %s - %s (%s)", track.Artist, track.Title, album)
notFoundTracks++
}
}
log.Infof("Not found: %d songs", notFoundTracks)
if LogLevel == log.DebugLevel {
missingList, _ := json.Marshal(missingTracks)
log.Debugln(string(missingList))
}
// Save songs to a new playlist
tracks.UploadPlaylist(client, &playList)
}