diff --git a/main.go b/main.go index cf90234..879edc6 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "fmt" ) -const baseUrl = "https://slowtwitch.cloud/wp-json/" +const baseUrl = "https://slowtwitch.cloud/" const wordpressKey = "admin@slowtwitch.cloud" const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh" diff --git a/services/migration/get-db-connection.go b/services/migration/get-db-connection.go index e4eb9ae..6a2b850 100644 --- a/services/migration/get-db-connection.go +++ b/services/migration/get-db-connection.go @@ -20,7 +20,7 @@ func Connect(user, pass, url, port, dbName string) (*sql.DB, error) { return nil, err } - fmt.Println("Successfully connected!") + fmt.Println("Successfully connected to:", dbName) return db, nil } diff --git a/services/migration/migrate-authors.go b/services/migration/migrate-authors.go new file mode 100644 index 0000000..753000b --- /dev/null +++ b/services/migration/migrate-authors.go @@ -0,0 +1 @@ +package migration diff --git a/services/migration/migrate-categories.go b/services/migration/migrate-categories.go index 02652cb..6e8b0e4 100644 --- a/services/migration/migrate-categories.go +++ b/services/migration/migrate-categories.go @@ -29,63 +29,78 @@ func (migration *MigrateCategories) Execute() []CategoryResult { errorMessage := "" - //Get wordpress parent if exists wordpressParentId, err := getWordpressParentCategoryId(category, migration, errorMessage) if err != nil { errorMessage = errorMessage + err.Error() err = nil } - //convert url for redirect - slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName) - slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath) - httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl) - //create in wp - createWordpressCategory := wordpress.CreateCategory{ - Name: strings.Trim(category.Name, " "), - Description: "", - ParentId: wordpressParentId, - } - wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) - //submit redirect - createRedirect := wordpress.CreateRedirect{ - Url: slowtwitchPath, - Title: "Category: " + wordpressCategory.Name, - MatchType: "page", - ActionType: "url", - ActionCode: 301, - GroupId: 1, - ActionData: wordpress.ActionData{ - Url: wordpressCategory.Slug, - }, - } - - _, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) - - if err != nil { - errorMessage = errorMessage + err.Error() - err = nil - } - //submit results - overallResult := CategoryResult{ - WordpressId: wordpressCategory.Id, - SlowtwitchId: category.Id, - OldUrl: slowtwitchUrl, - OldUrlStatus: httpStatus, - NewUrl: wordpressCategory.Link, - IsSuccess: true, - ErrorMessage: errorMessage, - } - - err = CreateCategoryResult(overallResult, migration.ResultsDatabase) - if err != nil { - fmt.Println(err) - err = nil - } + slowtwitchPath, slowtwitchUrl, httpStatus := getSlowtwitchUrlsAndVerify(category) + wordpressCategory := createInWordpress(category, wordpressParentId, migration) + err, errorMessage = createRedirect(wordpressCategory, migration, slowtwitchPath, err, errorMessage) + overallResult := submitResults(wordpressCategory, category, slowtwitchUrl, httpStatus, errorMessage, err, migration) + fmt.Println("Successfully Created Category:", wordpressCategory.Name) output = append(output, overallResult) } return output } +func submitResults(wordpressCategory wordpress.CategoryData, category slowtwitch.SlowtwitchCategory, slowtwitchUrl string, httpStatus int, errorMessage string, err error, migration *MigrateCategories) CategoryResult { + overallResult := CategoryResult{ + WordpressId: wordpressCategory.Id, + SlowtwitchId: category.Id, + OldUrl: slowtwitchUrl, + OldUrlStatus: httpStatus, + NewUrl: wordpressCategory.Link, + IsSuccess: true, + ErrorMessage: errorMessage, + } + + err = CreateCategoryResult(overallResult, migration.ResultsDatabase) + if err != nil { + fmt.Println(err) + err = nil + } + return overallResult +} + +func createRedirect(wordpressCategory wordpress.CategoryData, migration *MigrateCategories, slowtwitchPath string, err error, errorMessage string) (error, string) { + redirectUrl := "/" + strings.ReplaceAll(wordpressCategory.Link, migration.WordpressBaseUrl, "") + createRedirect := wordpress.CreateRedirect{ + Url: slowtwitchPath, + Title: "Category: " + wordpressCategory.Name, + MatchType: "page", + ActionType: "url", + ActionCode: 301, + GroupId: 1, + ActionData: wordpress.ActionData{ + Url: redirectUrl, + }, + } + _, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + if err != nil { + errorMessage = errorMessage + err.Error() + err = nil + } + return err, errorMessage +} + +func createInWordpress(category slowtwitch.SlowtwitchCategory, wordpressParentId int, migration *MigrateCategories) wordpress.CategoryData { + createWordpressCategory := wordpress.CreateCategory{ + Name: strings.Trim(category.Name, " "), + Description: "", + ParentId: wordpressParentId, + } + wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + return wordpressCategory +} + +func getSlowtwitchUrlsAndVerify(category slowtwitch.SlowtwitchCategory) (string, string, int) { + slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName) + slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath) + httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl) + return slowtwitchPath, slowtwitchUrl, httpStatus +} + func getWordpressParentCategoryId(category slowtwitch.SlowtwitchCategory, migration *MigrateCategories, errorMessage string) (int, error) { if category.FatherId != 0 { parentCategory, err := GetSlowtwitchCategoryResult(category.FatherId, migration.ResultsDatabase) diff --git a/services/slowtwitch/url-converter.go b/services/slowtwitch/url-converter.go index 36b2bfb..ad4eb26 100644 --- a/services/slowtwitch/url-converter.go +++ b/services/slowtwitch/url-converter.go @@ -8,7 +8,7 @@ func ConvertUrlToCategoryFormat(oldUrl string) string { output = strings.ReplaceAll(output, "(", "_") output = strings.ReplaceAll(output, ")", "_") - return output + "/index.html" + return "/" + output + "/index.html" } func GetURL(path string) string { diff --git a/services/wordpress/create-category.go b/services/wordpress/create-category.go index ee58def..0598042 100644 --- a/services/wordpress/create-category.go +++ b/services/wordpress/create-category.go @@ -12,7 +12,7 @@ type CreateCategory struct { } func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData { - endpoint := baseUrl + "wp/v2/categories" + endpoint := baseUrl + "wp-json/wp/v2/categories" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) var category CategoryData err := json.Unmarshal(body, &category) diff --git a/services/wordpress/create-image.go b/services/wordpress/create-image.go index 8098518..c5bd00a 100644 --- a/services/wordpress/create-image.go +++ b/services/wordpress/create-image.go @@ -24,7 +24,7 @@ func (parameters *CreateImage) Execute(baseUrl, user, pass string) CreateImageRe defer utilities.CloseBodyAndCheckError(resp.Body) body, err := io.ReadAll(resp.Body) utilities.CheckError(err) - request, err := http.NewRequest("POST", baseUrl+"wp/v2/media", bytes.NewReader(body)) + request, err := http.NewRequest("POST", baseUrl+"wp-json/wp/v2/media", bytes.NewReader(body)) utilities.CheckError(err) filename := GetFileName(parameters.Url) request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`) diff --git a/services/wordpress/create-post.go b/services/wordpress/create-post.go index c78bf79..6096fe7 100644 --- a/services/wordpress/create-post.go +++ b/services/wordpress/create-post.go @@ -24,7 +24,7 @@ type CreatePostResponse struct { } func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse { - endpoint := baseUrl + "wp/v2/posts" + endpoint := baseUrl + "wp-json/wp/v2/posts" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) var post CreatePostResponse err := json.Unmarshal(body, &post) diff --git a/services/wordpress/create-redirect.go b/services/wordpress/create-redirect.go index efe5903..05f753f 100644 --- a/services/wordpress/create-redirect.go +++ b/services/wordpress/create-redirect.go @@ -34,7 +34,7 @@ type CreatedRedirect struct { } func (parameters *CreateRedirect) Execute(baseUrl, user, pass string) (CreatedRedirect, error) { - url := baseUrl + "redirection/v1/redirect" + url := baseUrl + "wp-json/redirection/v1/redirect" response := utilities.PostHttpRequestToWordpress(url, user, pass, parameters) var results CreateRedirectResponse err := json.Unmarshal(response, &results) diff --git a/services/wordpress/create-tag.go b/services/wordpress/create-tag.go index 11f3df1..6d62f48 100644 --- a/services/wordpress/create-tag.go +++ b/services/wordpress/create-tag.go @@ -18,7 +18,7 @@ type CreateTagResponse struct { } func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse { - endpoint := baseUrl + "wp/v2/tags" + endpoint := baseUrl + "wp-json/wp/v2/tags" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) var tagResponse CreateTagResponse err := json.Unmarshal(body, &tagResponse) diff --git a/services/wordpress/create-user.go b/services/wordpress/create-user.go index 5914c1c..16251b2 100644 --- a/services/wordpress/create-user.go +++ b/services/wordpress/create-user.go @@ -24,7 +24,7 @@ type CreateUserResponse struct { } func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse { - endpoint := baseUrl + "wp/v2/users" + endpoint := baseUrl + "wp-json/wp/v2/users" body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) var userData CreateUserResponse err := json.Unmarshal(body, &userData) diff --git a/services/wordpress/get-category.go b/services/wordpress/get-category.go index 95e5bf1..af5e0f6 100644 --- a/services/wordpress/get-category.go +++ b/services/wordpress/get-category.go @@ -16,7 +16,7 @@ type CategoryData struct { } func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) { - endpoint := baseUrl + "wp/v2/categories?search=" + url.QueryEscape(name) + endpoint := baseUrl + "wp-json/wp/v2/categories?search=" + url.QueryEscape(name) response := utilities.GetHttpRequestToWordpress(endpoint, user, pass) var categoryData []CategoryData err := json.Unmarshal(response, &categoryData) diff --git a/services/wordpress/get-posts.go b/services/wordpress/get-posts.go index b71a2a6..0494584 100644 --- a/services/wordpress/get-posts.go +++ b/services/wordpress/get-posts.go @@ -22,7 +22,7 @@ type PostData struct { } func GetPosts(baseUrl, user, pass string) []PostData { - url := baseUrl + "wp/v2/posts?per_page=99" + url := baseUrl + "wp-json/wp/v2/posts?per_page=99" body := utilities.GetHttpRequestToWordpress(url, user, pass) var posts []PostData err := json.Unmarshal(body, &posts) diff --git a/services/wordpress/get-tag.go b/services/wordpress/get-tag.go index c0c0622..d35f018 100644 --- a/services/wordpress/get-tag.go +++ b/services/wordpress/get-tag.go @@ -13,7 +13,7 @@ type TagData struct { } func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) { - endpoint := baseUrl + "wp/v2/tags?search=" + url.QueryEscape(tagName) + endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) var tagData []TagData err := json.Unmarshal(body, &tagData) diff --git a/services/wordpress/get-user.go b/services/wordpress/get-user.go index 7349bb0..01ad2d7 100644 --- a/services/wordpress/get-user.go +++ b/services/wordpress/get-user.go @@ -12,7 +12,7 @@ type UserData struct { } func GetUser(baseUrl, name, user, pass string) (UserData, bool) { - endpoint := baseUrl + "wp/v2/users?search=" + url.QueryEscape(name) + endpoint := baseUrl + "wp-json/wp/v2/users?search=" + url.QueryEscape(name) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) var userData []UserData err := json.Unmarshal(body, &userData)