-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
696 lines (573 loc) · 17.6 KB
/
Copy pathmain.go
File metadata and controls
696 lines (573 loc) · 17.6 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/csv"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
const (
SheetURL = "https://docs.google.com/spreadsheets/d/1Z8aANbxXbnUGoZPRvJfWL3gz6jrzPPrwVt3d0c1iJ_4"
ZipURL = SheetURL + "/export?format=zip"
XlsxURL = SheetURL + "/export?format=xlsx"
ZipFilename = "Trackerhub.zip"
HTMLFilename = "Artists.html"
CSVFilename = "artists.csv"
XlsxFilename = "artists.xlsx"
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0 Safari/537.36"
BaseURL = "https://sheets.artistgrid.cx"
UpdateIntervalSeconds = 600
InfoPath = "info/status.json"
DEV_MODE = false
)
var ExcludeNames = map[string]bool{
"🎹Worst Comps & Edits": true,
"🎹 Yedits": true,
"🎹 Comps & Edits": true,
"Comps & Edits": true,
"Worst Comps & Edits": true,
"Yedits": true,
"K4$H K4$$!N0": true,
"K4HKn0": true,
"AI Models": true,
"🎹 BPM & Key Tracker": true,
"🎹Comps & Edits": true,
"🎹 Worst Comps & Edits": true,
"Allegations": true,
"Rap Disses Timeline": true,
"Underground Artists": true,
"bpmkeytracker": true,
}
var NameReplacements = map[string]string{
"Lasagna": "Frank Ocean",
}
var ManualCSVRows = [][]string{
{"Kanye West", "https://docs.google.com/spreadsheets/d/1VfpFhHpcLK6G_4sLKykLHV0PdlQar1Fc6sk5TLubMRg/", "p4, @kiwieater, Maker, Bobby, SamV1sion, @comptonrapper, Rose, Dr Wolf, Oreo Eater, Arco, @Free The Robots, @Alek, @Commandtechno, Snoop Dogg, Awesomefied, @rocky, @flab, Shadow, Reuben🇮🇪, @razacosmica, @Marcemaire, Solidus Jack, Marin, garfiiieeelld", "Yes", "Yes", "Yes"},
{"Kanye West (Suzy version)", "https://docs.google.com/spreadsheets/d/14nhCu-LYd4gi-r7jsVjJRKqUh4SkcwMA4b-1V15wdug/", "p4, @kiwieater, Maker, Bobby, SamV1sion, @comptonrapper, Rose, Dr Wolf, Oreo Eater, Arco, @Free The Robots, @Alek, @Commandtechno, Snoop Dogg, Awesomefied, @rocky, @flab, Shadow, Reuben🇮🇪, @razacosmica, @Marcemaire, Solidus Jack, Marin, garfiiieeelld", "Yes", "Yes", "no"},
{"BI$H", "https://docs.google.com/spreadsheets/d/1aoaUmgc4EyJ6hp6Ea5FrstdMcA3rYFW9g7drlrSiiXI/", "fish (?, dont take my word on this im not sure)", "Yes", "Yes", "No"},
{"mzyxx", "https://docs.google.com/spreadsheets/d/1fbUISzmf3BqhJKwQKl4gegjadO8X6Db77B_TJw1YtsA/", "xyan", "Yes", "Yes", "No"},
{"Unc and Phew", "https://docs.google.com/spreadsheets/d/1-JdaCDJOSA6NTmClTnnmEMBGTqNgaw-RZiQ7ulABpO8/", "xyan, michael", "Yes", "Yes", "No"},
{"Tyler, the Creator", "https://docs.google.com/spreadsheets/d/10jvvqsnTrPbPqtfkJTn24-xrhfAssFQxuDwWY9CpZow/", "?", "Yes", "Yes", "yes"},
{"Afrosurrealist", "https://docs.google.com/spreadsheets/d/1OfLRtdfW0SikpmOJpzBXNVv5L-O3zxP245_T-i6Twgo/", "xyanprod", "Yes", "Yes", "No"},
{"prodbycon", "https://docs.google.com/spreadsheets/d/17jBBPM5ygq8gjx-d5CY_hvFmVPhGOIIhi3s2WTcikI8/", "prodbycon", "Yes", "Yes", "No"},
{"Camilla Cabello (Sanchez Version)", "https://docs.google.com/spreadsheets/d/1XHIMrA-sE4SsT1Xf3W9om0lLwVhOUxeR2v3JM0J8BDw/", "Sanchez05310", "Yes", "Yes", "No"},
{"EsDeeKid", "https://docs.google.com/spreadsheets/d/1adwl0w_cAvqw7ZN4jnGNKjh_dOzEiYBCRgGiVcEs8cY/", "drosoje1444_72902", "Yes", "Yes", "No"},
}
var (
lastHTMLHash string
lastCSVData ArtistData
emojiRegex = regexp.MustCompile(`[\p{So}\p{Sk}\x{FE0F}\x{FE0E}\x{200D}⭐🤖🎭︎]+`)
)
type ArtistData map[string]map[string]string
type FileInfo struct {
Hash string `json:"hash"`
}
type StatusInfo struct {
LastUpdated string `json:"last_updated"`
Files map[string]FileInfo `json:"files"`
}
type DiscordMessage struct {
Content string `json:"content"`
}
func cleanArtistName(text string) string {
cleaned := emojiRegex.ReplaceAllString(text, "")
cleaned = strings.TrimSpace(cleaned)
cleaned = strings.TrimPrefix(cleaned, " ")
if replacement, exists := NameReplacements[cleaned]; exists {
cleaned = replacement
}
return cleaned
}
func forceStarFlag(starred bool) string {
if starred {
return "Yes"
}
return "No"
}
func hashFile(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "file_not_found", err
}
defer f.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func downloadFile(url, filename string, timeout time.Duration) bool {
log.Printf("Downloading %s...\n", filename)
client := &http.Client{Timeout: timeout}
resp, err := client.Get(url)
if err != nil {
log.Printf("ERROR: Failed to download %s: %v\n", filename, err)
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("ERROR: Failed to download %s: status %d\n", filename, resp.StatusCode)
return false
}
out, err := os.Create(filename)
if err != nil {
log.Printf("ERROR: Failed to create file %s: %v\n", filename, err)
return false
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Printf("ERROR: Failed to write file %s: %v\n", filename, err)
return false
}
log.Printf("SUCCESS: Saved %s\n", filename)
return true
}
func downloadZipAndExtractHTML() {
if !downloadFile(ZipURL, ZipFilename, 30*time.Second) {
return
}
log.Printf("Extracting %s from %s...\n", HTMLFilename, ZipFilename)
r, err := zip.OpenReader(ZipFilename)
if err != nil {
log.Printf("ERROR: Failed to open zip file: %v\n", err)
return
}
defer r.Close()
for _, f := range r.File {
if f.Name == HTMLFilename {
rc, err := f.Open()
if err != nil {
log.Printf("ERROR: Failed to open file in zip: %v\n", err)
return
}
defer rc.Close()
content, err := io.ReadAll(rc)
if err != nil {
log.Printf("ERROR: Failed to read file from zip: %v\n", err)
return
}
err = os.WriteFile(HTMLFilename, content, 0644)
if err != nil {
log.Printf("ERROR: Failed to write extracted file: %v\n", err)
return
}
log.Printf("SUCCESS: Extracted %s\n", HTMLFilename)
return
}
}
log.Printf("ERROR: %s not found in zip archive\n", HTMLFilename)
}
func downloadXLSX() {
downloadFile(XlsxURL, XlsxFilename, 30*time.Second)
}
func quoteCSVField(field string) string {
escaped := strings.ReplaceAll(field, `"`, `""`)
return `"` + escaped + `"`
}
func writeCSVRow(w io.Writer, fields []string) error {
quotedFields := make([]string, len(fields))
for i, field := range fields {
quotedFields[i] = quoteCSVField(field)
}
_, err := w.Write([]byte(strings.Join(quotedFields, ",") + "\n"))
return err
}
func generateCSV() {
log.Printf("Generating %s from %s...\n", CSVFilename, HTMLFilename)
f, err := os.Open(HTMLFilename)
if err != nil {
log.Printf("ERROR: %s not found. Cannot generate CSV.\n", HTMLFilename)
return
}
defer f.Close()
doc, err := goquery.NewDocumentFromReader(f)
if err != nil {
log.Printf("ERROR: Failed to parse HTML: %v\n", err)
return
}
tableBody := doc.Find("table.waffle tbody")
if tableBody.Length() == 0 {
log.Println("ERROR: Could not find the table body in HTML. Cannot generate CSV.")
return
}
rows := tableBody.Find("tr")
var data [][]string
starringSection := true
existingArtists := make(map[string]bool)
rows.Each(func(i int, row *goquery.Selection) {
if i < 3 {
return
}
cells := row.Find("td")
if cells.Length() < 4 {
return
}
artistNameRaw := cells.Eq(0).Text()
artistNameRaw = strings.TrimSpace(artistNameRaw)
artistURL, _ := cells.Eq(0).Find("a").Attr("href")
if artistNameRaw == "" || artistURL == "" {
return
}
if strings.Contains(artistNameRaw, "AI Models") {
starringSection = false
}
artistNameClean := cleanArtistName(artistNameRaw)
if ExcludeNames[artistNameClean] || strings.Contains(artistNameRaw, "🚩") {
return
}
lowerName := strings.ToLower(artistNameClean)
if strings.Contains(lowerName, "bpm") && strings.Contains(lowerName, "key") {
return
}
credit := strings.TrimSpace(cells.Eq(1).Text())
linksWork := strings.TrimSpace(cells.Eq(3).Text())
updated := strings.TrimSpace(cells.Eq(2).Text())
best := forceStarFlag(starringSection)
data = append(data, []string{
artistNameClean,
artistURL,
credit,
linksWork,
updated,
best,
})
existingArtists[artistNameClean] = true
})
for _, manualRow := range ManualCSVRows {
if len(manualRow) >= 6 {
artistName := manualRow[0]
if !existingArtists[artistName] {
lowerName := strings.ToLower(artistName)
if strings.Contains(lowerName, "bpm") && strings.Contains(lowerName, "key") {
continue
}
data = append(data, manualRow)
existingArtists[artistName] = true
}
}
}
sort.Slice(data, func(i, j int) bool {
bestI := data[i][5]
bestJ := data[j][5]
nameI := data[i][0]
nameJ := data[j][0]
if bestI != bestJ {
return bestI > bestJ
}
return strings.ToLower(nameI) < strings.ToLower(nameJ)
})
csvFile, err := os.Create(CSVFilename)
if err != nil {
log.Printf("ERROR: Failed to create CSV file %s: %v\n", CSVFilename, err)
return
}
defer csvFile.Close()
header := []string{"Artist Name", "URL", "Credit", "Links Work", "Updated", "Best"}
if err := writeCSVRow(csvFile, header); err != nil {
log.Printf("ERROR: Failed to write CSV header: %v\n", err)
return
}
for _, record := range data {
if err := writeCSVRow(csvFile, record); err != nil {
log.Printf("ERROR: Failed to write CSV row: %v\n", err)
return
}
}
log.Printf("SUCCESS: Generated %s with %d rows.\n", CSVFilename, len(data))
}
func readCSVToDict(filename string) ArtistData {
data := make(ArtistData)
f, err := os.Open(filename)
if err != nil {
log.Printf("WARNING: CSV file not found: %s\n", filename)
return data
}
defer f.Close()
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
log.Printf("ERROR: Error reading CSV file %s: %v\n", filename, err)
return data
}
if len(records) == 0 {
return data
}
headers := records[0]
for _, record := range records[1:] {
if len(record) < len(headers) {
continue
}
row := make(map[string]string)
for i, header := range headers {
row[header] = record[i]
}
if artistName, ok := row["Artist Name"]; ok && artistName != "" {
data[artistName] = row
}
}
return data
}
func detectChanges(oldData, newData ArtistData) []string {
var changes []string
oldKeys := make(map[string]bool)
newKeys := make(map[string]bool)
for k := range oldData {
oldKeys[k] = true
}
for k := range newData {
newKeys[k] = true
}
var removed []string
for k := range oldKeys {
if !newKeys[k] {
removed = append(removed, k)
}
}
sort.Strings(removed)
var added []string
for k := range newKeys {
if !oldKeys[k] {
added = append(added, k)
}
}
sort.Strings(added)
var common []string
for k := range oldKeys {
if newKeys[k] {
common = append(common, k)
}
}
sort.Strings(common)
for _, artist := range removed {
changes = append(changes, "REMOVED: **"+artist+"**")
}
for _, artist := range added {
changes = append(changes, "ADDED: **"+artist+"**")
}
for _, artist := range common {
oldRow := oldData[artist]
newRow := newData[artist]
if oldRow["URL"] != newRow["URL"] {
changes = append(changes, "LINK CHANGED: **"+artist+"**")
}
if oldRow["Credit"] != newRow["Credit"] {
changes = append(changes, "CREDIT CHANGED: **"+artist+"**")
}
if oldRow["Links Work"] != newRow["Links Work"] {
changes = append(changes, "LINKS WORK STATUS CHANGED: **"+artist+"**")
}
if oldRow["Updated"] != newRow["Updated"] {
changes = append(changes, "UPDATED DATE CHANGED: **"+artist+"**")
}
if oldRow["Best"] != newRow["Best"] {
changes = append(changes, "BEST FLAG CHANGED: **"+artist+"**")
}
}
return changes
}
func sendDiscordMessage(content string) {
webhookURL := os.Getenv("DISCORD_WEBHOOK_URL")
if webhookURL == "" {
log.Println("WARNING: Discord webhook URL not set. Skipping notification.")
return
}
if len(content) > 2000 {
content = content[:1990] + "\n... (truncated)"
}
message := DiscordMessage{Content: content}
jsonData, err := json.Marshal(message)
if err != nil {
log.Printf("WARNING: Failed to marshal Discord message: %v\n", err)
return
}
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Printf("WARNING: Exception sending Discord notification: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Println("SUCCESS: Discord notification sent successfully.")
} else {
log.Printf("WARNING: Discord notification failed with status: %d\n", resp.StatusCode)
}
}
func writeInfo(htmlHash, csvHash, xlsxHash string) {
os.MkdirAll("info", 0755)
nowISO := time.Now().UTC().Format(time.RFC3339)
var info StatusInfo
data, err := os.ReadFile(InfoPath)
if err == nil {
json.Unmarshal(data, &info)
}
if info.Files == nil {
info.Files = make(map[string]FileInfo)
}
info.LastUpdated = nowISO
info.Files[HTMLFilename] = FileInfo{Hash: htmlHash}
info.Files[CSVFilename] = FileInfo{Hash: csvHash}
info.Files[XlsxFilename] = FileInfo{Hash: xlsxHash}
jsonData, err := json.MarshalIndent(info, "", " ")
if err != nil {
log.Printf("WARNING: Failed to marshal status info: %v\n", err)
return
}
os.WriteFile(InfoPath, jsonData, 0644)
}
func runDevTests() {
log.Println("=== DEVELOPMENT MODE - Running Tests ===")
log.Println("\nTesting Discord Webhook...")
testMessage := fmt.Sprintf("**Development Mode Test**\nTimestamp: %s\nWebhook is working correctly!", time.Now().Format(time.RFC3339))
sendDiscordMessage(testMessage)
log.Println("\nDevelopment tests completed!")
log.Println("=========================================\n")
}
func updateLoop() {
for {
log.Println("--- Starting update cycle ---")
downloadZipAndExtractHTML()
downloadXLSX()
generateCSV()
files := []string{HTMLFilename, CSVFilename, XlsxFilename}
allExist := true
for _, f := range files {
if _, err := os.Stat(f); os.IsNotExist(err) {
allExist = false
break
}
}
if !allExist {
log.Println("WARNING: One or more files are missing after download/parse. Skipping this cycle.")
time.Sleep(UpdateIntervalSeconds * time.Second)
continue
}
htmlHash, _ := hashFile(HTMLFilename)
csvHash, _ := hashFile(CSVFilename)
xlsxHash, _ := hashFile(XlsxFilename)
currentCSVData := readCSVToDict(CSVFilename)
if lastHTMLHash == "" {
log.Println("INFO: First run: storing initial file hashes.")
} else if htmlHash != lastHTMLHash {
log.Println("ALERT: Artists.html has changed! Checking for data differences.")
changes := detectChanges(lastCSVData, currentCSVData)
if len(changes) > 0 {
message := "**Tracker Update Detected:**\n" + strings.Join(changes, "\n")
sendDiscordMessage(message)
} else {
log.Println("INFO: HTML hash changed, but no data differences found.")
}
} else {
log.Println("INFO: Artists.html is unchanged.")
}
writeInfo(htmlHash, csvHash, xlsxHash)
lastHTMLHash = htmlHash
lastCSVData = currentCSVData
log.Println("--- Update cycle finished ---")
log.Printf("Sleeping for %d seconds...\n", UpdateIntervalSeconds)
time.Sleep(UpdateIntervalSeconds * time.Second)
}
}
func getStatusData() (*StatusInfo, error) {
data, err := os.ReadFile(InfoPath)
if err != nil {
return nil, err
}
var status StatusInfo
err = json.Unmarshal(data, &status)
if err != nil {
return nil, err
}
return &status, nil
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
if DEV_MODE {
runDevTests()
}
log.Println("Starting background update goroutine...")
go updateLoop()
app := fiber.New()
app.Use(cors.New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendFile("templates/index.html")
})
app.Get("/robots.txt", func(c *fiber.Ctx) error {
return c.SendFile("templates/robots.txt")
})
app.Get("/artists.html", func(c *fiber.Ctx) error {
return c.SendFile(HTMLFilename)
})
app.Get("/artists.csv", func(c *fiber.Ctx) error {
return c.SendFile(CSVFilename)
})
app.Get("/artists.xlsx", func(c *fiber.Ctx) error {
return c.SendFile(XlsxFilename)
})
app.Static("/_next", "templates/_next")
app.Get("/info", func(c *fiber.Ctx) error {
data, err := getStatusData()
if err != nil {
return c.Status(404).JSON(fiber.Map{"error": "Info not available"})
}
return c.JSON(data)
})
app.Get("/info/html", func(c *fiber.Ctx) error {
data, err := getStatusData()
if err != nil {
c.Set("Content-Type", "text/html")
return c.Status(404).SendString("<p>Status info not available.</p>")
}
htmlInfo := data.Files[HTMLFilename]
csvInfo := data.Files[CSVFilename]
xlsxInfo := data.Files[XlsxFilename]
html := fmt.Sprintf(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Info</title>
<style>body { font-family: sans-serif; } li { margin-bottom: 1em; }</style>
</head>
<body>
<h1>Latest File Info</h1>
<p><strong>Last Updated:</strong> %s</p>
<ul>
<li><strong>%s</strong><br>
Hash: %s
</li>
<li><strong>%s</strong><br>
Hash: %s
</li>
<li><strong>%s</strong><br>
Hash: %s
</li>
</ul>
</body>
</html>
`, data.LastUpdated,
HTMLFilename, htmlInfo.Hash,
CSVFilename, csvInfo.Hash,
XlsxFilename, xlsxInfo.Hash)
c.Set("Content-Type", "text/html")
return c.SendString(html)
})
app.Use(func(c *fiber.Ctx) error {
return c.Status(404).SendFile("templates/404.html")
})
log.Println("Starting Fiber server on :5000...")
log.Fatal(app.Listen(":5000"))
}