Skip to content

Commit

Permalink
add date to news and blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaanngggg committed Aug 24, 2024
1 parent 917464a commit e1f892c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 15 additions & 1 deletion pkg/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
"log/slog"
"net/http"
"strings"
"time"
)

func fetchAllNews(ctx context.Context, isElite bool) ([]byte, error) {
Expand Down Expand Up @@ -41,19 +43,31 @@ func fetchAllNews(ctx context.Context, isElite bool) ([]byte, error) {
}

type Record struct {
Date string `json:"date"` // Jan-02 2006
Title string `json:"title"`
URL string `json:"url"`
}

func parseLinks(table *goquery.Selection) []Record {
loc, _ := time.LoadLocation("America/New_York")
today := time.Now().UTC().In(loc)
var records []Record
table.Find("a").Each(func(i int, a *goquery.Selection) {
table.Find("tr.news_table-row").Each(func(i int, tr *goquery.Selection) {
a := tr.Find("a")
href, exists := a.Attr("href")
if !exists {
return
}
date := strings.TrimSpace(tr.Find("td.news_date-cell").Text())
if strings.HasSuffix(date, "AM") || strings.HasSuffix(date, "PM") {
// if 05:30AM, format today
date = today.Format("Jan-02 2006")
} else {
date += " " + today.Format("2006") // add year
}
text := a.Text()
records = append(records, Record{
Date: date,
Title: text,
URL: href,
})
Expand Down
8 changes: 2 additions & 6 deletions pkg/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ func Test_parseNewsAndBlogs(t *testing.T) {
assert.NoError(t, err)
news, blogs, err := parseNewsAndBlogs(html)
assert.NoError(t, err)
for _, new_ := range news {
println(new_.Title)
}
for _, blog := range blogs {
println(blog.Title)
}
assert.NotEmpty(t, news)
assert.NotEmpty(t, blogs)
}

0 comments on commit e1f892c

Please sign in to comment.