This repository has been archived on 2021-09-02. You can view files and clone it, but cannot push or open issues or pull requests.
my-bloody-hetzner-sb-notifier/crawler/crawler.go
Mahmoud Rahbar Azad 66001f861f
Initial commit.
Example output:
Got 580 offers. Filtered offers: 3
           ID|     Ram|             HDD|                           CPU|    Price|   Score|  Reduce time|Specials
  SB57-931121|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V2 (12518)|  57.00 €|  103.12|      53h 53m|ECC, Ent. HDD, iNIC
  SB69-927780|  128 GB|  2x 2 TB (4096)|  Intel Xeon E5-1650V3 (13335)|  69.00 €|   89.92|      49h 39m|ECC, Ent. HDD, iNIC
  SB76-910394|  128 GB|  3x 2 TB (6144)|  Intel Xeon E5-1650V2 (12518)|  76.00 €|   82.73|      01h 21m|ECC, Ent. HDD, iNIC
2018-10-22 23:55:29 +02:00

84 lines
1.8 KiB
Go

package crawler
import (
"fmt"
"github.com/mrahbar/my-bloody-hetzner-sb-notifier/hetzner"
"os"
"sort"
"text/tabwriter"
)
type Crawler struct {
tabWriter *tabwriter.Writer
minPrice float64
maxPrice float64
minRam int
maxRam int
minHddSize int
maxHddSize int
minHddCount int
maxHddCount int
minBenchmark int
maxBenchmark int
}
func NewCrawler(minPrice float64, maxPrice float64, minRam int, maxRam int, minHddSize int, maxHddSize int, minHddCount int, maxHddCount int, minBenchmark int, maxBenchmark int) *Crawler {
crawler := &Crawler{
tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', tabwriter.Debug|tabwriter.AlignRight),
minPrice,
maxPrice,
minRam,
maxRam,
minHddSize,
maxHddSize,
minHddCount,
maxHddCount,
minBenchmark,
maxBenchmark,
}
return crawler
}
func (c *Crawler) Filter(servers []hetzner.Server) []hetzner.Server {
var filteredServers []hetzner.Server
for _, server := range servers {
if !c.isFiltered(server) {
filteredServers = append(filteredServers, server)
}
}
sort.Slice(servers, func(i, j int) bool {
return servers[i].Score() > servers[j].Score()
})
return filteredServers
}
func (c *Crawler) Print(servers []hetzner.Server) {
fmt.Fprintf(c.tabWriter, "%s\n", servers[0].Header())
for _, server := range servers {
fmt.Fprintf(c.tabWriter, "%s\n", server.ToString())
}
c.tabWriter.Flush()
}
func (c *Crawler) isFiltered(server hetzner.Server) bool {
filtered := true
priceParsed := server.ParsePrice()
if server.Cpu_benchmark >= c.minBenchmark && server.Cpu_benchmark <= c.maxBenchmark &&
priceParsed >= c.minPrice && priceParsed <= c.maxPrice &&
server.Ram >= c.minRam && server.Ram <= c.maxRam &&
server.TotalHdd() >= c.minHddSize && server.TotalHdd() <= c.maxHddSize &&
server.Hdd_count >= c.minHddCount && server.Hdd_count <= c.maxHddCount {
filtered = false
}
return filtered
}