blog post migration checkpoint 1
This commit is contained in:
parent
b3ef5a42ad
commit
588d0e1833
3
go.mod
3
go.mod
@ -4,5 +4,8 @@ go 1.22.2
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/PuerkitoBio/goquery v1.9.2 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.2 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
)
|
||||
|
40
go.sum
40
go.sum
@ -1,4 +1,44 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
|
||||
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
|
||||
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
38
main.go
38
main.go
@ -2,7 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"federated.computer/wp-sync-slowtwitch/services/migration"
|
||||
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
||||
"fmt"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const baseUrl = "https://slowtwitch.cloud/"
|
||||
@ -19,10 +25,8 @@ const federatedDbPort = "3306"
|
||||
var appCache AppCache
|
||||
|
||||
func main() {
|
||||
// TODO Category migration
|
||||
// TODO User migration
|
||||
// TODO Article migration
|
||||
slowtwitchDB, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName)
|
||||
slowtwitchDB, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
|
||||
if slowtwitchDbErr != nil {
|
||||
fmt.Println(slowtwitchDbErr)
|
||||
}
|
||||
@ -30,7 +34,35 @@ func main() {
|
||||
if resultsDBerr != nil {
|
||||
fmt.Println(resultsDBerr)
|
||||
}
|
||||
//EXPERIMENT START
|
||||
res, err := http.Get("https://www.slowtwitch.com/Products/Components/SRAM_Drops_New_RED_AXS_Groupset_8950.html")
|
||||
if err != nil {
|
||||
log.Fatalf("http.Get -> %v", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
// Read the HTML content
|
||||
htmlContent, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatalf("ioutil.ReadAll -> %v", err)
|
||||
}
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(htmlContent)))
|
||||
if err != nil {
|
||||
log.Fatalf("goquery.NewDocumentFromReader -> %v", err)
|
||||
}
|
||||
// Find all image tags and extract their 'src' attributes
|
||||
doc.Find(".detail_text img").Each(func(i int, img *goquery.Selection) {
|
||||
imgUrl, exists := img.Attr("src")
|
||||
if exists {
|
||||
log.Printf("Image URL %d: %s", i+1, slowtwitch.GetURL(imgUrl))
|
||||
}
|
||||
})
|
||||
blog, err := doc.Find(".detail_text").Html()
|
||||
if err != nil {
|
||||
log.Fatalf("goquery.NewDocumentFromReader -> %v", err)
|
||||
}
|
||||
fmt.Println(blog)
|
||||
//EXPERIMENT END
|
||||
editorMigration := migration.MigrateAuthors{
|
||||
SlowtwitchDatabase: slowtwitchDB,
|
||||
ResultsDatabase: resultsDB,
|
||||
|
24
services/migration/get-all-migrated-post-ids.go
Normal file
24
services/migration/get-all-migrated-post-ids.go
Normal file
@ -0,0 +1,24 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"federated.computer/wp-sync-slowtwitch/services/shared"
|
||||
)
|
||||
|
||||
func GetAllMigratedPostIds(db *sql.DB) ([]int, error) {
|
||||
var output []int
|
||||
rows, err := db.Query("SELECT SlowtwitchId FROM PostResults")
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id shared.Id
|
||||
err = rows.Scan(&id.Id)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
output = append(output, int(id.Id))
|
||||
}
|
||||
return output, nil
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
||||
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MigrateAuthors struct {
|
||||
@ -24,20 +25,29 @@ func (migration *MigrateAuthors) Execute() []EditorResult {
|
||||
|
||||
if hasBeenMigrated == false {
|
||||
createUser := wordpress.CreateUser{
|
||||
Username: editor.Username,
|
||||
Email: editor.Email,
|
||||
Username: strings.Trim(editor.Username, " "),
|
||||
Email: strings.Trim(editor.Email, " "),
|
||||
Password: editor.Password,
|
||||
Roles: "editor",
|
||||
}
|
||||
|
||||
result := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
editorResult := EditorResult{
|
||||
Username: result.Username,
|
||||
Email: result.Username,
|
||||
WordpressId: result.Id,
|
||||
IsSuccess: true,
|
||||
result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
|
||||
wordpressId := 0
|
||||
isSuccess := false
|
||||
|
||||
if err == nil {
|
||||
wordpressId = result.Id
|
||||
isSuccess = true
|
||||
}
|
||||
err := CreateEditorResult(editorResult, migration.ResultsDatabase)
|
||||
|
||||
editorResult := EditorResult{
|
||||
Username: editor.Username,
|
||||
Email: editor.Email,
|
||||
WordpressId: wordpressId,
|
||||
IsSuccess: isSuccess,
|
||||
}
|
||||
err = CreateEditorResult(editorResult, migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@ -35,27 +35,31 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
|
||||
err = nil
|
||||
}
|
||||
slowtwitchPath, slowtwitchUrl, httpStatus := getSlowtwitchUrlsAndVerify(category)
|
||||
wordpressCategory := createInWordpress(category, wordpressParentId, migration)
|
||||
wordpressCategory, err := createInWordpress(category, wordpressParentId, migration)
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
} else {
|
||||
err, errorMessage = createRedirect(wordpressCategory, migration, slowtwitchPath, err, errorMessage)
|
||||
overallResult := submitResults(wordpressCategory, category, slowtwitchUrl, httpStatus, errorMessage, err, migration)
|
||||
}
|
||||
overallResult := submitResults(wordpressCategory, category, slowtwitchUrl, httpStatus, errorMessage, 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 {
|
||||
func submitResults(wordpressCategory wordpress.CategoryData, category slowtwitch.SlowtwitchCategory, slowtwitchUrl string, httpStatus int, errorMessage string, migration *MigrateCategories) CategoryResult {
|
||||
overallResult := CategoryResult{
|
||||
WordpressId: wordpressCategory.Id,
|
||||
SlowtwitchId: category.Id,
|
||||
OldUrl: slowtwitchUrl,
|
||||
OldUrlStatus: httpStatus,
|
||||
NewUrl: wordpressCategory.Link,
|
||||
IsSuccess: true,
|
||||
IsSuccess: len(errorMessage) == 0,
|
||||
ErrorMessage: errorMessage,
|
||||
}
|
||||
|
||||
err = CreateCategoryResult(overallResult, migration.ResultsDatabase)
|
||||
err := CreateCategoryResult(overallResult, migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
err = nil
|
||||
@ -84,14 +88,18 @@ func createRedirect(wordpressCategory wordpress.CategoryData, migration *Migrate
|
||||
return err, errorMessage
|
||||
}
|
||||
|
||||
func createInWordpress(category slowtwitch.SlowtwitchCategory, wordpressParentId int, migration *MigrateCategories) wordpress.CategoryData {
|
||||
func createInWordpress(category slowtwitch.SlowtwitchCategory, wordpressParentId int, migration *MigrateCategories) (wordpress.CategoryData, error) {
|
||||
createWordpressCategory := wordpress.CreateCategory{
|
||||
Name: strings.Trim(category.Name, " "),
|
||||
Description: "",
|
||||
ParentId: wordpressParentId,
|
||||
}
|
||||
wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
return wordpressCategory
|
||||
wordpressCategory, err := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
if err != nil {
|
||||
return wordpress.CategoryData{}, err
|
||||
} else {
|
||||
return wordpressCategory, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlowtwitchUrlsAndVerify(category slowtwitch.SlowtwitchCategory) (string, string, int) {
|
||||
|
115
services/migration/migrate-posts.go
Normal file
115
services/migration/migrate-posts.go
Normal file
@ -0,0 +1,115 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
||||
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MigratePosts struct {
|
||||
SlowtwitchDatabase *sql.DB
|
||||
ResultsDatabase *sql.DB
|
||||
WordpressBaseUrl string
|
||||
WordpressUser string
|
||||
WordpressPassword string
|
||||
}
|
||||
|
||||
// TODO Get list of all ST post IDs
|
||||
// TODO Get ST post by ID
|
||||
// TODO Get List of all already migrated post IDs
|
||||
|
||||
func (migration MigratePosts) Execute() {
|
||||
slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase)
|
||||
if err != nil {
|
||||
fmt.Println("Could not migrate posts:", err)
|
||||
return
|
||||
}
|
||||
migratedPostIds, err := GetAllMigratedPostIds(migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
fmt.Println("Could not migrate posts:", err)
|
||||
return
|
||||
}
|
||||
|
||||
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)
|
||||
|
||||
for _, postId := range slowtwitchPostIdsForMigration {
|
||||
errorMessage := ""
|
||||
//migrate
|
||||
postBase, err := slowtwitch.GetPostBase(postId, migration.SlowtwitchDatabase)
|
||||
createWordpressPost := wordpress.CreatePost{
|
||||
Title: postBase.Title,
|
||||
Excerpt: postBase.Description,
|
||||
}
|
||||
|
||||
if postBase.DatePublished.Valid {
|
||||
createWordpressPost.Date = postBase.DatePublished.Time.String()
|
||||
} else {
|
||||
errorMessage = errorMessage + "Invalid Date Published"
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
continue
|
||||
}
|
||||
|
||||
var wordPressCategoryIds []int
|
||||
var firstCategoryResult CategoryResult
|
||||
for _, slowtwitchCategoryId := range postBase.CategoryIds {
|
||||
categoryResult, err := GetSlowtwitchCategoryResult(slowtwitchCategoryId, migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
continue
|
||||
}
|
||||
wordPressCategoryIds = append(wordPressCategoryIds, categoryResult.WordpressId)
|
||||
firstCategoryResult = categoryResult
|
||||
}
|
||||
createWordpressPost.Categories = wordPressCategoryIds
|
||||
//Get Author ID
|
||||
editor, err := GetEditor(postBase.Author, migration.ResultsDatabase)
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
continue
|
||||
}
|
||||
createWordpressPost.Author = editor.WordpressId
|
||||
//Get old link
|
||||
oldLink := strings.ReplaceAll(firstCategoryResult.OldUrl, "index.html", "") + slowtwitch.ConvertPostTitleToPath(postBase.Title, postBase.Id)
|
||||
linkStatus := slowtwitch.GetPageStatus(oldLink)
|
||||
|
||||
if linkStatus == 404 {
|
||||
errorMessage = errorMessage + "Page not found on Slowtwitch"
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
continue
|
||||
}
|
||||
|
||||
//Get page, parse out post data and images
|
||||
//Upload images to wordpress, swap out with new image urls
|
||||
//Submit
|
||||
|
||||
//Get new link
|
||||
//Store results
|
||||
//Update advanced Custom Fields with images
|
||||
}
|
||||
//Update related posts (get from post results db) as second loop
|
||||
}
|
||||
|
||||
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
|
||||
var output []int
|
||||
|
||||
for _, slowtwitchPostId := range slowtwitchPostIds {
|
||||
hasId := slices.Contains(migratedPostIds, slowtwitchPostId)
|
||||
if hasId == false {
|
||||
output = append(output, slowtwitchPostId)
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
5
services/shared/id.go
Normal file
5
services/shared/id.go
Normal file
@ -0,0 +1,5 @@
|
||||
package shared
|
||||
|
||||
type Id struct {
|
||||
Id int64
|
||||
}
|
24
services/slowtwitch/get-all-post-ids.go
Normal file
24
services/slowtwitch/get-all-post-ids.go
Normal file
@ -0,0 +1,24 @@
|
||||
package slowtwitch
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"federated.computer/wp-sync-slowtwitch/services/shared"
|
||||
)
|
||||
|
||||
func GetAllPostIds(db *sql.DB) ([]int, error) {
|
||||
var output []int
|
||||
rows, err := db.Query("SELECT ID as Id FROM glinks_Links order by Add_Date")
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id shared.Id
|
||||
err = rows.Scan(&id.Id)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
output = append(output, int(id.Id))
|
||||
}
|
||||
return output, nil
|
||||
}
|
46
services/slowtwitch/get-post-base.go
Normal file
46
services/slowtwitch/get-post-base.go
Normal file
@ -0,0 +1,46 @@
|
||||
package slowtwitch
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"federated.computer/wp-sync-slowtwitch/services/shared"
|
||||
)
|
||||
|
||||
type SlowtwitchPostBase struct {
|
||||
Id int
|
||||
CategoryIds []int
|
||||
Title string
|
||||
Author string
|
||||
Description string
|
||||
DatePublished sql.NullTime
|
||||
}
|
||||
|
||||
func GetPostBase(id int, db *sql.DB) (SlowtwitchPostBase, error) {
|
||||
var output SlowtwitchPostBase
|
||||
//Get Base
|
||||
row := db.QueryRow("select ID, Title, LinkOwner, Add_Date, Description from glinks_Links where ID = ?", id)
|
||||
err := row.Scan(&output.Id, &output.Title, &output.Author, &output.DatePublished, &output.Description)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
//Get category Ids
|
||||
var categoryIds []int
|
||||
rows, err := db.Query("select CategoryID from glinks_CatLinks where LinkID = ?", id)
|
||||
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var id shared.Id
|
||||
err := rows.Scan(&id.Id)
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
categoryIds = append(categoryIds, int(id.Id))
|
||||
}
|
||||
output.CategoryIds = categoryIds
|
||||
return output, nil
|
||||
}
|
@ -3,14 +3,25 @@ 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, ")", "_")
|
||||
output := convert(oldUrl)
|
||||
|
||||
return "/" + output + "/index.html"
|
||||
}
|
||||
|
||||
func GetURL(path string) string {
|
||||
return "https://www.slowtwitch.com/" + path
|
||||
func ConvertPostTitleToPath(title string, id int) string {
|
||||
output := convert(title)
|
||||
return output + "_" + string(id) + ".html"
|
||||
}
|
||||
|
||||
func GetURL(path string) string {
|
||||
return "https://www.slowtwitch.com" + path
|
||||
}
|
||||
|
||||
func convert(path string) string {
|
||||
output := strings.ReplaceAll(path, " ", "_")
|
||||
output = strings.ReplaceAll(output, "$", "_")
|
||||
output = strings.ReplaceAll(output, "(", "_")
|
||||
output = strings.ReplaceAll(output, ")", "_")
|
||||
|
||||
return output
|
||||
}
|
||||
|
@ -11,11 +11,16 @@ type CreateCategory struct {
|
||||
ParentId int `json:"parent"`
|
||||
}
|
||||
|
||||
func (parameters *CreateCategory) Execute(baseUrl, user, pass string) CategoryData {
|
||||
func (parameters *CreateCategory) Execute(baseUrl, user, pass string) (CategoryData, error) {
|
||||
endpoint := baseUrl + "wp-json/wp/v2/categories"
|
||||
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
body, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
if err != nil {
|
||||
return CategoryData{}, err
|
||||
}
|
||||
var category CategoryData
|
||||
err := json.Unmarshal(body, &category)
|
||||
utilities.CheckError(err)
|
||||
return category
|
||||
err = json.Unmarshal(body, &category)
|
||||
if err != nil {
|
||||
return CategoryData{}, err
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
|
@ -23,11 +23,17 @@ type CreatePostResponse struct {
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse {
|
||||
func (parameters *CreatePost) Execute(baseUrl, user, pass string) (CreatePostResponse, error) {
|
||||
endpoint := baseUrl + "wp-json/wp/v2/posts"
|
||||
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
body, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
if err != nil {
|
||||
return CreatePostResponse{}, err
|
||||
}
|
||||
var post CreatePostResponse
|
||||
err := json.Unmarshal(body, &post)
|
||||
err = json.Unmarshal(body, &post)
|
||||
if err != nil {
|
||||
return CreatePostResponse{}, err
|
||||
}
|
||||
utilities.CheckError(err)
|
||||
return post
|
||||
return post, nil
|
||||
}
|
||||
|
@ -35,9 +35,12 @@ type CreatedRedirect struct {
|
||||
|
||||
func (parameters *CreateRedirect) Execute(baseUrl, user, pass string) (CreatedRedirect, error) {
|
||||
url := baseUrl + "wp-json/redirection/v1/redirect"
|
||||
response := utilities.PostHttpRequestToWordpress(url, user, pass, parameters)
|
||||
response, err := utilities.PostHttpRequestToWordpress(url, user, pass, parameters)
|
||||
if err != nil {
|
||||
return CreatedRedirect{}, err
|
||||
}
|
||||
var results CreateRedirectResponse
|
||||
err := json.Unmarshal(response, &results)
|
||||
err = json.Unmarshal(response, &results)
|
||||
if err != nil {
|
||||
return CreatedRedirect{}, err
|
||||
}
|
||||
|
@ -17,11 +17,17 @@ type CreateTagResponse struct {
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse {
|
||||
func (parameters *CreateTag) Execute(baseUrl, user, pass string) (CreateTagResponse, error) {
|
||||
endpoint := baseUrl + "wp-json/wp/v2/tags"
|
||||
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
body, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
if err != nil {
|
||||
return CreateTagResponse{}, err
|
||||
}
|
||||
var tagResponse CreateTagResponse
|
||||
err := json.Unmarshal(body, &tagResponse)
|
||||
err = json.Unmarshal(body, &tagResponse)
|
||||
if err != nil {
|
||||
return CreateTagResponse{}, err
|
||||
}
|
||||
utilities.CheckError(err)
|
||||
return tagResponse
|
||||
return tagResponse, nil
|
||||
}
|
||||
|
@ -20,11 +20,16 @@ type CreateUserResponse struct {
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse {
|
||||
func (parameters *CreateUser) Execute(baseUrl, user, pass string) (CreateUserResponse, error) {
|
||||
endpoint := baseUrl + "wp-json/wp/v2/users"
|
||||
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
body, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
|
||||
if err != nil {
|
||||
return CreateUserResponse{}, err
|
||||
}
|
||||
var userData CreateUserResponse
|
||||
err := json.Unmarshal(body, &userData)
|
||||
utilities.CheckError(err)
|
||||
return userData
|
||||
err = json.Unmarshal(body, &userData)
|
||||
if err != nil {
|
||||
return CreateUserResponse{}, err
|
||||
}
|
||||
return userData, nil
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package utilities
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -12,30 +12,39 @@ func GetHttpRequestToWordpress(url, user, pass string) []byte {
|
||||
request, err := http.NewRequest("GET", url, nil)
|
||||
addAuth(user, pass, request)
|
||||
CheckError(err)
|
||||
body := requestToWordpress(request, user, pass)
|
||||
body, _ := requestToWordpress(request, user, pass)
|
||||
return body
|
||||
}
|
||||
|
||||
func PostHttpRequestToWordpress(url, user, pass string, parameters any) []byte {
|
||||
func PostHttpRequestToWordpress(url, user, pass string, parameters any) ([]byte, error) {
|
||||
requestBodyBytes, err := json.Marshal(parameters)
|
||||
requestBody := string(requestBodyBytes)
|
||||
CheckError(err)
|
||||
if err != nil {
|
||||
return make([]byte, 0), err
|
||||
}
|
||||
request, err := http.NewRequest("POST", url, strings.NewReader(requestBody))
|
||||
CheckError(err)
|
||||
body := requestToWordpress(request, user, pass)
|
||||
return body
|
||||
if err != nil {
|
||||
return make([]byte, 0), err
|
||||
}
|
||||
body, err := requestToWordpress(request, user, pass)
|
||||
return body, err
|
||||
}
|
||||
|
||||
func requestToWordpress(request *http.Request, user, pass string) []byte {
|
||||
func requestToWordpress(request *http.Request, user, pass string) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
addAuth(user, pass, request)
|
||||
response, err := client.Do(request)
|
||||
CheckError(err)
|
||||
if err != nil {
|
||||
return make([]byte, 0), err
|
||||
}
|
||||
defer CloseBodyAndCheckError(response.Body)
|
||||
checkHttpResponseStatus(response)
|
||||
err = checkHttpResponseStatus(response)
|
||||
if err != nil {
|
||||
return make([]byte, 0), err
|
||||
}
|
||||
body, err := io.ReadAll(response.Body)
|
||||
CheckError(err)
|
||||
return body
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func addAuth(user, pass string, request *http.Request) {
|
||||
@ -43,8 +52,10 @@ func addAuth(user, pass string, request *http.Request) {
|
||||
request.Header.Add("content-type", "application/json")
|
||||
}
|
||||
|
||||
func checkHttpResponseStatus(response *http.Response) {
|
||||
if response.StatusCode != 200 {
|
||||
fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode))
|
||||
func checkHttpResponseStatus(response *http.Response) error {
|
||||
if response.StatusCode >= 200 && response.StatusCode < 300 {
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("Status Code:" + response.Status + http.StatusText(response.StatusCode))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user