diff --git a/main.go b/main.go index f519b77..51fdcaf 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,6 @@ package main import ( "database/sql" "federated.computer/wp-sync-slowtwitch/services/migration" - "federated.computer/wp-sync-slowtwitch/services/slowtwitch" - "fmt" ) const baseUrl = "https://slowtwitch.cloud/" @@ -37,17 +35,6 @@ func main() { resultsDB = resultsDatabase } - // TODO Article migration - //EXPERIMENT AREA START - imageUrls, html, err := slowtwitch.GetImagesAndPostHtml("https://www.slowtwitch.com/Products/Components/SRAM_Drops_New_RED_AXS_Groupset_8950.html") - if err != nil { - fmt.Println(err) - } - for i, imageUrl := range imageUrls { - fmt.Println(i, imageUrl) - } - fmt.Println(html) - //EXPERIMENT END editorMigration := migration.MigrateAuthors{ SlowtwitchDatabase: slowtwitchDB, ResultsDatabase: resultsDB, @@ -64,4 +51,12 @@ func main() { WordpressPassword: wordpressSecret, } categoryMigration.Execute() + postMigration := migration.MigratePosts{ + SlowtwitchDatabase: slowtwitchDB, + ResultsDatabase: resultsDB, + WordpressBaseUrl: baseUrl, + WordpressUser: wordpressKey, + WordpressPassword: wordpressSecret, + } + postMigration.Execute() } diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index 316dc65..50d9396 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -6,6 +6,7 @@ import ( "federated.computer/wp-sync-slowtwitch/services/wordpress" "fmt" "slices" + "strconv" "strings" ) @@ -53,11 +54,15 @@ func (migration MigratePosts) Execute() { createWordpressPost := wordpress.CreatePost{ Title: postBase.Title, Excerpt: postBase.Description, - Status: "published", + Status: "publish", } if postBase.DatePublished.Valid { - createWordpressPost.Date = postBase.DatePublished.Time.String() + t := postBase.DatePublished.Time + timeString := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", + t.Year(), t.Month(), t.Day(), + t.Hour(), t.Minute(), t.Second()) + createWordpressPost.Date = timeString } else { errorMessage = errorMessage + "Invalid Date Published" // TODO SEND TO RESULTS DB WITH CALL @@ -188,7 +193,7 @@ func (migration MigratePosts) Execute() { postResultId, err := CreatePostResult(postResult, migration.ResultsDatabase) if err != nil { - panic("Could not record post result for Slowtwitch post:" + string(postId)) + fmt.Println("Could not record post result for Slowtwitch post:" + strconv.Itoa(postId) + err.Error()) } for _, imageResult := range imageResults { imageResult.PostId = postResultId @@ -197,7 +202,22 @@ func (migration MigratePosts) Execute() { fmt.Println("Error recording image result") } } - // TODO record redirect + oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "") + postRedirect := wordpress.CreateRedirect{ + Title: "Article Redirect" + postBase.Title, + Url: strings.ReplaceAll(oldPath, ".html", ""), + MatchType: "page", + ActionType: "url", + ActionCode: 301, + GroupId: 1, + ActionData: wordpress.ActionData{ + Url: "/" + strings.ReplaceAll(postResult.NewUrl, migration.WordpressBaseUrl, ""), + }, + } + _, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + if err != nil { + fmt.Println(err) + } } //Update related posts (get from post results db) as second loop //Update advanced Custom Fields with images diff --git a/services/migration/post-result.go b/services/migration/post-result.go index 7cac1dd..beb7213 100644 --- a/services/migration/post-result.go +++ b/services/migration/post-result.go @@ -13,7 +13,7 @@ type PostResult struct { } func CreatePostResult(parameters PostResult, db *sql.DB) (int, error) { - result, err := db.Exec("insert into ImageResults (WordpressId, SlowtwitchId, OldUrl, OldUrlStatus, NewUrl, IsSuccess, ErrorMessage) values (?, ?, ?, ?, ?, ?, ?)", parameters.WordpressId, parameters.SlowtwitchId, parameters.OldUrl, parameters.OldUrlStatus, parameters.NewUrl, parameters.IsSuccess, parameters.ErrorMessage) + result, err := db.Exec("insert into PostResults (WordpressId, SlowtwitchId, OldUrl, OldUrlStatus, NewUrl, IsSuccess, ErrorMessage) values (?, ?, ?, ?, ?, ?, ?)", parameters.WordpressId, parameters.SlowtwitchId, parameters.OldUrl, parameters.OldUrlStatus, parameters.NewUrl, parameters.IsSuccess, parameters.ErrorMessage) if err != nil { return 0, err } else { diff --git a/services/slowtwitch/get-images-and-post-html.go b/services/slowtwitch/get-images-and-post-html.go index 488c44c..3c3680f 100644 --- a/services/slowtwitch/get-images-and-post-html.go +++ b/services/slowtwitch/get-images-and-post-html.go @@ -14,7 +14,7 @@ func GetImagesAndPostHtml(url string) (imagePaths []string, htmlBody string, err return } defer res.Body.Close() - + imagePaths = make([]string, 0) // Read the HTML content htmlContent, err := io.ReadAll(res.Body) if err != nil { @@ -33,7 +33,6 @@ 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(":first-child").Remove() htmlBody, err = blog.Html() return } diff --git a/services/slowtwitch/url-converter.go b/services/slowtwitch/url-converter.go index 45e7178..27440d3 100644 --- a/services/slowtwitch/url-converter.go +++ b/services/slowtwitch/url-converter.go @@ -1,6 +1,9 @@ package slowtwitch -import "strings" +import ( + "strconv" + "strings" +) func ConvertUrlToCategoryFormat(oldUrl string) string { output := convert(oldUrl) @@ -10,7 +13,7 @@ func ConvertUrlToCategoryFormat(oldUrl string) string { func ConvertPostTitleToPath(title string, id int) string { output := convert(title) - return output + "_" + string(id) + ".html" + return output + "_" + strconv.Itoa(id) + ".html" } func GetURL(path string) string { @@ -22,6 +25,7 @@ func convert(path string) string { output = strings.ReplaceAll(output, "$", "_") output = strings.ReplaceAll(output, "(", "_") output = strings.ReplaceAll(output, ")", "_") - + output = strings.ReplaceAll(output, "?", "") + output = strings.ReplaceAll(output, ",", "") return output } diff --git a/services/wordpress/create-image.go b/services/wordpress/create-image.go index 18a019e..b02972e 100644 --- a/services/wordpress/create-image.go +++ b/services/wordpress/create-image.go @@ -34,6 +34,8 @@ func (parameters *CreateImage) Execute(baseUrl, user, pass string) (CreateImageR return CreateImageResponse{}, err } filename := GetFileName(parameters.Url) + contentType := getContentType(filename) + request.Header.Set("Content-Type", contentType) request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`) request.SetBasicAuth(user, pass) rsp, err := http.DefaultClient.Do(request) @@ -63,3 +65,12 @@ func GetFileName(url string) string { } return output } + +func getContentType(filename string) string { + if strings.Contains(filename, "png") { + return "image/png" + } else if strings.Contains(filename, "gif") { + return "image/gif" + } + return "image/jpeg" +}