first draft done

This commit is contained in:
Ross Trottier 2024-05-19 19:35:53 -06:00
parent 683e3a6a87
commit 270255b110
6 changed files with 52 additions and 23 deletions

21
main.go
View File

@ -3,8 +3,6 @@ package main
import ( import (
"database/sql" "database/sql"
"federated.computer/wp-sync-slowtwitch/services/migration" "federated.computer/wp-sync-slowtwitch/services/migration"
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
"fmt"
) )
const baseUrl = "https://slowtwitch.cloud/" const baseUrl = "https://slowtwitch.cloud/"
@ -37,17 +35,6 @@ func main() {
resultsDB = resultsDatabase resultsDB = resultsDatabase
} }
// TODO Article migration
//EXPERIMENT AREA START
imageUrls, html, err := slowtwitch.GetImagesAndPostHtml("https://www.slowtwitch.com/Products/Components/SRAM_Drops_New_RED_AXS_Groupset_8950.html")
if err != nil {
fmt.Println(err)
}
for i, imageUrl := range imageUrls {
fmt.Println(i, imageUrl)
}
fmt.Println(html)
//EXPERIMENT END
editorMigration := migration.MigrateAuthors{ editorMigration := migration.MigrateAuthors{
SlowtwitchDatabase: slowtwitchDB, SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB, ResultsDatabase: resultsDB,
@ -64,4 +51,12 @@ func main() {
WordpressPassword: wordpressSecret, WordpressPassword: wordpressSecret,
} }
categoryMigration.Execute() categoryMigration.Execute()
postMigration := migration.MigratePosts{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
postMigration.Execute()
} }

View File

@ -6,6 +6,7 @@ import (
"federated.computer/wp-sync-slowtwitch/services/wordpress" "federated.computer/wp-sync-slowtwitch/services/wordpress"
"fmt" "fmt"
"slices" "slices"
"strconv"
"strings" "strings"
) )
@ -53,11 +54,15 @@ func (migration MigratePosts) Execute() {
createWordpressPost := wordpress.CreatePost{ createWordpressPost := wordpress.CreatePost{
Title: postBase.Title, Title: postBase.Title,
Excerpt: postBase.Description, Excerpt: postBase.Description,
Status: "published", Status: "publish",
} }
if postBase.DatePublished.Valid { if postBase.DatePublished.Valid {
createWordpressPost.Date = postBase.DatePublished.Time.String() t := postBase.DatePublished.Time
timeString := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
createWordpressPost.Date = timeString
} else { } else {
errorMessage = errorMessage + "Invalid Date Published" errorMessage = errorMessage + "Invalid Date Published"
// TODO SEND TO RESULTS DB WITH CALL // TODO SEND TO RESULTS DB WITH CALL
@ -188,7 +193,7 @@ func (migration MigratePosts) Execute() {
postResultId, err := CreatePostResult(postResult, migration.ResultsDatabase) postResultId, err := CreatePostResult(postResult, migration.ResultsDatabase)
if err != nil { if err != nil {
panic("Could not record post result for Slowtwitch post:" + string(postId)) fmt.Println("Could not record post result for Slowtwitch post:" + strconv.Itoa(postId) + err.Error())
} }
for _, imageResult := range imageResults { for _, imageResult := range imageResults {
imageResult.PostId = postResultId imageResult.PostId = postResultId
@ -197,7 +202,22 @@ func (migration MigratePosts) Execute() {
fmt.Println("Error recording image result") fmt.Println("Error recording image result")
} }
} }
// TODO record redirect oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "")
postRedirect := wordpress.CreateRedirect{
Title: "Article Redirect" + postBase.Title,
Url: strings.ReplaceAll(oldPath, ".html", ""),
MatchType: "page",
ActionType: "url",
ActionCode: 301,
GroupId: 1,
ActionData: wordpress.ActionData{
Url: "/" + strings.ReplaceAll(postResult.NewUrl, migration.WordpressBaseUrl, ""),
},
}
_, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil {
fmt.Println(err)
}
} }
//Update related posts (get from post results db) as second loop //Update related posts (get from post results db) as second loop
//Update advanced Custom Fields with images //Update advanced Custom Fields with images

View File

@ -13,7 +13,7 @@ type PostResult struct {
} }
func CreatePostResult(parameters PostResult, db *sql.DB) (int, error) { func CreatePostResult(parameters PostResult, db *sql.DB) (int, error) {
result, err := db.Exec("insert into ImageResults (WordpressId, SlowtwitchId, OldUrl, OldUrlStatus, NewUrl, IsSuccess, ErrorMessage) values (?, ?, ?, ?, ?, ?, ?)", parameters.WordpressId, parameters.SlowtwitchId, parameters.OldUrl, parameters.OldUrlStatus, parameters.NewUrl, parameters.IsSuccess, parameters.ErrorMessage) result, err := db.Exec("insert into PostResults (WordpressId, SlowtwitchId, OldUrl, OldUrlStatus, NewUrl, IsSuccess, ErrorMessage) values (?, ?, ?, ?, ?, ?, ?)", parameters.WordpressId, parameters.SlowtwitchId, parameters.OldUrl, parameters.OldUrlStatus, parameters.NewUrl, parameters.IsSuccess, parameters.ErrorMessage)
if err != nil { if err != nil {
return 0, err return 0, err
} else { } else {

View File

@ -14,7 +14,7 @@ func GetImagesAndPostHtml(url string) (imagePaths []string, htmlBody string, err
return return
} }
defer res.Body.Close() defer res.Body.Close()
imagePaths = make([]string, 0)
// Read the HTML content // Read the HTML content
htmlContent, err := io.ReadAll(res.Body) htmlContent, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
@ -33,7 +33,6 @@ func GetImagesAndPostHtml(url string) (imagePaths []string, htmlBody string, err
}) })
// Get blog html, remove first image because wordpress will handle that as a featured image // Get blog html, remove first image because wordpress will handle that as a featured image
blog := doc.Find(".detail_text") blog := doc.Find(".detail_text")
blog.Find(":first-child").Remove()
htmlBody, err = blog.Html() htmlBody, err = blog.Html()
return return
} }

View File

@ -1,6 +1,9 @@
package slowtwitch package slowtwitch
import "strings" import (
"strconv"
"strings"
)
func ConvertUrlToCategoryFormat(oldUrl string) string { func ConvertUrlToCategoryFormat(oldUrl string) string {
output := convert(oldUrl) output := convert(oldUrl)
@ -10,7 +13,7 @@ func ConvertUrlToCategoryFormat(oldUrl string) string {
func ConvertPostTitleToPath(title string, id int) string { func ConvertPostTitleToPath(title string, id int) string {
output := convert(title) output := convert(title)
return output + "_" + string(id) + ".html" return output + "_" + strconv.Itoa(id) + ".html"
} }
func GetURL(path string) string { func GetURL(path string) string {
@ -22,6 +25,7 @@ func convert(path string) string {
output = strings.ReplaceAll(output, "$", "_") output = strings.ReplaceAll(output, "$", "_")
output = strings.ReplaceAll(output, "(", "_") output = strings.ReplaceAll(output, "(", "_")
output = strings.ReplaceAll(output, ")", "_") output = strings.ReplaceAll(output, ")", "_")
output = strings.ReplaceAll(output, "?", "")
output = strings.ReplaceAll(output, ",", "")
return output return output
} }

View File

@ -34,6 +34,8 @@ func (parameters *CreateImage) Execute(baseUrl, user, pass string) (CreateImageR
return CreateImageResponse{}, err return CreateImageResponse{}, err
} }
filename := GetFileName(parameters.Url) filename := GetFileName(parameters.Url)
contentType := getContentType(filename)
request.Header.Set("Content-Type", contentType)
request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`) request.Header.Set("Content-Disposition", `attachment;filename="`+filename+`"`)
request.SetBasicAuth(user, pass) request.SetBasicAuth(user, pass)
rsp, err := http.DefaultClient.Do(request) rsp, err := http.DefaultClient.Do(request)
@ -63,3 +65,12 @@ func GetFileName(url string) string {
} }
return output return output
} }
func getContentType(filename string) string {
if strings.Contains(filename, "png") {
return "image/png"
} else if strings.Contains(filename, "gif") {
return "image/gif"
}
return "image/jpeg"
}