-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
243 lines (230 loc) · 8.29 KB
/
Copy pathmain.go
File metadata and controls
243 lines (230 loc) · 8.29 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"fmt"
"log/slog"
"os"
"path"
"time"
"github.com/cterence/scrobble-deduplicator/internal/app"
altsrc "github.com/urfave/cli-altsrc/v3"
"github.com/urfave/cli-altsrc/v3/yaml"
"github.com/urfave/cli/v3"
)
var (
version = "dev"
commit = "unknown"
date = "unknown"
)
func setLogger(logLevel string) error {
var slogLogLevel slog.Level
switch logLevel {
case "debug":
slogLogLevel = slog.LevelDebug
case "info":
slogLogLevel = slog.LevelInfo
case "warn":
slogLogLevel = slog.LevelWarn
case "error":
slogLogLevel = slog.LevelError
default:
return fmt.Errorf("unknown log level: %s", logLevel)
}
logOpts := slog.HandlerOptions{
Level: slogLogLevel,
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &logOpts)))
return nil
}
func main() {
var (
configFilePath string
cacheType string
lastFMUsername string
lastFMPassword string
startPage int
from time.Time
to time.Time
browserHeadful bool
browserURL string
redisURL string
canDelete bool
logLevel string
duplicateThreshold int
completeThreshold int
processingMode string
dataDir string
telegramBotToken string
telegramChatID string
)
wd, err := os.Getwd()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
cmd := &cli.Command{
Name: "scrobble-deduplicator",
Usage: "Deduplicate Last.fm scrobbles",
Version: fmt.Sprintf("Version: %s\nCommit: %s\nBuild Date: %s", version, commit, date),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: "config.yaml",
Usage: "Path to the configuration file",
Destination: &configFilePath,
},
&cli.StringFlag{
Name: "lastfm-username",
Aliases: []string{"u"},
Usage: "Last.fm username",
Required: true,
Sources: cli.NewValueSourceChain(cli.EnvVar("LASTFM_USERNAME"), yaml.YAML("lastfm.username", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &lastFMUsername,
},
&cli.StringFlag{
Name: "lastfm-password",
Aliases: []string{"p"},
Usage: "Last.fm password",
Required: true,
Sources: cli.NewValueSourceChain(cli.EnvVar("LASTFM_PASSWORD"), yaml.YAML("lastfm.password", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &lastFMPassword,
},
&cli.BoolFlag{
Name: "delete",
Usage: "Delete duplicate scrobbles",
Value: false,
Sources: cli.NewValueSourceChain(cli.EnvVar("DELETE"), yaml.YAML("delete", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &canDelete,
},
&cli.IntFlag{
Name: "duplicate-threshold",
Usage: "Percentage of a track's duration below which two successive scrobbles are considered duplicates",
Value: 90,
Sources: cli.NewValueSourceChain(cli.EnvVar("DUPLICATE_THRESHOLD"), yaml.YAML("duplicateThreshold", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &duplicateThreshold,
},
&cli.IntFlag{
Name: "complete-threshold",
Usage: "Percentage of a track's duration to consider a scrobble complete, set a value to enable",
Sources: cli.NewValueSourceChain(cli.EnvVar("COMPLETE_THRESHOLD"), yaml.YAML("completeThreshold", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &completeThreshold,
},
&cli.IntFlag{
Name: "start-page",
Aliases: []string{"s"},
Usage: "Last.fm scrobble library page to start from",
Sources: cli.NewValueSourceChain(cli.EnvVar("START_PAGE"), yaml.YAML("startPage", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &startPage,
},
&cli.TimestampFlag{
Name: "from",
Usage: "Day at which the program should start deduplicating scrobbles (layout: 02-01-2006)",
Config: cli.TimestampConfig{
Layouts: []string{app.InputDayFormat},
},
Sources: cli.NewValueSourceChain(cli.EnvVar("FROM"), yaml.YAML("from", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &from,
},
&cli.TimestampFlag{
Name: "to",
Usage: "Day at which the program should end deduplicating scrobbles (layout: 02-01-2006)",
Config: cli.TimestampConfig{
Layouts: []string{app.InputDayFormat},
},
Sources: cli.NewValueSourceChain(cli.EnvVar("TO"), yaml.YAML("to", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &to,
},
&cli.StringFlag{
Name: "processing-mode",
Usage: "Mode for processing the scrobbles (sequential, parallel)",
Value: "sequential",
Sources: cli.NewValueSourceChain(cli.EnvVar("PROCESSING_MODE"), yaml.YAML("processingMode", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &processingMode,
},
&cli.StringFlag{
Name: "cache-type",
Usage: "Cache type for MusicBrainz API queries (inmemory, file, redis) (must specify redis-url flag for redis)",
Value: "inmemory",
Sources: cli.NewValueSourceChain(cli.EnvVar("CACHE_TYPE"), yaml.YAML("cacheType", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &cacheType,
},
&cli.BoolFlag{
Name: "browser-headful",
Usage: "Run with a visible browser UI",
Sources: cli.NewValueSourceChain(cli.EnvVar("BROWSER_HEADFUL"), yaml.YAML("browserHeadful", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &browserHeadful,
},
&cli.StringFlag{
Name: "browser-url",
Usage: "Remote browser URL",
Sources: cli.NewValueSourceChain(cli.EnvVar("BROWSER_URL"), yaml.YAML("browserURL", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &browserURL,
},
&cli.StringFlag{
Name: "redis-url",
Usage: "Redis URL for redis cache type",
Sources: cli.NewValueSourceChain(cli.EnvVar("REDIS_URL"), yaml.YAML("redisURL", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &redisURL,
},
&cli.StringFlag{
Name: "data-dir",
Usage: "Path to a directory that this program can use to read and produce files",
Sources: cli.NewValueSourceChain(cli.EnvVar("DATA_DIR"), yaml.YAML("dataDir", altsrc.NewStringPtrSourcer(&configFilePath))),
Value: path.Join(wd, "data"),
Destination: &dataDir,
},
&cli.StringFlag{
Name: "log-level",
Usage: "Log level (debug, info, warn, error)",
Sources: cli.NewValueSourceChain(cli.EnvVar("LOG_LEVEL"), yaml.YAML("logLevel", altsrc.NewStringPtrSourcer(&configFilePath))),
Value: "info",
Destination: &logLevel,
},
&cli.StringFlag{
Name: "telegram-bot-token",
Usage: "Telegram Bot token to send a message to when a run finishes",
Sources: cli.NewValueSourceChain(cli.EnvVar("TELEGRAM_BOT_TOKEN"), yaml.YAML("telegram.botToken", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &telegramBotToken,
},
&cli.StringFlag{
Name: "telegram-chat-id",
Usage: "Telegram chat ID where the bot can send message to",
Sources: cli.NewValueSourceChain(cli.EnvVar("TELEGRAM_CHAT_ID"), yaml.YAML("telegram.chatID", altsrc.NewStringPtrSourcer(&configFilePath))),
Destination: &telegramChatID,
},
},
Action: func(context.Context, *cli.Command) error {
ctx := context.Background()
c := app.Config{
FilePath: configFilePath,
CacheType: cacheType,
LastFMUsername: lastFMUsername,
LastFMPassword: lastFMPassword,
StartPage: startPage,
From: from,
To: to,
BrowserHeadful: browserHeadful,
RedisURL: redisURL,
BrowserURL: browserURL,
CanDelete: canDelete,
LogLevel: logLevel,
DuplicateThreshold: duplicateThreshold,
CompleteThreshold: completeThreshold,
ProcessingMode: processingMode,
DataDir: dataDir,
TelegramBotToken: telegramBotToken,
TelegramChatID: telegramChatID,
}
err := setLogger(c.LogLevel)
if err != nil {
return fmt.Errorf("failed to set logger: %w", err)
}
return app.Run(ctx, &c)
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}