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