70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package scraper
|
|
|
|
import (
|
|
"encoding/json"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"production/internal/pcbuilder"
|
|
)
|
|
|
|
func specToJSON(spec pcbuilder.Spec) ([]byte, error) {
|
|
return json.Marshal(spec)
|
|
}
|
|
|
|
var (
|
|
socketRe = regexp.MustCompile(`(?i)\b(AM4|AM5|LGA\s?1700|LGA\s?1200|LGA\s?1851|LGA\s?2011(?:-3)?)\b`)
|
|
ramTypeRe = regexp.MustCompile(`(?i)\bDDR([345])\b`)
|
|
tdpRe = regexp.MustCompile(`(?i)TDP[^\d]{0,10}(\d+)\s*Вт`)
|
|
wattageRe = regexp.MustCompile(`(?i)\b(\d{3,4})\s*Вт\b`)
|
|
formFactRe = regexp.MustCompile(`(?i)\b(E-?ATX|Micro-?ATX|mATX|Mini-?ITX|ITX|ATX)\b`)
|
|
)
|
|
|
|
// extractSocket, extractRAMType etc. are best-effort regex reads over a
|
|
// listing's free-text title/description — see Item.Spec's doc comment for
|
|
// why sparse results are fine here (pcbuilder.Check skips checks on empty
|
|
// fields rather than erroring).
|
|
func extractSocket(text string) string {
|
|
m := socketRe.FindString(text)
|
|
return strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(m), " ", ""))
|
|
}
|
|
|
|
func extractRAMType(text string) string {
|
|
m := ramTypeRe.FindStringSubmatch(text)
|
|
if m == nil {
|
|
return ""
|
|
}
|
|
return "DDR" + m[1]
|
|
}
|
|
|
|
func extractTDPWatts(text string) int {
|
|
m := tdpRe.FindStringSubmatch(text)
|
|
if m == nil {
|
|
return 0
|
|
}
|
|
n, _ := strconv.Atoi(m[1])
|
|
return n
|
|
}
|
|
|
|
// extractWattage is for PSUs — "750 Вт" without a "TDP" prefix, so it needs
|
|
// its own looser pattern (and picks the largest match, since PSU titles
|
|
// often also mention unrelated numbers like model/certification wattages
|
|
// in efficiency badges further down the text).
|
|
func extractWattage(text string) int {
|
|
matches := wattageRe.FindAllStringSubmatch(text, -1)
|
|
best := 0
|
|
for _, m := range matches {
|
|
n, _ := strconv.Atoi(m[1])
|
|
if n > best {
|
|
best = n
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
func extractFormFactor(text string) string {
|
|
m := formFactRe.FindString(text)
|
|
return strings.ToUpper(strings.ReplaceAll(m, "-", ""))
|
|
}
|