-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
240 lines (200 loc) · 5.33 KB
/
main.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
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
// main.go: Brainz command.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"time"
)
// Maximum value of an int64.
const MaxInt64 = int64(^uint(0) >> 1)
// ListenBrainzAPI points to the root of the ListenBrainz REST API.
// https://listenbrainz.readthedocs.io/en/latest/users/api
const ListenBrainzAPI = "https://api.listenbrainz.org/1"
// ItemsPerPage determines how many items to retrieve per request.
// Defaults to the maximum of MAX_ITEMS_PER_GET.
const ItemsPerPage = 1000
// Track describes a music track
type Track struct {
Name string `json:"track_name"`
Artist string `json:"artist_name"`
}
// Listen describes the Recording of a Track listened at a given ListenedAt time.
type Listen struct {
Recording string `json:"recording_msid"`
Track Track `json:"track_metadata"`
ListenedAt int64 `json:"listened_at"`
}
// Time the Track/Recording was listened to.
func (listen Listen) Time() time.Time {
return time.Unix(listen.ListenedAt, 0)
}
func (listen Listen) String() string {
return "<" + listen.Recording + "> " + listen.Track.Artist + " - \"" + listen.Track.Name + "\""
}
// Payload contains a set of Listen's.
type Payload struct {
Count int `json:"count"`
Latest int `json:"latest_listen_ts"`
Listens []Listen `json:"listens"`
}
// Listens contains a Payload describing a set of Listen's.
type Listens struct {
Payload Payload `json:"payload"`
}
func (listens *Listens) length() int {
if listens != nil {
return len(listens.Payload.Listens)
}
return 0
}
func deleteListen(listen Listen) bool {
url := ListenBrainzAPI + "/delete-listen"
// Create a payload to send in the request
payload := map[string]string{
"listened_at": fmt.Sprintf("%d", listen.ListenedAt),
"recording_msid": listen.Recording,
}
jsonpayload, err := json.Marshal(payload)
if err != nil {
panic(err)
}
// Create a new http get request
req, err := http.NewRequest("post", url, bytes.NewBuffer(jsonpayload))
if err != nil {
fmt.Println("error creating request:", err)
return false
}
req.Header.Set("content-type", "application/json")
req.Header.Set("authorization", fmt.Sprintf("token %s", os.Getenv("brainz_token")))
// Make the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if verbosePrint {
fmt.Printf("(debug) deletelisten(%s, %s): response status: %s\n",
listen.Time(), listen.Recording, resp.Status)
}
return resp.Status == "200 ok"
}
func lastTimestamp(listens []Listen) int64 {
return listens[len(listens)-1].ListenedAt
}
func getAllListens() []Listen {
var listens []Listen
timestamp := int64(0)
for {
page := getListens(timestamp)
if page.length() == 0 {
break
}
timestamp = lastTimestamp(page.Payload.Listens)
for _, listen := range page.Payload.Listens {
listens = append(listens, listen)
if int64(len(listens)) >= maxCount {
return listens
}
}
}
return listens
}
func getListens(max int64) Listens {
url := fmt.Sprintf("%s/user/%s/listens?count=%d",
ListenBrainzAPI, userName, ItemsPerPage)
if max > 0 {
url = fmt.Sprintf("%s&max_ts=%d", url, max)
}
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return Listens{}
}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return Listens{}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return Listens{}
}
var listens Listens
err = json.Unmarshal(body, &listens)
if err != nil {
fmt.Println("Error:", err)
return Listens{}
}
return listens
}
var (
maxCount int64
deleteListens bool
userName string
searchPattern string
verbosePrint bool
showUsage bool
)
func init() {
flag.Int64Var(&maxCount, "c", MaxInt64, "Maxium number of items.")
flag.BoolVar(&deleteListens, "d", false, "Delete matched listens.")
flag.BoolVar(&verbosePrint, "v", false, "Debug/verbose output.")
flag.StringVar(&userName, "u", "", "The user name or login ID.")
flag.StringVar(&searchPattern, "s", ".+", "The search pattern.")
flag.BoolVar(&showUsage, "h", false, "Show usage help.")
}
func usage() {
fmt.Println("Usage: go run main.go [-lcdvh] -u <username> -s <regexp>")
fmt.Println(" -c: Limit action to a number of items.")
fmt.Println(" -d: Delete matched listens.")
fmt.Println(" -u: The user name or login ID.")
fmt.Println(" -s: Search regexp pattern.")
fmt.Println(" -v: Debug/verbose output.")
fmt.Println(" -h: Show this help.")
os.Exit(2)
}
func brainz() {
var listens []Listen = getAllListens()
for _, listen := range listens {
match, err := regexp.MatchString("(?i)"+searchPattern, listen.String())
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
if match {
fmt.Println(listen)
if deleteListens && !deleteListen(listen) {
fmt.Printf("Warning: failed deleting listen: %s", listen)
}
}
}
}
func main() {
flag.Parse()
if showUsage {
usage()
}
if os.Getenv("LISTENBRAINZ_TOKEN") == "" {
fmt.Println("Error: please define LISTENBRAINZ_TOKEN.")
os.Exit(1)
}
if userName == "" {
fmt.Println("Error: username is missing.")
usage()
}
if maxCount < 1 {
fmt.Println("Error: invalid maxCount:", maxCount)
usage()
}
brainz()
}