trending/main.go

67 lines
1.5 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/caarlos0/log"
"github.com/charmbracelet/lipgloss"
)
func main() {
repos, err := trending("rust", []string{"daily", "weekly", "monthly"})
if err != nil {
log.WithError(err).Fatal("could not get trending repositories")
}
for _, repo := range repos {
log.Info(repo)
}
}
func trending(lang string, timeframes []string) ([]string, error) {
repoMap := make(map[string]struct{})
for _, timeframe := range timeframes {
rs, err := trendingFrame(lang, timeframe)
if err != nil {
return nil, err
}
for _, r := range rs {
repoMap[r] = struct{}{}
}
}
var repos []string
for repo := range repoMap {
repos = append(repos, repo)
}
return repos, nil
}
func trendingFrame(lang, timeframe string) ([]string, error) {
u := fmt.Sprintf("https://github.com/trending/%s?since=%s", lang, timeframe)
res, err := http.Get(u)
if err != nil {
return nil, err
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
var repos []string
doc.Find(".Box-row").Each(func(_ int, s *goquery.Selection) {
// TODO Only do this once per lang
rlcStyle, _ := s.Find(".repo-language-color").Attr("style")
rlc := strings.Fields(rlcStyle)[1]
log.Styles[log.InfoLevel] = lipgloss.NewStyle().Foreground(lipgloss.Color(rlc)).Bold(true)
e := s.Find("h1 a")
repo := strings.Join(strings.Fields(e.Text()), "")
repos = append(repos, repo)
})
return repos, nil
}