feat: get timestamp and author details

This commit is contained in:
httpjamesm 2022-12-27 22:46:31 -05:00
parent 60f0ec5ce9
commit b7345d39a0
3 changed files with 69 additions and 7 deletions

52
main.go
View File

@ -51,9 +51,57 @@ func main() {
panic(err)
}
questionCard := doc.Find("div.postcell")
questionMetadata := questionCard.Find("div.user-info")
questionTimestamp := ""
questionMetadata.Find("span.relativetime").Each(func(i int, s *goquery.Selection) {
// get the second
if i == 0 {
if s.Text() != "" {
// if it's not been edited, it means it's the first
questionTimestamp = s.Text()
return
}
}
// otherwise it's the second element
if i == 1 {
questionTimestamp = s.Text()
return
}
})
userDetails := questionMetadata.Find("div.user-details")
questionAuthor := ""
questionAuthorURL := ""
userDetails.Find("a").Each(func(i int, s *goquery.Selection) {
// get the second
if i == 0 {
if s.Text() != "" {
// if it's not been edited, it means it's the first
questionAuthor = s.Text()
questionAuthorURL, _ = s.Attr("href")
return
}
}
// otherwise it's the second element
if i == 1 {
questionAuthor = s.Text()
questionAuthorURL, _ = s.Attr("href")
return
}
})
c.HTML(200, "question.html", gin.H{
"title": questionText,
"body": template.HTML(questionBodyParentHTML),
"title": questionText,
"body": template.HTML(questionBodyParentHTML),
"timestamp": questionTimestamp,
"author": questionAuthor,
"authorURL": questionAuthorURL,
})
})

View File

@ -43,4 +43,15 @@ pre {
border-radius: 5px;
color: white;
overflow-x: auto;
line-height: 1.35;
}
.timestamp {
color: #b3b3b3;
font-size: 0.8rem;
}
a {
color: #92adff;
}

View File

@ -2,19 +2,22 @@
<html>
<head>
<title>{{ .title }}</title>
<link rel="stylesheet" href="/static/question.css">
<link rel="stylesheet" href="/static/question.css" />
</head>
<body>
<div class="parent">
<div class="card">
<div class="card-header">
<h1>{{ .title }}</h1>
<p class="timestamp">
Asked {{ .timestamp }} by
<a href="{{ .authorURL }}">{{ .author }}</a>.
</p>
</div>
<div class="card-body">
{{ .body }}
</div>
<div class="card-body">{{ .body }}</div>
</div>
<hr />
<h2>Answers</h2>
</div>
</body>
</html>