-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_windows.go
131 lines (103 loc) · 2.64 KB
/
main_windows.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
package main
import (
"bufio"
"flag"
"fmt"
"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...")
defaultWallpaperPath, err := utils.GetWallpaperPath()
if err != nil {
color.Red("❌ Cannot get default wallpaper path.")
}
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()
}
wallpaperManager := utils.NewWallpaperManager(defaultWallpaperPath)
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 == "" {
wallpaperManager.RestoreWallpaper()
prevImage = ""
continue
}
if image == prevImage {
continue
}
prevImage = image
filepath := utils.CalcTmpPath(image, blurFlag, darkFlag)
if !utils.CheckImageExist(filepath) {
color.Yellow("🔍 Downloading image...")
err := utils.DownloadImage(image, filepath)
if err != nil {
color.Red("❌ Failed to download image: %v", err)
continue
}
if blurFlag > 0 {
utils.BlurImage(filepath, blurFlag)
}
if darkFlag > 0 {
utils.DarkImage(filepath, darkFlag)
}
} else {
color.Yellow("📁 Image already exists. Skipping download.")
}
wallpaperManager.ApplyWallpaper(fmt.Sprintf("C:%s", filepath))
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig,
syscall.SIGTERM,
syscall.SIGINT,
os.Interrupt)
<-sig
color.Yellow("🔌 Shutting down...")
wallpaperManager.RestoreWallpaper()
}