Post-Migrator/main.go

112 lines
3.2 KiB
Go
Raw Normal View History

2024-05-07 22:03:15 +00:00
package main
import (
2024-05-16 16:26:59 +00:00
"federated.computer/wp-sync-slowtwitch/services/migration"
2024-05-17 18:49:55 +00:00
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
"fmt"
2024-05-17 18:49:55 +00:00
"github.com/PuerkitoBio/goquery"
2024-05-17 19:52:38 +00:00
"golang.org/x/net/html"
2024-05-17 18:49:55 +00:00
"io"
"log"
"net/http"
"strings"
)
const baseUrl = "https://slowtwitch.cloud/"
2024-05-16 16:26:59 +00:00
const wordpressKey = "admin@slowtwitch.cloud"
const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh"
2024-05-07 22:03:15 +00:00
2024-05-14 21:59:04 +00:00
const slowtwitchAdminUser = "admin"
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
const slowtwitchDbName = "slowtwitch"
const migrationDbName = "slowtwitch_transfer"
const federatedDbUrl = "slowtwitch.northend.network"
const federatedDbPort = "3306"
2024-05-14 21:59:04 +00:00
2024-05-07 22:03:15 +00:00
var appCache AppCache
func main() {
// TODO Article migration
2024-05-17 18:49:55 +00:00
slowtwitchDB, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
2024-05-16 16:26:59 +00:00
if slowtwitchDbErr != nil {
fmt.Println(slowtwitchDbErr)
}
resultsDB, resultsDBerr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, migrationDbName)
if resultsDBerr != nil {
fmt.Println(resultsDBerr)
}
2024-05-17 18:49:55 +00:00
//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()
2024-05-17 18:49:55 +00:00
// 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
2024-05-17 19:52:38 +00:00
var imagePaths []string
2024-05-17 18:49:55 +00:00
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))
2024-05-17 19:52:38 +00:00
imagePaths = append(imagePaths, imgUrl)
2024-05-17 18:49:55 +00:00
}
})
2024-05-17 19:52:38 +00:00
blog := doc.Find(".detail_text")
blog.Find(":first-child").Remove()
blog.Find("img").Each(func(i int, img *goquery.Selection) {
imgUrl, exists := img.Attr("src")
if exists {
newEle := goquery.NewDocumentFromNode(&html.Node{
Type: html.ElementNode,
Data: "img",
Attr: []html.Attribute{
html.Attribute{
Key: "src",
Val: "www.slowtwitch.cloud" + imgUrl,
},
html.Attribute{
Key: "class",
Val: "class1 class2 class3",
},
},
})
img.AfterSelection(newEle.Selection)
}
img.Remove()
})
blogContent, err := blog.Html()
2024-05-17 18:49:55 +00:00
if err != nil {
2024-05-17 19:52:38 +00:00
fmt.Println(err)
2024-05-17 18:49:55 +00:00
}
2024-05-17 19:52:38 +00:00
fmt.Println(blogContent)
//First image in list will be the featured image, remove that img tag from the returned html
//images will need their src updated after upload
2024-05-17 18:49:55 +00:00
//EXPERIMENT END
editorMigration := migration.MigrateAuthors{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
editorMigration.Execute()
2024-05-16 16:26:59 +00:00
categoryMigration := migration.MigrateCategories{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
WordpressBaseUrl: baseUrl,
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
2024-05-14 21:59:04 +00:00
}
2024-05-16 16:26:59 +00:00
categoryMigration.Execute()
}