From 865dfbf1cf858f2b999ca3609d24c7309208838e Mon Sep 17 00:00:00 2001 From: al2f Date: Wed, 24 Jan 2024 12:50:36 +1030 Subject: [PATCH] Use the rendered html readme from the json in a script tag --- pages/repo.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/pages/repo.go b/pages/repo.go index 6a361ce..dcef25a 100644 --- a/pages/repo.go +++ b/pages/repo.go @@ -5,7 +5,10 @@ import ( "net/http" "os" "strings" - + "fmt" + + "github.com/tidwall/gjson" + "codeberg.org/gothub/gothub/utils" "github.com/gocolly/colly" "github.com/gofiber/fiber/v2" @@ -63,7 +66,6 @@ func HandleRepo(c *fiber.Ctx) error { "error": "Repository " + c.Params("user") + "/" + repoUrl + branchExists + " not found", }) } - // Scraping Scrape := Repo{} @@ -92,10 +94,22 @@ func HandleRepo(c *fiber.Ctx) error { sc.OnHTML("div#readme", func(e *colly.HTMLElement) { Scrape.Readme = e.ChildText("a[href='#readme']") }) - sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) { - Content, _ := e.DOM.Html() - readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1), "https://user-images.githubusercontent.com", "/user-images", -1) + + // jq '.props.initialPayload.overview.overviewFiles[]|select(.tabName == "README").richText' + sc.OnHTML("div.Layout-main react-partial script", func(e *colly.HTMLElement) { + jsonMess := e.Text + + result := gjson.Get(jsonMess, "props.initialPayload.overview.overviewFiles") + result.ForEach(func(i, e gjson.Result) bool { + if e.Get("tabName").Str == "README" { + unsanitizedOutput := e.Get("richText").Str + readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(unsanitizedOutput))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1), "https://user-images.githubusercontent.com", "/user-images", -1) + return false + } + return true // keep iterating + }) }) + sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) { e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) { var lang Languages @@ -129,6 +143,7 @@ func HandleRepo(c *fiber.Ctx) error { }) }) sc.Visit("https://github.com/" + c.Params("user") + "/" + repoUrl + branchExists) + // Add scrape-based info to repoArray repoArray = append(repoArray, Scrape) return c.Render("repo", fiber.Map{