fixed redirects - refactored for readability
This commit is contained in:
parent
83ef9f4192
commit
7931a859f3
2
main.go
2
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"
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
1
services/migration/migrate-authors.go
Normal file
1
services/migration/migrate-authors.go
Normal file
@ -0,0 +1 @@
|
||||
package migration
|
@ -29,43 +29,22 @@ 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,
|
||||
},
|
||||
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
|
||||
}
|
||||
|
||||
_, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
err = nil
|
||||
}
|
||||
//submit results
|
||||
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,
|
||||
@ -81,9 +60,45 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
|
||||
fmt.Println(err)
|
||||
err = nil
|
||||
}
|
||||
output = append(output, overallResult)
|
||||
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,
|
||||
},
|
||||
}
|
||||
return output
|
||||
_, 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) {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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+`"`)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user