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

68
main.go
View File

@ -11,56 +11,80 @@ import (
)
func main() {
repos, err := trending("rust", []string{"daily", "weekly", "monthly"})
languages := []string{"go", "rust"}
for _, lang := range languages {
l, err := trendingLang(lang, []string{"daily", "weekly", "monthly"})
if err != nil {
log.WithError(err).Fatal("could not get trending repositories")
}
for _, repo := range repos {
log.Info(repo)
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) {
repoMap := make(map[string]struct{})
for _, timeframe := range timeframes {
rs, err := trendingFrame(lang, timeframe)
if err != nil {
return nil, err
func trendingLang(lang string, timeframes []string) (language, error) {
l := language{
repositories: make([]repository, 0),
}
for _, r := range rs {
repoMap := make(map[repository]struct{})
for _, timeframe := range timeframes {
la, err := trendingRepos(lang, timeframe)
if err != nil {
return la, err
}
l.color = la.color
for _, r := range la.repositories {
repoMap[r] = struct{}{}
}
}
var repos []string
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)
res, err := http.Get(u)
if err != nil {
return nil, err
return language{}, err
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
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) {
// TODO Only do this once per lang
if l.color == "" {
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)
l.color = strings.Fields(rlcStyle)[1]
}
e := s.Find("h1 a")
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)
}