2023-07-17 10:57:23 +00:00
|
|
|
package service
|
2023-05-24 14:05:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-08 13:01:27 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-07-17 10:57:23 +00:00
|
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/logic"
|
|
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/util"
|
2023-06-21 11:29:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-05-24 14:05:27 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func ScrapeUser(ctx context.Context, graphConnection logic.GraphConnection, client *e621.Client, username string) error {
|
2023-05-24 14:05:27 +00:00
|
|
|
var err error
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
e621User, err := client.GetUserByName(username).Execute()
|
2023-05-24 14:05:27 +00:00
|
|
|
if err != nil {
|
2023-07-17 08:10:13 +00:00
|
|
|
log.Info(err)
|
2023-05-24 14:05:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
if e621User.IsBanned {
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
"e621_user_bann": e621User.IsBanned,
|
|
|
|
}).Info("service: user is Banned")
|
2023-05-24 21:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
}).Info("service: processing user")
|
2023-05-24 21:11:49 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = graphConnection.UploadUser(ctx, e621User)
|
2023-05-24 14:05:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
}).Info("service: start processing favorites")
|
2023-05-24 21:11:49 +00:00
|
|
|
start := time.Now()
|
2023-07-17 08:10:13 +00:00
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
e621FavoritesBuilder := client.GetFavoritesBuilder().SetUserID(e621User.ID)
|
|
|
|
e621Favorites, err := client.GetAllFavoritesForUser(e621FavoritesBuilder)
|
2023-07-17 08:10:13 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// Uploads all Tags, Posts as Nodes to Neo4j
|
2023-11-08 13:01:27 +00:00
|
|
|
for i, post := range e621Favorites {
|
2023-06-20 08:38:36 +00:00
|
|
|
if exists, err := graphConnection.CheckUserToPostLink(ctx, post.ID, e621User.ID); err == nil && exists {
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
"last_post_id": post.ID,
|
|
|
|
}).Info("service: no new favorites found")
|
2023-06-17 17:13:25 +00:00
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
start = time.Now()
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadNodes(ctx, graphConnection, post)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
"post_number": i,
|
2023-11-08 13:01:27 +00:00
|
|
|
"post_amount": len(e621Favorites),
|
2023-07-26 13:27:18 +00:00
|
|
|
"post_id": post.ID,
|
|
|
|
"upload_time": time.Since(start),
|
|
|
|
}).Debug("service: uploading post")
|
2023-05-24 21:11:49 +00:00
|
|
|
|
2023-05-24 14:05:27 +00:00
|
|
|
start := time.Now()
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadPostToUserRelationship(ctx, graphConnection, post, e621User)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadSourceTagRelationship(ctx, graphConnection, post)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadGeneralTagRelationship(ctx, graphConnection, post)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadCharacterTagtRelationship(ctx, graphConnection, post)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadCopyrightTagRelationship(ctx, graphConnection, post)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err = uploadArtistTagRelationship(ctx, graphConnection, post)
|
2023-05-24 14:05:27 +00:00
|
|
|
if err != nil {
|
2023-05-24 21:11:49 +00:00
|
|
|
log.Fatal(err)
|
2023-05-24 14:05:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
|
|
|
"post_number": i,
|
2023-11-08 13:01:27 +00:00
|
|
|
"post_amount": len(e621Favorites),
|
2023-07-26 13:27:18 +00:00
|
|
|
"post_id": post.ID,
|
|
|
|
"upload_time": time.Since(start),
|
|
|
|
}).Debug("service: making relationship")
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
2023-06-21 11:29:23 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-07-26 13:27:18 +00:00
|
|
|
"e621_username": e621User.Name,
|
|
|
|
"e621_user_id": e621User.ID,
|
2023-11-08 13:01:27 +00:00
|
|
|
"post_amount": len(e621Favorites),
|
2023-07-26 13:27:18 +00:00
|
|
|
"scrape_time": time.Since(start),
|
|
|
|
}).Info("service: finished processing favorites")
|
2023-06-17 17:13:25 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadNodes uploads the post to the database and creates the nodes
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadNodes(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-06-17 17:13:25 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
uniqueGeneralTags := make([]string, 0)
|
|
|
|
uniqueCharacterTags := make([]string, 0)
|
|
|
|
uniqueCopyrightTags := make([]string, 0)
|
|
|
|
uniqueArtistTags := make([]string, 0)
|
|
|
|
|
|
|
|
allGeneralTags := make([]string, 0)
|
|
|
|
allCharacterTags := make([]string, 0)
|
|
|
|
allCopyrightTags := make([]string, 0)
|
|
|
|
allArtistTags := make([]string, 0)
|
|
|
|
|
|
|
|
allGeneralTags = append(allGeneralTags, post.Tags.General...)
|
|
|
|
allCharacterTags = append(allCharacterTags, post.Tags.Character...)
|
2023-06-22 07:04:52 +00:00
|
|
|
allCopyrightTags = append(allCopyrightTags, post.Tags.Copyright...)
|
2023-05-24 21:11:49 +00:00
|
|
|
allArtistTags = append(allArtistTags, post.Tags.Artist...)
|
|
|
|
|
2023-07-17 10:57:23 +00:00
|
|
|
uniqueGeneralTags = util.UniqueNonEmptyElementsOf(allGeneralTags)
|
|
|
|
uniqueCharacterTags = util.UniqueNonEmptyElementsOf(allCharacterTags)
|
|
|
|
uniqueCopyrightTags = util.UniqueNonEmptyElementsOf(allCopyrightTags)
|
|
|
|
uniqueArtistTags = util.UniqueNonEmptyElementsOf(allArtistTags)
|
2023-05-24 21:11:49 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadPost(ctx, post.ID)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uploads the source to the database
|
|
|
|
for _, source := range post.Sources {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadSource(ctx, source)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, uniqueGeneralTag := range uniqueGeneralTags {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadTag(ctx, uniqueGeneralTag, "general")
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
|
|
|
for _, uniqueCharacterTag := range uniqueCharacterTags {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadTag(ctx, uniqueCharacterTag, "character")
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, uniqueCopyrightTag := range uniqueCopyrightTags {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadTag(ctx, uniqueCopyrightTag, "copyright")
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, uniqueArtistTag := range uniqueArtistTags {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.UploadTag(ctx, uniqueArtistTag, "artist")
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-17 17:13:25 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadPostToUserRelationship creates a relationship between the user and the post
|
2023-11-08 13:01:27 +00:00
|
|
|
func uploadPostToUserRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post, e621User model.User) error {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishUserToPostLink(ctx, post.ID, e621User.ID)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// log.Printf("Created UserToPostRelationship for user: %s to post: %d", e621User.Name, post.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadSourceTagRelationship creates a relationship between the post and the source
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadSourceTagRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, source := range post.Sources {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishPostToSourceLink(ctx, post.ID, source)
|
2023-05-24 14:05:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
// log.Printf("Created PostToSourceRelationship for Post: %d to source: %s", post.ID, source)
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadGeneralTagRelationship creates a relationship between the post and the general tag
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadGeneralTagRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, generalTag := range post.Tags.General {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishPostToTagLink(ctx, post.ID, generalTag)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
// log.Printf("Created PostToTagRelationship for post: %d to general tag: %s", post.ID, generalTag)
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadCharacterTagtRelationship creates a relationship between the post and the character tag
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadCharacterTagtRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, characterTag := range post.Tags.Character {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishPostToTagLink(ctx, post.ID, characterTag)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
// log.Printf("Created PostToTagRelationship for post: %d to character tag: %s", post.ID, characterTag)
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
// uploadCopyrightTagRelationship creates a relationship between the post and the copyright tag
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadCopyrightTagRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, copyrightTag := range post.Tags.Copyright {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishPostToTagLink(ctx, post.ID, copyrightTag)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-24 14:05:27 +00:00
|
|
|
}
|
2023-07-17 08:10:13 +00:00
|
|
|
// log.Printf("Created PostToTagRelationship for post: %d to copyright tag: %s", post.ID, copyrightTag)
|
2023-05-24 14:05:27 +00:00
|
|
|
|
|
|
|
}
|
2023-05-24 21:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// uploadArtistTagRelationship creates a relationship between the post and the artist tag
|
2023-07-17 10:57:23 +00:00
|
|
|
func uploadArtistTagRelationship(ctx context.Context, graphConnection logic.GraphConnection, post model.Post) error {
|
2023-05-24 21:11:49 +00:00
|
|
|
for _, artistTag := range post.Tags.Artist {
|
2023-06-20 08:38:36 +00:00
|
|
|
err := graphConnection.EstablishPostToTagLink(ctx, post.ID, artistTag)
|
2023-05-24 21:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// log.Printf("Created PostToTagRelationship for post: %d to artist tag: %s", post.ID, artistTag)
|
2023-05-24 14:05:27 +00:00
|
|
|
|
2023-05-24 21:11:49 +00:00
|
|
|
}
|
2023-05-24 14:05:27 +00:00
|
|
|
return nil
|
|
|
|
}
|