From 1359be1db4d06f146ac706b3c4e6d4b0db053c53 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 27 Jan 2022 15:01:38 -0700 Subject: [PATCH] impl fast exact parse --- broker/fastexact.go | 50 +++++++++++++++++++++++++++++++++++++++- broker/fastexact_test.go | 28 +++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/broker/fastexact.go b/broker/fastexact.go index 8723322..5da2fea 100644 --- a/broker/fastexact.go +++ b/broker/fastexact.go @@ -9,7 +9,11 @@ import ( "local/storage" "local/truckstop/config" "net/http" + "strconv" "strings" + "time" + + "github.com/PuerkitoBio/goquery" ) type FastExact struct { @@ -128,7 +132,51 @@ func (fe FastExact) doRequest(req *http.Request) (*http.Response, error) { } func (fe FastExact) parse(resp *http.Response) ([]Job, error) { - return nil, errors.New("not impl: fe.parse") + doc, err := goquery.NewDocumentFromReader(resp.Body) + if err != nil { + return nil, err + } + result := make([]Job, 0) + doc.Find("#list table tr").Each(func(i int, s *goquery.Selection) { + columns := []string{} + s.Find("td").Each(func(i int, s *goquery.Selection) { + if s.Nodes[0].LastChild != nil && len(s.Nodes[0].LastChild.Attr) > 0 { + attrs := s.Nodes[0].LastChild.Attr + columns = append(columns, attrs[len(attrs)-1].Val) + } else { + columns = append(columns, s.Text()) + } + }) + if len(columns) < 9 { + return + } + job := Job{ + ID: columns[0], + URI: columns[8], + } + job.Pickup.Date, _ = time.ParseInLocation("02-Jan-2006 15:04:05", columns[7], time.Local) + job.Pickup.City = strings.Title(strings.ToLower(strings.Split(columns[1], ",")[0])) + if strings.Contains(columns[1], ",") { + job.Pickup.State = strings.Title(strings.Split(strings.Split(columns[1], ",")[1], " ")[1]) + } + + job.Dropoff.Date = job.Pickup.Date + job.Dropoff.City = strings.Title(strings.ToLower(strings.Split(columns[2], ",")[0])) + if strings.Contains(columns[2], ",") { + job.Dropoff.State = strings.Title(strings.Split(strings.Split(columns[2], ",")[1], " ")[1]) + } + + job.Miles, _ = strconv.Atoi(columns[3]) + if strings.Contains(columns[4], "/") { + weight, _ := strconv.ParseFloat(strings.TrimSpace(strings.Split(columns[4], "/")[1]), 32) + job.Weight = int(weight) + } + + job.Meta = fmt.Sprintf(`dimensions:%s`, strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(columns[5], " ", ""), "\n", ""))) + + result = append(result, job) + }) + return result, nil } type mockFastExactDoer struct{} diff --git a/broker/fastexact_test.go b/broker/fastexact_test.go index d0533c7..0a31168 100644 --- a/broker/fastexact_test.go +++ b/broker/fastexact_test.go @@ -26,7 +26,33 @@ func TestFastExactSearch(t *testing.T) { _ = db if jobs, err := fe.search([]config.State{config.State("NC"), config.State("SC")}); err != nil { t.Fatal(err) - } else { + } else if len(jobs) != 20 { t.Fatal(jobs) + } else { + for _, job := range jobs { + if job.ID == "" { + t.Error(job) + } else if job.Miles == 0 { + t.Error(job) + } else if job.URI == "" { + t.Error(job) + } else if job.Meta == "" { + t.Error(job) + } else if job.Weight == 0 { + t.Error(job) + } else if job.Pickup.Date.IsZero() { + t.Error(job) + } else if job.Pickup.Date != job.Dropoff.Date { + t.Error(job) + } else if job.Dropoff.State == "" { + t.Error(job) + } else if job.Dropoff.City == "" { + t.Error(job) + } else if job.Pickup.State == "" { + t.Error(job) + } else if job.Pickup.City == "" { + t.Error(job) + } + } } }