feat: parse answer author and timestamp

This commit is contained in:
httpjamesm 2022-12-27 23:23:19 -05:00
parent 010bd290af
commit 85dad1cbe0
3 changed files with 67 additions and 6 deletions

46
main.go
View File

@ -20,6 +20,10 @@ func main() {
r.LoadHTMLGlob("templates/*") r.LoadHTMLGlob("templates/*")
r.Static("/static", "./public") 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) { r.GET("/questions/:id/:title", func(c *gin.Context) {
questionId := c.Param("id") questionId := c.Param("id")
questionTitle := c.Param("title") questionTitle := c.Param("title")
@ -100,14 +104,52 @@ func main() {
doc.Find("div.answer").Each(func(i int, s *goquery.Selection) { doc.Find("div.answer").Each(func(i int, s *goquery.Selection) {
postLayout := s.Find("div.post-layout") postLayout := s.Find("div.post-layout")
voteCell := postLayout.Find("div.votecell")
answerCell := postLayout.Find("div.answercell") answerCell := postLayout.Find("div.answercell")
answerBody := answerCell.Find("div.s-prose") answerBody := answerCell.Find("div.s-prose")
answerBodyHTML, _ := answerBody.Html() answerBodyHTML, _ := answerBody.Html()
voteCount := voteCell.Find("div.js-vote-count").Text()
if s.HasClass("accepted-answer") { if s.HasClass("accepted-answer") {
// add <div class="accepted-answer">Accepted Answer</div> to the top of the answer // add <div class="answer-meta accepted">Accepted Answer</div> to the top of the answer
answerBodyHTML = `<div class="accepted-answer">Accepted Answer</div>` + answerBodyHTML answerBodyHTML = fmt.Sprintf(`<div class="answer-meta accepted">Accepted Answer - %s Upvotes</div>`, voteCount) + answerBodyHTML
} else {
// add <div class="answer-meta">%s Upvotes</div> to the top of the answer
answerBodyHTML = fmt.Sprintf(`<div class="answer-meta">%s Upvotes</div>`, 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 <div class="answer-author">Answered %s by %s</div> to the bottom of the answer
answerBodyHTML += fmt.Sprintf(`<div class="answer-author-parent"><div class="answer-author">Answered at %s by <a href="https://stackoverflow.com/%s" target="_blank" rel="noopener noreferrer">%s</a></div></div>`, answerTimestamp, answerAuthorURL, answerAuthorName)
// get the timestamp and author
answers = append(answers, template.HTML(answerBodyHTML)) answers = append(answers, template.HTML(answerBodyHTML))
}) })

View File

@ -73,11 +73,30 @@ img {
max-width: 100%; max-width: 100%;
} }
.accepted-answer { .answer-meta {
background-color: #8cffc0;
border-radius: 5px; border-radius: 5px;
padding: 1rem; padding: 1rem;
color: black; color: black;
margin-bottom: 1rem; 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;
} }

View File

@ -15,7 +15,7 @@
<h1>{{ .title }}</h1> <h1>{{ .title }}</h1>
<p class="timestamp"> <p class="timestamp">
Asked {{ .timestamp }} by Asked {{ .timestamp }} by
<a href="{{ .authorURL }}">{{ .author }}</a>. <a href="{{ .authorURL }}" target="_blank" rel="noopener noreferrer">{{ .author }}</a>.
</p> </p>
</div> </div>
<div class="card-body">{{ .body }}</div> <div class="card-body">{{ .body }}</div>