forked from fabiofalci/sconsify
/
sconsify.go
126 lines (111 loc) · 4.17 KB
/
sconsify.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
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/fabiofalci/sconsify/infrastructure"
"github.com/fabiofalci/sconsify/rpc"
"github.com/fabiofalci/sconsify/sconsify"
"github.com/fabiofalci/sconsify/spotify"
"github.com/fabiofalci/sconsify/ui/noui"
"github.com/fabiofalci/sconsify/ui/simple"
"github.com/howeyc/gopass"
"runtime"
"strconv"
)
var version string
var commit string
var buildDate string
var spotifyClientId string
var authRedirectUrl string
func init() {
rand.Seed(time.Now().Unix())
}
func main() {
infrastructure.ProcessSconsifyrc()
providedUsername := flag.String("username", "", "Spotify username.")
providedWebApi := flag.Bool("web-api", false, "Use Spotify WEB API for more features. It requires web authorization.")
providedUi := flag.Bool("ui", true, "Run Sconsify with Console User Interface. If false then no User Interface will be presented and it'll shuffle tracks.")
providedPlaylists := flag.String("playlists", "", "Select just some Playlists to play. Comma separated list.")
providedPreferredBitrate := flag.String("preferred-bitrate", "320k", "Preferred bitrate: 96k, 160k, 320k (default).")
providedNoUiSilent := flag.Bool("noui-silent", false, "Silent mode when no UI is used.")
providedNoUiRepeatOn := flag.Bool("noui-repeat-on", true, "Play your playlist and repeat it after the last track.")
providedNoUiShuffle := flag.Bool("noui-shuffle", true, "Shuffle tracks or follow playlist order.")
providedWebApiCacheToken := flag.Bool("web-api-cache-token", true, "Cache the web-api token as plain text in ~/.sconsify until its expiration.")
providedWebApiCacheContent := flag.Bool("web-api-cache-content", true, "Cache some of the web-api content as plain text in ~/.sconsify.")
providedDebug := flag.Bool("debug", false, "Enable debug mode.")
askingVersion := flag.Bool("version", false, "Print version.")
providedCommand := flag.String("command", "", "Execute a command in the server: replay, play_pause, next")
providedServer := flag.Bool("server", true, "Start a background server to accept commands.")
flag.Parse()
if *askingVersion {
fmt.Println("Version: " + version)
fmt.Println("Git commit: " + commit)
if i, err := strconv.ParseInt(buildDate, 10, 64); err == nil {
fmt.Println("Build date: " + time.Unix(i, 0).UTC().String())
}
fmt.Println("Go version: " + runtime.Version())
os.Exit(0)
}
if *providedDebug {
infrastructure.InitialiseLogger()
defer infrastructure.CloseLogger()
}
if *providedCommand != "" {
rpc.Client(*providedCommand)
return
}
fmt.Println("Sconsify - your awesome Spotify music service in a text-mode interface.")
username, pass := credentials(providedUsername)
events := sconsify.InitialiseEvents()
initConf := &spotify.SpotifyInitConf{
WebApiAuth: *providedWebApi,
PlaylistFilter: *providedPlaylists,
PreferredBitrate: *providedPreferredBitrate,
CacheWebApiToken: *providedWebApiCacheToken,
CacheWebApiContent: *providedWebApiCacheContent,
SpotifyClientId: spotifyClientId,
AuthRedirectUrl: authRedirectUrl,
}
go spotify.Initialise(initConf, username, pass, events)
if *providedServer {
go rpc.StartServer(events)
}
if *providedUi {
ui := simple.InitialiseConsoleUserInterface(events, true)
sconsify.StartMainLoop(events, ui, false)
} else {
var output noui.Printer
if *providedNoUiSilent {
output = new(noui.SilentPrinter)
}
ui := noui.InitialiseNoUserInterface(events, output, providedNoUiRepeatOn, providedNoUiShuffle)
sconsify.StartMainLoop(events, ui, true)
}
}
func credentials(providedUsername *string) (string, []byte) {
username := ""
if *providedUsername == "" {
fmt.Print("Premium account username: ")
reader := bufio.NewReader(os.Stdin)
username, _ = reader.ReadString('\n')
} else {
username = *providedUsername
fmt.Println("Provided username: " + username)
}
return strings.Trim(username, " \n\r"), getPassword()
}
func getPassword() []byte {
passFromEnv := os.Getenv("SCONSIFY_PASSWORD")
if passFromEnv != "" {
fmt.Println("Reading password from environment variable SCONSIFY_PASSWORD.")
return []byte(passFromEnv)
}
fmt.Print("Password: ")
b, _ := gopass.GetPasswdMasked()
return b
}