From 74dfcc4266f29b7f45b07474e1cac2e6df5cd558 Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Fri, 17 May 2024 13:52:38 -0600 Subject: [PATCH] figured out goquery --- main.go | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e4ed93e..3f0e43f 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "federated.computer/wp-sync-slowtwitch/services/slowtwitch" "fmt" "github.com/PuerkitoBio/goquery" + "golang.org/x/net/html" "io" "log" "net/http" @@ -51,17 +52,45 @@ func main() { log.Fatalf("goquery.NewDocumentFromReader -> %v", err) } // Find all image tags and extract their 'src' attributes + var imagePaths []string 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)) + imagePaths = append(imagePaths, imgUrl) } }) - blog, err := doc.Find(".detail_text").Html() + 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() if err != nil { - log.Fatalf("goquery.NewDocumentFromReader -> %v", err) + fmt.Println(err) } - fmt.Println(blog) + 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 //EXPERIMENT END editorMigration := migration.MigrateAuthors{ SlowtwitchDatabase: slowtwitchDB,