Fixed errs on post migrator

This commit is contained in:
Ross Trottier 2024-05-21 11:08:47 -06:00
parent 732b238e52
commit a9362d02ce

View File

@ -44,7 +44,13 @@ func (migration MigratePosts) Execute() {
for _, postId := range slowtwitchPostIdsForMigration { for _, postId := range slowtwitchPostIdsForMigration {
errorMessage := "" errorMessage := ""
//migrate //migrate
postBase, err := slowtwitch.GetPostBase(postId, migration.SlowtwitchDatabase) postBase, postBaseErr := slowtwitch.GetPostBase(postId, migration.SlowtwitchDatabase)
if postBaseErr != nil {
errorMessage = errorMessage + postBaseErr.Error()
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue
}
createWordpressPost := wordpress.CreatePost{ createWordpressPost := wordpress.CreatePost{
Title: postBase.Title, Title: postBase.Title,
Excerpt: postBase.Description, Excerpt: postBase.Description,
@ -75,12 +81,6 @@ func (migration MigratePosts) Execute() {
} }
} }
if err != nil {
errorMessage = errorMessage + err.Error()
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue
}
var wordPressCategoryIds []int var wordPressCategoryIds []int
var firstCategoryResult CategoryResult var firstCategoryResult CategoryResult
for _, slowtwitchCategoryId := range postBase.CategoryIds { for _, slowtwitchCategoryId := range postBase.CategoryIds {
@ -95,10 +95,10 @@ func (migration MigratePosts) Execute() {
} }
createWordpressPost.Categories = wordPressCategoryIds createWordpressPost.Categories = wordPressCategoryIds
//Get Author ID //Get Author ID
editor, err := GetEditor(postBase.Author, migration.ResultsDatabase) editor, findEditorErr := GetEditor(postBase.Author, migration.ResultsDatabase)
if err != nil { if findEditorErr != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + findEditorErr.Error()
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase) createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -116,25 +116,26 @@ func (migration MigratePosts) Execute() {
//Get page, parse out post data and images //Get page, parse out post data and images
//Upload images to wordpress, swap out with new image urls //Upload images to wordpress, swap out with new image urls
//Submit //Submit
imagePaths, html, err := slowtwitch.GetImagesAndPostHtml(oldLink) imagePaths, html, retreiveHtmlErr := slowtwitch.GetImagesAndPostHtml(oldLink)
if err != nil { if retreiveHtmlErr != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + retreiveHtmlErr.Error()
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase) createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
//Create images from the image paths
var imageResults []ImageResult var imageResults []ImageResult
for i, imagePath := range imagePaths { for i, imagePath := range imagePaths {
//construct URL
imageUrl := "https://www.slowtwitch.com" + imagePath imageUrl := "https://www.slowtwitch.com" + imagePath
createWordpressImage := wordpress.CreateImage{ createWordpressImage := wordpress.CreateImage{
Url: imageUrl, Url: imageUrl,
} }
//submit image
wordpressImage, wordpressImageErr := createWordpressImage.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
wordpressImage, err := createWordpressImage.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) if wordpressImageErr != nil {
errorMessage = errorMessage + wordpressImageErr.Error()
if err != nil {
errorMessage = errorMessage + err.Error()
createImageFailureResult(imageUrl, migration.ResultsDatabase) createImageFailureResult(imageUrl, migration.ResultsDatabase)
continue continue
} }
@ -153,7 +154,7 @@ func (migration MigratePosts) Execute() {
//replace old links with new in post html //replace old links with new in post html
strings.ReplaceAll(html, imageUrl, wordpressImage.Link) strings.ReplaceAll(html, imageUrl, wordpressImage.Link)
//create redirect //create redirect
createRedirect := wordpress.CreateRedirect{ imageRedirect := wordpress.CreateRedirect{
Title: postBase.Title + "image-" + string((i + 1)), Title: postBase.Title + "image-" + string((i + 1)),
Url: imagePath, Url: imagePath,
MatchType: "page", MatchType: "page",
@ -165,13 +166,17 @@ func (migration MigratePosts) Execute() {
}, },
} }
createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) _, imageRedirectErr := imageRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if imageRedirectErr != nil {
fmt.Println("Failed to create image redirect:", imageUrl, ":", imageRedirectErr.Error())
}
} }
createWordpressPost.Content = html createWordpressPost.Content = html
post, err := createWordpressPost.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) post, createWordpressPostErr := createWordpressPost.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil { if createWordpressPostErr != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + createWordpressPostErr.Error()
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase) createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -191,14 +196,14 @@ func (migration MigratePosts) Execute() {
ErrorMessage: errorMessage, ErrorMessage: errorMessage,
} }
postResultId, err := CreatePostResult(postResult, migration.ResultsDatabase) postResultId, createPostResultErr := CreatePostResult(postResult, migration.ResultsDatabase)
if err != nil { if createPostResultErr != nil {
fmt.Println("Could not record post result for Slowtwitch post:" + strconv.Itoa(postId) + err.Error()) fmt.Println("Could not record post result for Slowtwitch post:" + strconv.Itoa(postId) + createPostResultErr.Error())
} }
for _, imageResult := range imageResults { for _, imageResult := range imageResults {
imageResult.PostId = postResultId imageResult.PostId = postResultId
err := CreateImageResult(imageResult, migration.ResultsDatabase) createImageResultErr := CreateImageResult(imageResult, migration.ResultsDatabase)
if err != nil { if createImageResultErr != nil {
fmt.Println("Error recording image result") fmt.Println("Error recording image result")
} }
} }
@ -214,9 +219,9 @@ func (migration MigratePosts) Execute() {
Url: "/" + strings.ReplaceAll(postResult.NewUrl, migration.WordpressBaseUrl, ""), Url: "/" + strings.ReplaceAll(postResult.NewUrl, migration.WordpressBaseUrl, ""),
}, },
} }
_, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) _, postRedirectErr := postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil { if postRedirectErr != nil {
fmt.Println("Error creating redirect for", postId, ":"+err.Error()) fmt.Println("Error creating redirect for", postId, ":"+postRedirectErr.Error())
} }
fmt.Println("Successfully created post and result for", postId) fmt.Println("Successfully created post and result for", postId)
// TODO Update advanced Custom Fields with images // TODO Update advanced Custom Fields with images