This repository was archived by the owner on Jun 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
106 lines (97 loc) · 3.05 KB
/
Copy pathgithub.go
File metadata and controls
106 lines (97 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package sources
import (
"context"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"feedreader/internal/domain"
"github.com/PuerkitoBio/goquery"
)
type GitHubTrendingSource struct{}
func (GitHubTrendingSource) Key() string { return "github" }
func (GitHubTrendingSource) Label() string { return "GitHub Trending" }
func (GitHubTrendingSource) HomePageURL() string { return "https://github.com/trending" }
func (s GitHubTrendingSource) Fetch(ctx context.Context, client *http.Client) ([]domain.FeedItem, error) {
resp, err := getWithRetry(ctx, client, s.HomePageURL())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
return nil, &httpError{StatusCode: resp.StatusCode, Body: string(body)}
}
return parseGitHubTrending(resp.Body)
}
func parseGitHubTrending(reader io.Reader) ([]domain.FeedItem, error) {
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
return nil, err
}
items := []domain.FeedItem{}
doc.Find("article.Box-row").Each(func(i int, article *goquery.Selection) {
repoLink := article.Find("h2 a").First()
if repoLink.Length() == 0 {
return
}
href, _ := repoLink.Attr("href")
repoPath := normalizeGitHubRepoPath(href, repoLink.Text())
if repoPath == "" {
return
}
description := cleanString(article.Find("p").First().Text())
language := cleanString(article.Find(`[itemprop="programmingLanguage"]`).First().Text())
stars := parseDigits(article.Find(`a[href$="/stargazers"]`).First().Text())
forks := parseDigits(article.Find(`a[href$="/forks"]`).First().Text())
articleText := strings.Join(strings.Fields(article.Text()), " ")
starsToday := extractInt(articleText, `(\d[\d,]*)\s+stars today`)
metadata := map[string]any{}
if language != nil {
metadata["language"] = *language
}
if stars != nil {
metadata["total_stars"] = *stars
}
if forks != nil {
metadata["forks"] = *forks
}
if starsToday != nil {
metadata["stars_today"] = *starsToday
}
items = append(items, domain.FeedItem{
Source: "github",
ExternalID: strings.ToLower(repoPath),
Title: repoPath,
URL: resolveGitHubURL(href),
Summary: description,
Score: starsToday,
SourceRank: i + 1,
Metadata: metadata,
})
})
return items, nil
}
func resolveGitHubURL(path string) string {
base, _ := url.Parse("https://github.com")
rel, _ := url.Parse(path)
return base.ResolveReference(rel).String()
}
func normalizeGitHubRepoPath(href string, linkText string) string {
if parsed, err := url.Parse(strings.TrimSpace(href)); err == nil {
path := strings.Trim(parsed.Path, "/")
parts := strings.Split(path, "/")
if len(parts) >= 2 && parts[0] != "" && parts[1] != "" {
return parts[0] + "/" + parts[1]
}
}
return strings.Trim(strings.Join(strings.Fields(linkText), ""), "/")
}
func parseDigits(value string) *int {
cleaned := regexp.MustCompile(`[^\d]`).ReplaceAllString(value, "")
if cleaned == "" {
return nil
}
return extractInt(cleaned, `(\d+)`)
}