-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (84 loc) · 2.41 KB
/
main.go
File metadata and controls
95 lines (84 loc) · 2.41 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
"slices"
"strings"
"github.com/izzymg/hsnipe/config"
"github.com/izzymg/hsnipe/web"
)
func main() {
var quietFlag = flag.Bool("quiet", false, "No stdout")
configFilePath := "config.json"
var configFlag = flag.String("config", configFilePath, "Path to the config file")
var exportPath = flag.String("export", "", "Path to export results (CSV or JSON)")
var exportFormat = flag.String("format", "csv", "Export format: csv or json")
flag.Parse()
if configFlag != nil {
configFilePath = *configFlag
}
logger := log.New(os.Stdout, "hsnipe: ", log.Lmsgprefix)
if *quietFlag {
logger.SetOutput(io.Discard)
}
config, err := config.ParseConfig(configFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
search := web.NewSearch([]web.Provider{
web.NewPBTechProvider(*regexp.MustCompile(config.PBTechConfig.Filter)),
web.NewComputerLoungeProvider(*regexp.MustCompile(config.ComputerLoungeConfig.TitleFilter)),
web.NewAscentProvider(*regexp.MustCompile(config.AscentConfig.TitleFilter)),
}, logger)
logger.Printf("Searching for %s...\n", config.SearchTerm)
results, err := search.Search(config.SearchTerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Sort products by price for each provider
for i := range results {
slices.SortFunc(results[i].Products, func(a, b web.Product) int {
return int(a.Price - b.Price)
})
}
// Export if requested
if *exportPath != "" {
switch strings.ToLower(*exportFormat) {
case "csv":
if err := ExportCSV(*exportPath, results); err != nil {
logger.Fatalf("Failed to export CSV: %v\n", err)
os.Exit(1)
}
logger.Printf("Exported results to %s (CSV)\n", *exportPath)
case "json":
if err := ExportJSON(*exportPath, results); err != nil {
logger.Fatalf("Failed to export JSON: %v\n", err)
os.Exit(1)
}
logger.Printf("Exported results to %s (JSON)\n", *exportPath)
default:
logger.Fatalf("Unknown export format: %s\n", *exportFormat)
os.Exit(1)
}
return
}
// Print to stdout as before
for _, result := range results {
logger.Printf("Results: Provider: %s\n", result.Provider)
if len(result.Errors) > 0 {
logger.Printf("Errors: \n")
for _, err := range result.Errors {
logger.Printf("\t%s\n", err)
}
}
for _, product := range result.Products {
logger.Printf("%s \t\t%s $%f\n", product.Title, product.Code, product.Price)
}
}
}