From 85dad1cbe0ead6ae5f3006c414640dcd710e30f1 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Tue, 27 Dec 2022 23:23:19 -0500 Subject: [PATCH] feat: parse answer author and timestamp --- main.go | 46 +++++++++++++++++++++++++++++++++++++++-- public/question.css | 25 +++++++++++++++++++--- templates/question.html | 2 +- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 944a582..01e0f94 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,10 @@ func main() { r.LoadHTMLGlob("templates/*") r.Static("/static", "./public") + r.GET("/robots.txt", func(c *gin.Context) { + c.String(200, "User-agent: *\nDisallow: /") + }) + r.GET("/questions/:id/:title", func(c *gin.Context) { questionId := c.Param("id") questionTitle := c.Param("title") @@ -100,14 +104,52 @@ func main() { doc.Find("div.answer").Each(func(i int, s *goquery.Selection) { postLayout := s.Find("div.post-layout") + voteCell := postLayout.Find("div.votecell") answerCell := postLayout.Find("div.answercell") answerBody := answerCell.Find("div.s-prose") answerBodyHTML, _ := answerBody.Html() + + voteCount := voteCell.Find("div.js-vote-count").Text() + if s.HasClass("accepted-answer") { - // add
Accepted Answer
to the top of the answer - answerBodyHTML = `
Accepted Answer
` + answerBodyHTML + // add
Accepted Answer
to the top of the answer + answerBodyHTML = fmt.Sprintf(`
Accepted Answer - %s Upvotes
`, voteCount) + answerBodyHTML + } else { + // add
%s Upvotes
to the top of the answer + answerBodyHTML = fmt.Sprintf(`
%s Upvotes
`, voteCount) + answerBodyHTML } + answerFooter := s.Find("div.mt24") + + answerAuthorURL := "" + answerAuthorName := "" + answerTimestamp := "" + + answerFooter.Find("div.post-signature").Each(func(i int, s *goquery.Selection) { + answerAuthorDetails := s.Find("div.user-details") + + if answerAuthorDetails.Length() == 0 { + return + } + + if answerAuthorDetails.Length() > 1 { + if i == 0 { + return + } + } + + answerAuthor := answerAuthorDetails.Find("a").First() + + answerAuthorURL = answerAuthor.AttrOr("href", "") + answerAuthorName = answerAuthor.Text() + answerTimestamp = s.Find("span.relativetime").Text() + }) + + // append
Answered %s by %s
to the bottom of the answer + answerBodyHTML += fmt.Sprintf(`
Answered at %s by %s
`, answerTimestamp, answerAuthorURL, answerAuthorName) + + // get the timestamp and author + answers = append(answers, template.HTML(answerBodyHTML)) }) diff --git a/public/question.css b/public/question.css index d01810d..9d48b38 100644 --- a/public/question.css +++ b/public/question.css @@ -73,11 +73,30 @@ img { max-width: 100%; } -.accepted-answer { - background-color: #8cffc0; +.answer-meta { border-radius: 5px; padding: 1rem; color: black; margin-bottom: 1rem; - /* width: fit-content; */ + background-color: rgb(82, 82, 98); + color: white; +} + +.answer-meta.accepted { + background-color: #8cffc0 !important; + color: black !important; +} + +.answer-author-parent { + display: flex; + justify-content: flex-end; +} + +.answer-author { + width: fit-content; + background-color: rgb(82, 82, 98); + padding: .5rem; + border-radius: 5px; + text-align: right; + font-size: .8rem; } \ No newline at end of file diff --git a/templates/question.html b/templates/question.html index 56660ba..61499f2 100644 --- a/templates/question.html +++ b/templates/question.html @@ -15,7 +15,7 @@

{{ .title }}

Asked {{ .timestamp }} by - {{ .author }}. + {{ .author }}.

{{ .body }}