create redirects, redirect url converter, results db
This commit is contained in:
parent
00ba765dc8
commit
5737ce23d1
37
main.go
37
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)
|
||||
}
|
||||
|
45
services/migration/category-result.go
Normal file
45
services/migration/category-result.go
Normal file
@ -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")
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package slowtwitch
|
||||
package migration
|
||||
|
||||
import (
|
||||
"database/sql"
|
18
services/migration/migrate-categories.go
Normal file
18
services/migration/migrate-categories.go
Normal file
@ -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
|
||||
|
||||
}*/
|
@ -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
|
||||
}
|
||||
|
27
services/slowtwitch/get-page-status.go
Normal file
27
services/slowtwitch/get-page-status.go
Normal file
@ -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, "<h2>Error</h2>") {
|
||||
return 404
|
||||
} else {
|
||||
return 200
|
||||
}
|
||||
}
|
16
services/slowtwitch/url-converter.go
Normal file
16
services/slowtwitch/url-converter.go
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
|
@ -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+`"`)
|
||||
|
@ -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)
|
||||
|
50
services/wordpress/create-redirect.go
Normal file
50
services/wordpress/create-redirect.go
Normal file
@ -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")
|
||||
}
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user