figured out goquery

This commit is contained in:
Ross Trottier 2024-05-17 13:52:38 -06:00
parent 588d0e1833
commit 74dfcc4266

37
main.go
View File

@ -5,6 +5,7 @@ import (
"federated.computer/wp-sync-slowtwitch/services/slowtwitch" "federated.computer/wp-sync-slowtwitch/services/slowtwitch"
"fmt" "fmt"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -51,17 +52,45 @@ func main() {
log.Fatalf("goquery.NewDocumentFromReader -> %v", err) log.Fatalf("goquery.NewDocumentFromReader -> %v", err)
} }
// Find all image tags and extract their 'src' attributes // Find all image tags and extract their 'src' attributes
var imagePaths []string
doc.Find(".detail_text img").Each(func(i int, img *goquery.Selection) { doc.Find(".detail_text img").Each(func(i int, img *goquery.Selection) {
imgUrl, exists := img.Attr("src") imgUrl, exists := img.Attr("src")
if exists { if exists {
log.Printf("Image URL %d: %s", i+1, slowtwitch.GetURL(imgUrl)) 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")
if err != nil { blog.Find(":first-child").Remove()
log.Fatalf("goquery.NewDocumentFromReader -> %v", err) 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)
} }
fmt.Println(blog)
img.Remove()
})
blogContent, err := blog.Html()
if err != nil {
fmt.Println(err)
}
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 //EXPERIMENT END
editorMigration := migration.MigrateAuthors{ editorMigration := migration.MigrateAuthors{
SlowtwitchDatabase: slowtwitchDB, SlowtwitchDatabase: slowtwitchDB,