From ffde450209295448db5743289a7f63b571a762ea Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Tue, 28 May 2024 17:45:47 -0600 Subject: [PATCH] ready to roll --- services/migration/migrate-posts.go | 42 ++++++++++++------- .../slowtwitch/get-images-and-post-html.go | 5 +++ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index c042c98..ec51668 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -40,12 +40,13 @@ func (migration MigratePosts) Execute() []PostResult { batchSize := 5 var postBatch []int - for _, postId := range slowtwitchPostIdsForMigration { + for i, postId := range slowtwitchPostIdsForMigration { //anonymous func will take in post IDs: the rest it can access from scope - postBatch = append(postBatch, postId) - if len(postBatch) == batchSize { + + if len(postBatch) == batchSize || i == len(slowtwitchPostIdsForMigration)-1 { postResultsChannel := make(chan PostResult) + for _, batchId := range postBatch { go func(id int) { errorMessage := "" @@ -157,7 +158,13 @@ func (migration MigratePosts) Execute() []PostResult { if wordpressImageErr != nil { errorMessage = errorMessage + wordpressImageErr.Error() - createImageFailureResult(imageUrl, migration.ResultsDatabase) + imageFailureResult := ImageResult{ + OldUrl: imageUrl, + NewUrl: "", + WordpressId: 0, + IsSuccess: false, + } + imageResults = append(imageResults, imageFailureResult) continue } //first photo is the featured photo @@ -271,7 +278,9 @@ func (migration MigratePosts) Execute() []PostResult { } func updatePostRelationships(postResults []PostResult, migration MigratePosts) { - for _, postResult := range postResults { + fmt.Println("Updating post relationships") + for i, postResult := range postResults { + fmt.Println("Updating post", i+1, "/", len(postResults)) if postResult.IsSuccess { var relatedWordpressIds []int @@ -357,7 +366,9 @@ func getSuccessfulWordpressImageIds(imageResults []ImageResult) []int { var output []int for _, imageResult := range imageResults { - output = append(output, imageResult.WordpressId) + if imageResult.IsSuccess { + output = append(output, imageResult.WordpressId) + } } return output @@ -366,15 +377,18 @@ func getSuccessfulWordpressImageIds(imageResults []ImageResult) []int { func updateAcfImages(imageResults []ImageResult, postId int, baseUrl, user, pass string) { if len(imageResults) > 0 { wordpressImageIds := getSuccessfulWordpressImageIds(imageResults) - updateAcfImages := wordpress.UpdateAcfImages{ - Acf: wordpress.AcfImages{ - PostImages: wordpressImageIds, - }, - } - acfImagesErr := updateAcfImages.Execute(baseUrl, user, pass, postId) - if acfImagesErr != nil { - fmt.Println("Error updating acf images for post", postId, ":", acfImagesErr.Error()) + if len(wordpressImageIds) > 0 { + updateAcfImages := wordpress.UpdateAcfImages{ + Acf: wordpress.AcfImages{ + PostImages: wordpressImageIds, + }, + } + acfImagesErr := updateAcfImages.Execute(baseUrl, user, pass, postId) + + if acfImagesErr != nil { + fmt.Println("Error updating acf images for post", postId, ":", acfImagesErr.Error()) + } } } } diff --git a/services/slowtwitch/get-images-and-post-html.go b/services/slowtwitch/get-images-and-post-html.go index 3c3680f..d1ce43e 100644 --- a/services/slowtwitch/get-images-and-post-html.go +++ b/services/slowtwitch/get-images-and-post-html.go @@ -33,6 +33,11 @@ func GetImagesAndPostHtml(url string) (imagePaths []string, htmlBody string, err }) // Get blog html, remove first image because wordpress will handle that as a featured image blog := doc.Find(".detail_text") + blog.Find("a").Each(func(i int, a *goquery.Selection) { + if a.Text() == "Slideshow" { + a.Remove() + } + }) htmlBody, err = blog.Html() return }