add structure

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2022-10-24 23:06:55 -05:00
parent bef16f427b
commit f86af5a34c
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 48 additions and 24 deletions

72
main.go
View File

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