diff --git a/broker/ntgvision.go b/broker/ntgvision.go index 4f0fdd5..edca617 100644 --- a/broker/ntgvision.go +++ b/broker/ntgvision.go @@ -1,11 +1,14 @@ package broker import ( + "bytes" "encoding/json" - "errors" "fmt" "io" + "io/ioutil" "local/truckstop/config" + "net/http" + "strings" "time" ) @@ -61,10 +64,6 @@ func (ntg NTGVision) WithMock() NTGVision { return ntg } -func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) { - return nil, errors.New("not impl: ntg.search") -} - func (ntg NTGVision) Search(states []config.State) ([]Job, error) { rc, err := ntg.searcher.search(states) if err != nil { @@ -81,3 +80,64 @@ func (ntg NTGVision) Search(states []config.State) ([]Job, error) { } return jobs, err } + +func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) { + request, err := ntg.newRequest(states) + if err != nil { + return nil, err + } + c := http.Client{Timeout: time.Minute} + resp, err := c.Do(request) + if err != nil { + return nil, err + } + if resp.StatusCode != http.StatusOK { + b, _ := ioutil.ReadAll(resp.Body) + resp.Body.Close() + return nil, fmt.Errorf("bad status searching ntg: %d: %s", resp.StatusCode, b) + } + return resp.Body, nil +} + +func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) { + body, err := json.Marshal(map[string]interface{}{ + "OriginFromDate": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"), + "OriginToDate": time.Now().UTC().Add(time.Hour * 24 * 30).Format("2006-01-02T15:04:05.000Z"), + "DestinationFromDate": nil, + "DestinationToDate": nil, + "OriginState": states, + "OriginRadius": 100, + "DestinationState": []string{}, + "DestinationRadius": 100, + "EquipmentTypes": []string{"STRAIGHT TRUCK", "str truck w/ lift gate"}, + "MaxMilesLimit": nil, + "MaxNumberOfStopsLimit": nil, + "MaxWeightLimit": nil, + "MinMilesLimit": nil, + "SavedSearchId": nil, ///2956, + }) + if err != nil { + return nil, err + } + request, err := http.NewRequest(http.MethodPost, "https://ntgvision.com/api/v1/load/LoadBoardSearchResults", bytes.NewReader(body)) + if err != nil { + return nil, err + } + request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0") + request.Header.Set("Accept", "application/json, text/plain, */*") + request.Header.Set("Accept-Language", "en-US,en;q=0.5") + request.Header.Set("Accept-Encoding", "gzip, deflate, br") + request.Header.Set("Content-Type", "application/json;charset=utf-8") + request.Header.Set("Authorization", strings.Split(strings.Split(config.Get().Brokers.NTG.Cookie, "; NTGAuthToken=")[1], "; ")[0]) + request.Header.Set("Origin", "https://ntgvision.com") + request.Header.Set("DNT", "1") + request.Header.Set("Connection", "keep-alive") + request.Header.Set("Cookie", config.Get().Brokers.NTG.Cookie) + request.Header.Set("Sec-Fetch-Dest", "empty") + request.Header.Set("Sec-Fetch-Mode", "cors") + request.Header.Set("Sec-Fetch-Site", "same-origin") + request.Header.Set("Pragma", "no-cache") + request.Header.Set("Cache-Control", "no-cache") + + return request, nil +} diff --git a/config/config.go b/config/config.go index 8c621a1..07e0e90 100644 --- a/config/config.go +++ b/config/config.go @@ -13,6 +13,11 @@ type Config struct { States []State Storage []string Once bool + Brokers struct { + NTG struct { + Cookie string + } + } lock sync.Mutex db storage.DB