parent
bef16f427b
commit
f86af5a34c
72
main.go
72
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue