-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_linux.go
125 lines (99 loc) · 2.43 KB
/
main_linux.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 (
"bufio"
"flag"
"os"
"os/signal"
"syscall"
"time"
"github.com/fatih/color"
"github.com/pmh-only/spoti2wall/config"
"github.com/pmh-only/spoti2wall/rest"
"github.com/pmh-only/spoti2wall/utils"
)
var blurFlag int
var darkFlag int
var reauthFlag bool
func init() {
config.InitConfig()
flag.IntVar(&blurFlag, "b", 0, "Blur image with blur option")
flag.IntVar(&darkFlag, "d", 0, "Dark image with darker option")
flag.BoolVar(&reauthFlag, "reauth", false, "Reauth with spotify")
flag.Parse()
rest.RefreshToken = utils.ReadRefreshToken()
}
func getClientId() string {
return config.GlobalConfig.Section("").Key("client_id").String()
}
func main() {
color.Magenta("🎵 spoti2wall started...")
if getClientId() == "" {
color.Green("📝 Enter client id [default]: ")
scanner := bufio.NewScanner(os.Stdin)
_ = scanner.Scan()
if scanner.Text() == "" {
utils.SaveClientId(rest.ClientId)
} else {
utils.SaveClientId(scanner.Text())
}
color.Green("📝 Enter client secret [default]: ")
_ = scanner.Scan()
if scanner.Text() == "" {
utils.SaveClientSecret(rest.ClientSecret)
} else {
utils.SaveClientSecret(scanner.Text())
}
// reinit for refresh values
config.InitConfig()
}
nitrogenExist := utils.CheckWallpaperCliExist()
if !nitrogenExist {
color.Red("❌ You need to install `nitrogen` before use.")
os.Exit(-1)
return
}
if reauthFlag || rest.RefreshToken == "" {
rest.StartAuthServer()
} else {
rest.AccessToken = rest.RefreshAccessToken(rest.RefreshToken)
go rest.KeepRefreshToken()
}
prevImage := ""
go func() {
for {
time.Sleep(100 * time.Millisecond)
image := rest.GetTrackImage()
if image == "" {
utils.RestoreWallpaper()
prevImage = ""
continue
}
if image == prevImage {
continue
}
prevImage = image
filepath := utils.CalcTmpPath(image, blurFlag, darkFlag)
if !utils.CheckImageExist(filepath) {
color.Yellow("🔍 Downloading image...")
utils.DownloadImage(image, filepath)
if blurFlag > 0 {
utils.BlurImage(filepath, blurFlag)
}
if darkFlag > 0 {
utils.DarkImage(filepath, darkFlag)
}
} else {
color.Yellow("📁 Image already exists. Skipping download.")
}
utils.ApplyWallpaper(filepath)
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig,
syscall.SIGTERM,
syscall.SIGINT,
os.Interrupt)
<-sig
color.Yellow("🔌 Shutting down...")
utils.RestoreWallpaper()
}