diff --git a/main.go b/main.go index 26c00d3..70efddb 100644 --- a/main.go +++ b/main.go @@ -1,33 +1,44 @@ package main import ( - "federated.computer/wp-sync-slowtwitch/services/slowtwitch" + "federated.computer/wp-sync-slowtwitch/services/wordpress" "fmt" ) -const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/" +const baseUrl = "https://go-api-playground.local/wp-json/" const wordpressKey = "admin" const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" const slowtwitchAdminUser = "admin" const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv" -const slowtwitchDbUrl = "slowtwitch.northend.network" -const slowtwitchDbPort = "3306" const slowtwitchDbName = "slowtwitch" +const migrationDbName = "slowtwitch_transfer" +const federatedDbUrl = "slowtwitch.northend.network" +const federatedDbPort = "3306" var appCache AppCache func main() { - // TODO Use cached data to correctly build post form submissions - slowtwitchDB, err := slowtwitch.Connect(slowtwitchAdminUser, slowtwitchAdminPass, slowtwitchDbUrl, slowtwitchDbPort, slowtwitchDbName) + // TODO Category migration + // TODO User migration + // TODO Article migration + //slowtwitchDB, err := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName) + createRedirect := wordpress.CreateRedirect{ + Url: "/gotest", + Title: "Test From Go", + MatchType: "page", + ActionType: "url", + ActionCode: 301, + GroupId: 1, + ActionData: wordpress.ActionData{ + Url: "/GoTestResult", + }, + } + + result, err := createRedirect.Execute(baseUrl, wordpressKey, wordpressSecret) if err != nil { - fmt.Println(err) - } - - categories := slowtwitch.GetCategories(slowtwitchDB) - - for _, category := range categories { - fmt.Println(category.FullName) + panic(err) } + fmt.Println(result) } diff --git a/services/migration/category-result.go b/services/migration/category-result.go new file mode 100644 index 0000000..b969dfb --- /dev/null +++ b/services/migration/category-result.go @@ -0,0 +1,45 @@ +package migration + +import ( + "database/sql" + "errors" + "fmt" +) + +type CategoryResult struct { + WordpressId int + SlowtwitchId int + OldUrl string + OldUrlStatus int + NewUrl string + IsSuccess bool + ErrorMessage string +} + +func CreateCategoryResult(result CategoryResult, db *sql.DB) error { + _, err := db.Exec("insert into CategoryResults (SlowtwitchId, WordpressId, OldUrl, NewUrl, IsSuccess, ErrorMessage, OldUrlStatus) values (?, ?, ?, ?, ?, ?, ?);", result.SlowtwitchId, result.WordpressId, result.OldUrl, result.NewUrl, result.IsSuccess, result.ErrorMessage) + return err +} + +func GetSlowtwitchCategoryResult(slowtwitchId int, db *sql.DB) (CategoryResult, error) { + rows, err := db.Query("select WordpressId, SlowtwitchId, OldUrl, NewUrl, (IsSuccess = b'1'), ErrorMessage, OldUrlStatus from CategoryResults where SlowtwitchId = ?", slowtwitchId) + if err != nil { + fmt.Println(err) + } + + defer rows.Close() + + for rows.Next() { + var slowtwitchCategoryResult CategoryResult + + err = rows.Scan(&slowtwitchCategoryResult.WordpressId, &slowtwitchCategoryResult.SlowtwitchId, &slowtwitchCategoryResult.OldUrl, &slowtwitchCategoryResult.NewUrl, &slowtwitchCategoryResult.IsSuccess, &slowtwitchCategoryResult.ErrorMessage, &slowtwitchCategoryResult.OldUrlStatus) + if err != nil { + fmt.Println(err) + } + if slowtwitchCategoryResult.SlowtwitchId == slowtwitchId { + return slowtwitchCategoryResult, nil + } + } + + return CategoryResult{}, errors.New("Category Not Found") +} diff --git a/services/slowtwitch/slowtwitch-db.go b/services/migration/get-db-connection.go similarity index 95% rename from services/slowtwitch/slowtwitch-db.go rename to services/migration/get-db-connection.go index b3e1b25..e4eb9ae 100644 --- a/services/slowtwitch/slowtwitch-db.go +++ b/services/migration/get-db-connection.go @@ -1,4 +1,4 @@ -package slowtwitch +package migration import ( "database/sql" diff --git a/services/migration/migrate-categories.go b/services/migration/migrate-categories.go new file mode 100644 index 0000000..b47185a --- /dev/null +++ b/services/migration/migrate-categories.go @@ -0,0 +1,18 @@ +package migration + +//THIS SHOULD BE A STRUCT WITH AN EXECUTE +/*import ( + "database/sql" + "federated.computer/wp-sync-slowtwitch/services/slowtwitch" +) + +func MigrateCategories(slowtwitchDB *sql.DB, resultsDB *sql.DB, baseUrl, user, pass string) ([]CategoryResult, error) { + slowtwitchCategories := slowtwitch.GetCategories(slowtwitchDB) + + for _, category := range slowtwitchCategories { + + } + + var results []CategoryResult + +}*/ diff --git a/services/slowtwitch/get-category.go b/services/slowtwitch/get-category.go index 3648eac..aab1b77 100644 --- a/services/slowtwitch/get-category.go +++ b/services/slowtwitch/get-category.go @@ -3,6 +3,7 @@ package slowtwitch import ( "database/sql" "fmt" + "sort" ) type SlowtwitchCategory struct { @@ -15,7 +16,7 @@ type SlowtwitchCategory struct { } func GetCategories(db *sql.DB) []SlowtwitchCategory { - rows, err := db.Query("select ID, Name, Full_Name, FatherID, CatDepth, Number_of_Links from glinks_Category;") + rows, err := db.Query("select ID, Name, Full_Name, FatherID, CatDepth, Number_of_Links from glinks_Category where Number_of_Links > 0;") if err != nil { fmt.Println(err) @@ -36,5 +37,9 @@ func GetCategories(db *sql.DB) []SlowtwitchCategory { categories = append(categories, category) } + sort.Slice(categories, func(i, j int) bool { + return categories[i].CatDepth < categories[j].CatDepth + }) + return categories } diff --git a/services/slowtwitch/get-page-status.go b/services/slowtwitch/get-page-status.go new file mode 100644 index 0000000..bb31a7a --- /dev/null +++ b/services/slowtwitch/get-page-status.go @@ -0,0 +1,27 @@ +package slowtwitch + +import ( + "fmt" + "io" + "net/http" + "strings" +) + +func GetPageStatus(url string) int { + response, err := http.Get(url) + + if err != nil { + fmt.Println(err) + } + + defer response.Body.Close() + + bytes, _ := io.ReadAll(response.Body) + html := string(bytes) + + if strings.Contains(html, "

Error

") { + return 404 + } else { + return 200 + } +} diff --git a/services/slowtwitch/url-converter.go b/services/slowtwitch/url-converter.go new file mode 100644 index 0000000..36b2bfb --- /dev/null +++ b/services/slowtwitch/url-converter.go @@ -0,0 +1,16 @@ +package slowtwitch + +import "strings" + +func ConvertUrlToCategoryFormat(oldUrl string) string { + output := strings.ReplaceAll(oldUrl, " ", "_") + output = strings.ReplaceAll(output, "$", "_") + output = strings.ReplaceAll(output, "(", "_") + output = strings.ReplaceAll(output, ")", "_") + + return output + "/index.html" +} + +func GetURL(path string) string { + return "https://www.slowtwitch.com/" + path +} diff --git a/services/wordpress/create-category.go b/services/wordpress/create-category.go index 98190b2..ee58def 100644 --- a/services/wordpress/create-category.go +++ b/services/wordpress/create-category.go @@ -9,11 +9,10 @@ type CreateCategory struct { Name string `json:"name"` Description string `json:"description"` ParentId int `json:"parent"` - Slug string `json:"slug"` } func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData { - endpoint := baseUrl + "categories" + endpoint := baseUrl + "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 fd85671..8098518 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+"media", bytes.NewReader(body)) + request, err := http.NewRequest("POST", baseUrl+"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 a6c9165..c78bf79 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 + "posts" + endpoint := baseUrl + "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 new file mode 100644 index 0000000..efe5903 --- /dev/null +++ b/services/wordpress/create-redirect.go @@ -0,0 +1,50 @@ +package wordpress + +import ( + "encoding/json" + "errors" + "federated.computer/wp-sync-slowtwitch/utilities" +) + +type CreateRedirect struct { + Title string `json:"title"` + Url string `json:"url"` + MatchType string `json:"match_type"` + ActionType string `json:"action_type"` + ActionCode int `json:"action_code"` + GroupId int `json:"group_id"` + ActionData ActionData `json:"action_data"` +} + +type ActionData struct { + Url string `json:"url"` +} + +type CreateRedirectResponse struct { + Items []CreatedRedirect `json:"items"` +} + +type CreatedRedirect struct { + Id int `json:"id"` + Url string `json:"url"` + MatchUrl string `json:"match_url"` + ActionData struct { + Url string `json:"url"` + } `json:"action_data"` +} + +func (parameters *CreateRedirect) Execute(baseUrl, user, pass string) (CreatedRedirect, error) { + url := baseUrl + "redirection/v1/redirect" + response := utilities.PostHttpRequestToWordpress(url, user, pass, parameters) + var results CreateRedirectResponse + err := json.Unmarshal(response, &results) + if err != nil { + return CreatedRedirect{}, err + } + for _, result := range results.Items { + if result.Url == parameters.Url { + return result, nil + } + } + return CreatedRedirect{}, errors.New("Redirect Not Found") +} diff --git a/services/wordpress/create-tag.go b/services/wordpress/create-tag.go index 13f720f..11f3df1 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 + "tags" + endpoint := baseUrl + "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 c26e238..5914c1c 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 + "users" + endpoint := baseUrl + "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 b183e52..6692e81 100644 --- a/services/wordpress/get-category.go +++ b/services/wordpress/get-category.go @@ -14,7 +14,7 @@ type CategoryData struct { } func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) { - endpoint := baseUrl + "categories?search=" + url.QueryEscape(name) + endpoint := baseUrl + "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 f9adde7..b71a2a6 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 + "posts?per_page=99" + url := baseUrl + "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 c9f40a6..c0c0622 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 + "tags?search=" + url.QueryEscape(tagName) + endpoint := baseUrl + "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 d7fa3c0..7349bb0 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 + "users?search=" + url.QueryEscape(name) + endpoint := baseUrl + "wp/v2/users?search=" + url.QueryEscape(name) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) var userData []UserData err := json.Unmarshal(body, &userData)