2024-10-25 20:33:13 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-26 21:13:24 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
|
2024-10-25 20:33:13 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
2024-10-26 21:13:24 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"slices"
|
2024-10-25 20:33:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2024-10-25 20:46:22 +00:00
|
|
|
UserFavoriteCount int64
|
|
|
|
UserName string
|
|
|
|
UserID string
|
2024-10-25 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Favorites struct {
|
2024-10-25 20:46:22 +00:00
|
|
|
Posts []models.Post
|
|
|
|
NextPage string
|
|
|
|
LastPage string
|
2024-10-25 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Plug interface {
|
|
|
|
// GetFavoritePage
|
|
|
|
// The API Key can be an empty string if it's not supplied by the user, in this case the default API Key should be used
|
|
|
|
GetFavoritePage(ctx context.Context, apiKey string, userSource models.UserSource, pageIdentifier string) (Favorites, error)
|
|
|
|
|
|
|
|
// GetUserProfile
|
|
|
|
// The API Key can be an empty string if it's not supplied by the user, in this case the default API Key should be used
|
|
|
|
GetUserProfile(ctx context.Context, apiKey string, userSource models.UserSource) (User, error)
|
|
|
|
}
|
|
|
|
|
2024-10-26 18:30:12 +00:00
|
|
|
func algorithm(ctx context.Context, plugInterface Plug, userSource models.UserSource, anthroveUserFavCount int64, deepScrape bool, apiKey string) (TaskSummery, error) {
|
2024-10-25 20:33:13 +00:00
|
|
|
ctx, span := tracer.Start(ctx, "mainScrapeAlgorithm")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("user_source_id", string(userSource.ID)),
|
|
|
|
attribute.String("user_source_user_id", string(userSource.UserID)),
|
|
|
|
attribute.String("user_source_source_id", string(userSource.SourceID)),
|
|
|
|
)
|
|
|
|
|
|
|
|
basicLoggingInfo := log.Fields{
|
|
|
|
"user_source_id": userSource.ID,
|
|
|
|
"user_source_user_id": userSource.UserID,
|
|
|
|
"user_source_source_id": userSource.SourceID,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).Info("Starting mainScrapeAlgorithm")
|
|
|
|
|
|
|
|
taskSummery := TaskSummery{
|
|
|
|
AddedPosts: 0,
|
|
|
|
DeletedPosts: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
profile, err := plugInterface.GetUserProfile(ctx, apiKey, userSource)
|
|
|
|
if err != nil {
|
|
|
|
return taskSummery, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage := ""
|
2024-10-26 21:13:24 +00:00
|
|
|
var newPosts []models.Post
|
|
|
|
var anthroveFaves []models.UserFavorite
|
2024-10-25 20:33:13 +00:00
|
|
|
|
|
|
|
outer:
|
2024-10-26 21:13:24 +00:00
|
|
|
for {
|
2024-11-01 20:06:03 +00:00
|
|
|
if anthroveUserFavCount < profile.UserFavoriteCount && profile.UserFavoriteCount > 0 {
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break outer
|
|
|
|
default:
|
|
|
|
span.AddEvent("Executing getFavorites request")
|
|
|
|
favorites, err := plugInterface.GetFavoritePage(ctx, apiKey, userSource, nextPage)
|
|
|
|
span.AddEvent("Finished executing getFavorites request")
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).WithError(err).Error("Failed to execute favorites page")
|
|
|
|
return taskSummery, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(favorites.Posts) == 0 {
|
|
|
|
span.AddEvent("No more favorites found")
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).Info("No more favorites found")
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
span.AddEvent("Executing BatchPostProcessingWithSummery")
|
|
|
|
pageNewPosts, pageAnthroveFaves, err := BatchPostProcessingWithSummery(ctx, userSource, favorites.Posts)
|
|
|
|
|
|
|
|
anthroveFaves = append(anthroveFaves, pageAnthroveFaves...)
|
|
|
|
newPosts = append(newPosts, pageNewPosts...)
|
|
|
|
|
|
|
|
span.AddEvent("Finished executing BatchPostProcessingWithSummery")
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).WithError(err).Error("Failed in BatchPostProcessing")
|
|
|
|
return taskSummery, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(anthroveFaves) <= 0 || len(favorites.Posts) != len(anthroveFaves) {
|
|
|
|
span.AddEvent("No more favorites found to add")
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).Info("No more favorites found")
|
2024-10-25 20:33:13 +00:00
|
|
|
break outer
|
|
|
|
}
|
2024-11-01 20:06:03 +00:00
|
|
|
|
|
|
|
nextPage = favorites.NextPage
|
|
|
|
taskSummery.AddedPosts += len(pageAnthroveFaves)
|
2024-10-26 21:13:24 +00:00
|
|
|
}
|
|
|
|
break outer
|
|
|
|
}
|
2024-10-25 20:33:13 +00:00
|
|
|
|
2024-10-26 21:13:24 +00:00
|
|
|
if len(newPosts) > 0 {
|
2024-10-28 20:08:54 +00:00
|
|
|
span.AddEvent("Executing CreatePostInBatch")
|
2024-10-26 21:13:24 +00:00
|
|
|
err = database.CreatePostInBatch(ctx, newPosts, BatchSize)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithError(err).Error("Failed to create new posts in batch")
|
|
|
|
return taskSummery, err
|
|
|
|
}
|
|
|
|
span.AddEvent("Created new posts in batch", trace.WithAttributes(attribute.Int("batch_size", BatchSize)))
|
|
|
|
log.WithContext(ctx).WithFields(BasicLoggingFields).Info("Created new posts in batch")
|
|
|
|
}
|
2024-10-25 20:33:13 +00:00
|
|
|
|
2024-10-26 21:13:24 +00:00
|
|
|
if len(anthroveFaves) > 0 {
|
2024-10-28 20:08:54 +00:00
|
|
|
span.AddEvent("Executing CreateUserFavoriteInBatch")
|
|
|
|
|
2024-10-26 21:13:24 +00:00
|
|
|
slices.Reverse(anthroveFaves)
|
|
|
|
err = database.CreateUserFavoriteInBatch(ctx, anthroveFaves, BatchSize)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithError(err).WithFields(BasicLoggingFields).Error("Failed to create user favorites in batch")
|
|
|
|
return taskSummery, err
|
2024-10-25 20:33:13 +00:00
|
|
|
}
|
2024-10-26 21:13:24 +00:00
|
|
|
span.AddEvent("Created user favorites in batch", trace.WithAttributes(attribute.Int("batch_size", BatchSize)))
|
|
|
|
log.WithContext(ctx).WithFields(BasicLoggingFields).Info("Created user favorites in batch")
|
2024-10-25 20:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
span.AddEvent("Completed scraping algorithm")
|
|
|
|
log.WithContext(ctx).WithFields(basicLoggingInfo).Info("Completed scraping algorithm")
|
|
|
|
return taskSummery, nil
|
|
|
|
}
|