impl fast exact parse
parent
12fd38ad2c
commit
1359be1db4
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue