-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
187 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cainiao | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/alufers/paczkobot/commondata" | ||
"github.com/alufers/paczkobot/commonerrors" | ||
) | ||
|
||
type CainiaoProvider struct{} | ||
|
||
func (pp *CainiaoProvider) GetName() string { | ||
return "cainiao" | ||
} | ||
|
||
func (pp *CainiaoProvider) MatchesNumber(trackingNumber string) bool { | ||
return true | ||
} | ||
|
||
func (pp *CainiaoProvider) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) { | ||
|
||
req, err := http.NewRequest( | ||
"GET", | ||
"https://global.cainiao.com/global/detail.json?mailNos="+url.QueryEscape(trackingNumber)+"&lang=en-US&language=en-US", | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create GET request: %w", err) | ||
} | ||
commondata.SetCommonHTTPHeaders(&req.Header) | ||
httpResponse, err := http.DefaultClient.Do(req) | ||
Check failure on line 36 in providers/cainiao/cainiao_provider.go GitHub Actions / build-and-push-image
|
||
if err != nil { | ||
return nil, commonerrors.NewNetworkError(pp.GetName(), req) | ||
} | ||
|
||
if httpResponse.StatusCode != 200 { | ||
return nil, commonerrors.NotFoundError | ||
} | ||
|
||
var cainiaoResponse CainiaoResponse | ||
err = json.NewDecoder(httpResponse.Body).Decode(&cainiaoResponse) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
if len(cainiaoResponse.Module) == 0 { | ||
return nil, commonerrors.NotFoundError | ||
} | ||
module := cainiaoResponse.Module[0] | ||
if len(module.DetailList) == 0 { | ||
return nil, commonerrors.NotFoundError | ||
} | ||
|
||
td := &commondata.TrackingData{ | ||
ProviderName: pp.GetName(), | ||
ShipmentNumber: trackingNumber, | ||
TrackingSteps: make([]*commondata.TrackingStep, 0), | ||
Destination: module.DestCountry, | ||
SentFrom: module.OriginCountry, | ||
} | ||
|
||
for _, detail := range module.DetailList { | ||
|
||
date, _ := time.Parse("2006-01-02 15:04:05", detail.TimeStr) | ||
step := &commondata.TrackingStep{ | ||
Datetime: date, | ||
Message: detail.Desc, | ||
Location: detail.Group.NodeDesc, | ||
CommonType: commondata.CommonTrackingStepType_UNKNOWN, | ||
} | ||
td.TrackingSteps = append(td.TrackingSteps, step) | ||
} | ||
|
||
// attempt to augement the destination with the city | ||
// requires a separate request, if it fails, it's not a big deal | ||
cityReq, err := http.NewRequest( | ||
"GET", | ||
"https://global.cainiao.com/global/getCity.json?mailNo="+url.QueryEscape(trackingNumber)+"&lang=en-US&language=en-US", | ||
nil, | ||
) | ||
if err != nil { | ||
return td, nil | ||
Check failure on line 86 in providers/cainiao/cainiao_provider.go GitHub Actions / build-and-push-image
|
||
} | ||
commondata.SetCommonHTTPHeaders(&cityReq.Header) | ||
cityResp, err := http.DefaultClient.Do(cityReq) | ||
Check failure on line 89 in providers/cainiao/cainiao_provider.go GitHub Actions / build-and-push-image
|
||
if err != nil { | ||
return td, nil | ||
Check failure on line 91 in providers/cainiao/cainiao_provider.go GitHub Actions / build-and-push-image
|
||
} | ||
if cityResp.StatusCode != 200 { | ||
return td, nil | ||
} | ||
var cityResponse GetCityResponse | ||
err = json.NewDecoder(cityResp.Body).Decode(&cityResponse) | ||
if err != nil || !cityResponse.Success { | ||
return td, nil | ||
Check failure on line 99 in providers/cainiao/cainiao_provider.go GitHub Actions / build-and-push-image
|
||
} | ||
|
||
td.Destination = cityResponse.Module + ", " + td.Destination | ||
|
||
return td, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cainiao | ||
|
||
type CainiaoResponse struct { | ||
Module []Module `json:"module"` | ||
Success bool `json:"success"` | ||
} | ||
|
||
type GetCityResponse struct { | ||
Module string `json:"module"` | ||
Success bool `json:"success"` | ||
} | ||
|
||
type ProgressPointList struct { | ||
PointName string `json:"pointName"` | ||
Light bool `json:"light,omitempty"` | ||
Reload bool `json:"reload,omitempty"` | ||
} | ||
type ProcessInfo struct { | ||
ProgressStatus string `json:"progressStatus"` | ||
ProgressRate float64 `json:"progressRate"` | ||
Type string `json:"type"` | ||
ProgressPointList []ProgressPointList `json:"progressPointList"` | ||
} | ||
type GlobalEtaInfo struct { | ||
EtaDesc string `json:"etaDesc"` | ||
DeliveryMinTime int64 `json:"deliveryMinTime"` | ||
DeliveryMaxTime int64 `json:"deliveryMaxTime"` | ||
} | ||
type Group struct { | ||
NodeCode string `json:"nodeCode"` | ||
NodeDesc string `json:"nodeDesc"` | ||
CurrentIconURL string `json:"currentIconUrl"` | ||
HistoryIconURL string `json:"historyIconUrl"` | ||
} | ||
type GlobalCombinedLogisticsTraceDTO struct { | ||
Time int64 `json:"time"` | ||
TimeStr string `json:"timeStr"` | ||
Desc string `json:"desc"` | ||
StanderdDesc string `json:"standerdDesc"` | ||
DescTitle string `json:"descTitle"` | ||
TimeZone string `json:"timeZone"` | ||
ActionCode string `json:"actionCode"` | ||
Group Group `json:"group"` | ||
} | ||
type LatestTrace struct { | ||
Time int64 `json:"time"` | ||
TimeStr string `json:"timeStr"` | ||
Desc string `json:"desc"` | ||
StanderdDesc string `json:"standerdDesc"` | ||
DescTitle string `json:"descTitle"` | ||
TimeZone string `json:"timeZone"` | ||
ActionCode string `json:"actionCode"` | ||
Group Group `json:"group"` | ||
} | ||
type DetailListItem struct { | ||
Time int64 `json:"time"` | ||
TimeStr string `json:"timeStr"` | ||
Desc string `json:"desc"` | ||
StanderdDesc string `json:"standerdDesc"` | ||
DescTitle string `json:"descTitle"` | ||
TimeZone string `json:"timeZone"` | ||
ActionCode string `json:"actionCode"` | ||
Group Group `json:"group,omitempty"` | ||
} | ||
type Module struct { | ||
MailNo string `json:"mailNo"` | ||
OriginCountry string `json:"originCountry"` | ||
DestCountry string `json:"destCountry"` | ||
MailType string `json:"mailType"` | ||
MailTypeDesc string `json:"mailTypeDesc"` | ||
Status string `json:"status"` | ||
StatusDesc string `json:"statusDesc"` | ||
MailNoSource string `json:"mailNoSource"` | ||
ProcessInfo ProcessInfo `json:"processInfo"` | ||
GlobalEtaInfo GlobalEtaInfo `json:"globalEtaInfo"` | ||
GlobalCombinedLogisticsTraceDTO GlobalCombinedLogisticsTraceDTO `json:"globalCombinedLogisticsTraceDTO"` | ||
LatestTrace LatestTrace `json:"latestTrace"` | ||
DetailList []DetailListItem `json:"detailList"` | ||
DaysNumber string `json:"daysNumber"` | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters