This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
/
accounts.go
164 lines (145 loc) · 3.13 KB
/
accounts.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
package lastradio
import (
"io/ioutil"
"log"
"os"
"time"
"github.com/gordonklaus/portaudio"
spotify "github.com/op/go-libspotify/spotify"
"github.com/shkh/lastfm-go/lastfm"
)
var (
LOCAL_APP_DIR = os.Getenv("HOME") + "/.local/share/lastradio"
)
type LastFmUser struct {
Username string
Password string
ApiKey string
ApiSecret string
}
func (u *LastFmUser) GetUsername() string {
return u.Username
}
type SpotifyUser struct {
AppKeyPath string
Username string
Password string
Remember bool
Debug bool
}
func LoginLastFm(user *LastFmUser) (api *lastfm.Api, err error) {
api = lastfm.New(user.ApiKey, user.ApiSecret)
err = api.Login(user.Username, user.Password)
return api, err
}
func loginToLastFm(lastFmUser *LastFmUser) (*lastfm.Api, error) {
// login and get api client
lastFmApi, err := LoginLastFm(lastFmUser)
if err != nil {
return nil, err
}
return lastFmApi, err
}
func getSpotifySession(spotifyUser *SpotifyUser) (*spotify.Session, error) {
appKey, err := ioutil.ReadFile(spotifyUser.AppKeyPath)
if err != nil {
log.Fatal(err)
return nil, err
}
log.Printf("libspotify %s", spotify.BuildId())
audio, err := newAudioWriter()
if err != nil {
log.Fatal(err)
}
session, err := spotify.NewSession(&spotify.Config{
ApplicationKey: appKey,
ApplicationName: "test",
CacheLocation: LOCAL_APP_DIR,
SettingsLocation: LOCAL_APP_DIR,
AudioConsumer: audio,
// Disable playlists to make playback faster
DisablePlaylistMetadataCache: true,
InitiallyUnloadPlaylists: true,
})
if err != nil {
log.Fatal(err)
return nil, err
}
return session, err
}
func loginToSpotifySession(session *spotify.Session, spotifyUser *SpotifyUser) error {
var err error
if len(spotifyUser.Password) > 0 {
credentials := spotify.Credentials{
Username: spotifyUser.Username,
Password: spotifyUser.Password,
}
if err = session.Login(credentials, spotifyUser.Remember); err != nil {
log.Fatal(err)
return err
}
} else {
if err = session.Relogin(); err != nil {
log.Fatal(err)
return err
}
}
// Wait for login and expect it to go fine
if err = <-session.LoggedInUpdates(); err != nil {
if err != nil {
log.Print(err)
return err
}
return nil
}
return nil
}
func HandleSpotifySession(session *spotify.Session, exit <-chan bool) {
exitAttempts := 0
running := true
log.Print("TODO: spotify signal handling")
for running {
select {
case <-session.LoggedOutUpdates():
running = false
case <-exit:
if exitAttempts >= 3 {
os.Exit(42)
}
exitAttempts++
session.Logout()
case <-time.After(5 * time.Second):
}
}
session.Close()
os.Exit(32)
}
func Close() {
portaudio.Terminate()
}
func init() {
createLocalDir(LOCAL_APP_DIR)
}
func createLocalDir(appDir string) {
exists, err := localDirExists(appDir)
if err != nil {
log.Fatal(err)
}
if exists == false {
log.Print("Create local dir")
err := os.Mkdir(appDir, 0755)
if err != nil {
log.Fatal(err)
}
}
}
func localDirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}