task-processor/internal/parsers/surugaya.go

69 lines
1.4 KiB
Go
Raw Normal View History

2025-10-02 20:35:53 +03:00
package parsers
import (
2025-10-03 19:17:01 +03:00
"context"
2025-10-02 20:35:53 +03:00
log "github.com/sirupsen/logrus"
2025-10-03 19:17:01 +03:00
"io"
"task-processor/internal/appState"
"task-processor/internal/shared"
sc "task-processor/proto/surugayaScrapper"
2025-10-02 20:35:53 +03:00
)
2025-10-03 19:17:01 +03:00
type SurugayaParser struct {
scrapper sc.SurugayaScrapperClient
}
2025-10-02 20:35:53 +03:00
2026-02-28 10:53:02 +03:00
func NewSurugayaParser(scrapper sc.SurugayaScrapperClient) *SurugayaParser {
2025-10-03 19:17:01 +03:00
log.Debug("Surugaya parser init")
return &SurugayaParser{
scrapper: scrapper,
}
2025-10-02 20:35:53 +03:00
}
2026-02-28 10:53:02 +03:00
func (s *SurugayaParser) HandleTasks(ctx context.Context, tasks []shared.Task, sender chan shared.TaskResult, state *appState.State) {
2025-10-06 23:13:32 +03:00
log.WithField("count", len(tasks)).Debug("Handling Surugaya Tasks")
2025-10-03 19:17:01 +03:00
2026-02-28 10:53:02 +03:00
stream, err := s.scrapper.ProcessTasks(ctx)
2025-10-03 19:17:01 +03:00
if err != nil {
log.WithField("err", err).Error("Error creating stream")
return
}
2025-10-06 23:13:32 +03:00
for _, t := range tasks {
2025-10-03 19:17:01 +03:00
if err = stream.Send(&sc.Task{
MerchUuid: t.MerchUuid,
Link: t.Link,
}); err != nil {
log.WithField("err", err).Error("Error sending task")
return
}
}
if err = stream.CloseSend(); err != nil {
log.WithError(err).Warn("Failed to close send stream")
}
counter := 0
for {
result, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.WithError(err).Error("Error receiving result")
return
}
sender <- shared.TaskResult{
MerchUuid: result.GetMerchUuid(),
Origin: shared.OriginSurugaya,
Price: result.GetPrice(),
}
counter++
}
log.WithField("count", counter).Debug("All Surugaya results received")
2025-10-02 20:35:53 +03:00
}