export for future cache
ci/woodpecker/push/goreleaser Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2022-10-25 15:39:02 -05:00
parent b30e898d82
commit 480035417b
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 22 additions and 22 deletions

View File

@ -8,45 +8,45 @@ import (
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
) )
func trendingLang(lang string, timeframes []string) (language, error) { func trendingLang(lang string, timeframes []string) (Language, error) {
l := language{ l := Language{
repositories: make([]repository, 0), Repositories: make([]Repository, 0),
} }
repoMap := make(map[repository]struct{}) repoMap := make(map[Repository]struct{})
for _, timeframe := range timeframes { for _, timeframe := range timeframes {
la, err := trendingRepos(lang, timeframe) la, err := trendingRepos(lang, timeframe)
if err != nil { if err != nil {
return la, err return la, err
} }
l.color = la.color l.Color = la.Color
for _, r := range la.repositories { for _, r := range la.Repositories {
repoMap[r] = struct{}{} repoMap[r] = struct{}{}
} }
} }
for repo := range repoMap { for repo := range repoMap {
l.repositories = append(l.repositories, repo) l.Repositories = append(l.Repositories, repo)
} }
return l, nil return l, nil
} }
func trendingRepos(lang, timeframe string) (language, 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 language{}, 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 language{}, err return Language{}, err
} }
var l language var l Language
doc.Find(".Box-row").Each(func(_ int, s *goquery.Selection) { doc.Find(".Box-row").Each(func(_ int, s *goquery.Selection) {
if l.color == "" { if l.Color == "" {
rlcStyle, _ := s.Find(".repo-language-color").Attr("style") rlcStyle, _ := s.Find(".repo-language-color").Attr("style")
l.color = strings.Fields(rlcStyle)[1] l.Color = strings.Fields(rlcStyle)[1]
} }
e := s.Find("h1 a") e := s.Find("h1 a")
@ -54,21 +54,21 @@ func trendingRepos(lang, timeframe string) (language, error) {
d := strings.TrimSpace(s.Find("p").Text()) d := strings.TrimSpace(s.Find("p").Text())
l.repositories = append(l.repositories, repository{name: repo, description: d}) l.Repositories = append(l.Repositories, Repository{Name: repo, Description: d})
}) })
return l, nil return l, nil
} }
type language struct { type Language struct {
color string Color string `json:"color"`
repositories []repository Repositories []Repository `json:"repositories"`
} }
type repository struct { type Repository struct {
name string Name string `json:"name"`
description string Description string `json:"description"`
} }
func (r repository) link() string { func (r Repository) link() string {
return fmt.Sprintf("https://github.com/%s", r.name) return fmt.Sprintf("https://github.com/%s", r.Name)
} }