Skip to content

Commit

Permalink
Add orlen provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Jun 22, 2023
1 parent 14f1aa6 commit 510f0f2
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
7 changes: 4 additions & 3 deletions paczkobot/bot_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"

"github.com/alufers/paczkobot/httphelpers"
"github.com/alufers/paczkobot/inpostextra"
"github.com/alufers/paczkobot/tghelpers"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand All @@ -15,7 +16,7 @@ type BotApp struct {
DB *gorm.DB
Commands []tghelpers.Command
CommandDispatcher *tghelpers.CommandDispatcher
BaseHTTPClient *http.Client
BaseHTTPClient httphelpers.Client
NotificationsService *NotificationsService
TrackingService *TrackingService
AskService *tghelpers.AskService
Expand All @@ -33,9 +34,9 @@ func NewBotApp(b *tgbotapi.BotAPI, DB *gorm.DB) (a *BotApp) {
Bot: b,
DB: DB,
}
a.BaseHTTPClient = &http.Client{
a.BaseHTTPClient = httphelpers.NewTracingHttpClient(&http.Client{
Timeout: 10,
}
})
a.AskService = tghelpers.NewAskService(a.Bot)
a.CommandDispatcher = tghelpers.NewCommandDispatcher(
b,
Expand Down
21 changes: 21 additions & 0 deletions providers/orlen/json_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package orlen

// OrlenResponse is the root object in the JSONP response from Orlen
type OrlenResponse struct {
Status string `json:"status"`
Number string `json:"number"`
Full bool `json:"full"`
HistoryHTML string `json:"historyHtml"`
History []HistoryEntry `json:"history"`
Label string `json:"label"`
Return bool `json:"return"`
TruckNo string `json:"truckNo"`
ReturnTruck string `json:"returnTruck"`
}

type HistoryEntry struct {
Date string `json:"date"`
Code string `json:"code"`
Label string `json:"label"`
LabelShort string `json:"labelShort"`
}
107 changes: 107 additions & 0 deletions providers/orlen/orlen_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package orlen

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"time"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/commonerrors"
)

// https://www.fedex.com/pl-pl/online/domestic-tracking.html

var commonMappings = map[string]commondata.CommonTrackingStepType{
"Twoja paczka jest w trakcie przygotowania przez nadawcę ": commondata.CommonTrackingStepType_INFORMATION_PREPARED,
"Paczka w trakcie przygotowania": commondata.CommonTrackingStepType_INFORMATION_PREPARED,
"Z przyjemnością informujemy, że Twoja paczka została nadana. ": commondata.CommonTrackingStepType_SENT,
"Paczka nadana": commondata.CommonTrackingStepType_SENT,
"Paczka w sortowni regionalnej": commondata.CommonTrackingStepType_IN_TRANSIT,
}

type OrlenProvider struct{}

func (op *OrlenProvider) GetName() string {
return "orlen"
}

func (op *OrlenProvider) MatchesNumber(trackingNumber string) bool {
return len(trackingNumber) > 2
}

func (op *OrlenProvider) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) {
trackingReq, err := http.NewRequestWithContext(
ctx,
"GET",
"https://nadaj.orlenpaczka.pl/parcel/api-status?id="+
url.QueryEscape(trackingNumber)+
"&jsonp=callback&_="+fmt.Sprintf("%d", time.Now().Unix()),
nil,
)
if err != nil {
return nil, err
}
commondata.SetCommonHTTPHeaders(&trackingReq.Header)

resp, err := http.DefaultClient.Do(trackingReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNoContent || resp.StatusCode == http.StatusNotFound {
return nil, commonerrors.NotFoundError
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("orlen: unexpected status code: %d", resp.StatusCode)
}

allData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// strip the callback() from the response
reg := regexp.MustCompile(`^callback[(]|[)];$`)
strData := reg.ReplaceAllString(string(allData), "")

respData := &OrlenResponse{}
err = json.Unmarshal([]byte(strData), respData)
if err != nil {
return nil, err
}

if respData.Status != "OK" {
return nil, commonerrors.NotFoundError
}

td := &commondata.TrackingData{
ShipmentNumber: trackingNumber,
ProviderName: op.GetName(),
TrackingSteps: []*commondata.TrackingStep{},
}

for _, ev := range respData.History {
// example date: 20-06-2023, 21:23

datetime, _ := time.Parse("02-01-2006, 15:04", ev.Date)
commonStepType, ok := commonMappings[ev.Label]
if !ok {
commonStepType, ok = commonMappings[ev.LabelShort]
if !ok {
commonStepType = commondata.CommonTrackingStepType_UNKNOWN
}
}
td.TrackingSteps = append(td.TrackingSteps, &commondata.TrackingStep{
Datetime: datetime,
Message: ev.Label,
CommonType: commonStepType,
})
}

return td, nil
}
2 changes: 2 additions & 0 deletions providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/alufers/paczkobot/providers/geis_pl"
"github.com/alufers/paczkobot/providers/gls"
"github.com/alufers/paczkobot/providers/inpost"
"github.com/alufers/paczkobot/providers/orlen"
"github.com/alufers/paczkobot/providers/packeta"
"github.com/alufers/paczkobot/providers/pocztapolska"
"github.com/alufers/paczkobot/providers/postnl"
Expand All @@ -30,6 +31,7 @@ var AllProviders = []Provider{
&packeta.PacketaProvider{},
&fedex_pl.FedexPlProvider{},
&geis_pl.GeisPlProvider{},
&orlen.OrlenProvider{},
}

type Provider interface {
Expand Down

0 comments on commit 510f0f2

Please sign in to comment.