Skip to content

Commit

Permalink
Add Deutsche post provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Sep 21, 2023
1 parent 10976a7 commit b342382
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions paczkobot/track_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func (t *TrackCommand) Execute(ctx context.Context, args *tghelpers.CommandArgum

detailsString := ""
if rep.data.Destination != "" && rep.data.SentFrom != "" {
detailsString += "The package is headed from " + rep.data.SentFrom + " to " + rep.data.Destination + "."
detailsString += "The package is headed from <u>" + rep.data.SentFrom + "</u> to <u>" + rep.data.Destination + "</u>."
} else if rep.data.Destination != "" {
detailsString += "The package is headed to " + rep.data.Destination + "."
detailsString += "The package is headed to <u>" + rep.data.Destination + "</u>."
}

if rep.data.Weight != 0.0 {
Expand Down
88 changes: 88 additions & 0 deletions providers/deutsche_post/deutsche_post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package deutsche_post

import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"strings"
"time"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/commonerrors"
providerutil "github.com/alufers/paczkobot/provider_util"

"github.com/PuerkitoBio/goquery"
)

var descriptionMappings = map[string]commondata.CommonTrackingStepType{
"Shipment information uploaded to Deutsche Post": commondata.CommonTrackingStepType_INFORMATION_PREPARED,
"Item received at Deutsche Post Mailterminal": commondata.CommonTrackingStepType_SENT,
"Departure to destination country": commondata.CommonTrackingStepType_IN_TRANSIT,
"Delivered": commondata.CommonTrackingStepType_DELIVERED,
}

type DeutschePostProvider struct{}

func (gp *DeutschePostProvider) GetName() string {
return "deutsche-post"
}

func (gp *DeutschePostProvider) MatchesNumber(trackingNumber string) bool {
return true
}

func (gp *DeutschePostProvider) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) {
doc, err := providerutil.FetchGoqueryDocument(
ctx,
gp.GetName(),
"https://www.packet.deutschepost.com/webapp/public/packet_traceit.xhtml?barcode="+url.QueryEscape(trackingNumber),
true,
)
if err != nil {
return nil, fmt.Errorf("failed to read HTML response from Deutsche Post: %w", err)
}
datatables := doc.Find(".gmpacketTraceItHistoryTable table").First()
if datatables.Length() <= 0 {
return nil, commonerrors.NotFoundError
}
td := &commondata.TrackingData{
ShipmentNumber: trackingNumber,
ProviderName: gp.GetName(),
TrackingSteps: []*commondata.TrackingStep{},
}
datatables.Find("tbody tr").Each(func(i int, row *goquery.Selection) {
date := strings.ReplaceAll(row.Find("td:nth-child(1)").Text(), " ", "")

description := row.Find("td:nth-child(2)").Text()

// 02.06.2022 10:39
t, err := time.Parse("02.01.2006", date)
if err != nil {
log.Printf("error while parsing date from Detsche Post: %v", err)
}

td.TrackingSteps = append(td.TrackingSteps, &commondata.TrackingStep{
Datetime: t,

Message: strings.TrimSpace(description),
})
})

td.Destination = doc.Find(".gmpacketTraceItDestinationTo .section>.section").First().Text()
td.SentFrom = doc.Find(".gmpacketTraceItDestinationFrom .section>.section").First().Text()

// replace newlines with spaces
// and replace multiple spaces with single space
re1 := regexp.MustCompile(`[\n\r]+`)
re2 := regexp.MustCompile(`\s+`)
td.Destination = re1.ReplaceAllString(td.Destination, " ")
td.Destination = re2.ReplaceAllString(td.Destination, " ")
td.SentFrom = re1.ReplaceAllString(td.SentFrom, " ")
td.SentFrom = re2.ReplaceAllString(td.SentFrom, " ")

td.ApplyCommonTypeMappings(descriptionMappings)

return td, nil
}
2 changes: 2 additions & 0 deletions providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/providers/deutsche_post"
"github.com/alufers/paczkobot/providers/dhl"
"github.com/alufers/paczkobot/providers/dpdcompl"
"github.com/alufers/paczkobot/providers/fedex_pl"
Expand Down Expand Up @@ -32,6 +33,7 @@ var AllProviders = []Provider{
&fedex_pl.FedexPlProvider{},
&geis_pl.GeisPlProvider{},
&orlen.OrlenProvider{},
&deutsche_post.DeutschePostProvider{},
}

type Provider interface {
Expand Down

0 comments on commit b342382

Please sign in to comment.