-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
104 lines (88 loc) · 2.21 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/google/go-github/v62/github"
"golang.org/x/oauth2"
)
type RepoInfo struct {
Name string `json:"name"`
CloneURL string `json:"clone_url"`
Stars int `json:"stars"`
Size int `json:"size"`
}
func main() {
// TOKEN USE ACTION
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(1)
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.SearchOptions{
Sort: "stars",
ListOptions: github.ListOptions{
PerPage: 5,
},
}
query := "created:>" + time.Now().AddDate(0, 0, -2).Format("2006-01-02") +
" size:50..5000 is:public"
result, _, err := client.Search.Repositories(ctx, query, opt)
if err != nil {
fmt.Printf("Error searching repositories: %v\n", err)
os.Exit(1)
}
var repos []RepoInfo
for _, repo := range result.Repositories {
repos = append(repos, RepoInfo{
Name: repo.GetFullName(),
CloneURL: repo.GetCloneURL(),
Stars: repo.GetStargazersCount(),
Size: repo.GetSize(),
})
}
const FolderSave = "Projects/ScrapDay/"
jsonData, err := json.MarshalIndent(repos, "", " ")
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
os.Exit(1)
}
filename := "repos_to_clone.json"
err = os.WriteFile(FolderSave+filename, jsonData, 0644)
if err != nil {
fmt.Printf("Error writing file: %v\n", err)
os.Exit(1)
}
filename2 := "latest_repos.md"
f, err := os.Create(FolderSave + filename2)
if err != nil {
fmt.Printf("Error creating file: %v\n", err)
os.Exit(1)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
}
}(f)
_, err = fmt.Fprintf(f, "# Latest Repositories (%s .. %s)\n\n",
time.Now().AddDate(0, 0, -2).Format("2006-01-02"),
time.Now().AddDate(0, 0, 0).Format("2006-01-02"))
if err != nil {
return
}
for _, repo := range result.Repositories {
_, err := fmt.Fprintf(f, "- [%s](%s) (%d)\n", repo.GetFullName(), repo.GetHTMLURL(), repo.GetStargazersCount())
if err != nil {
return
}
}
fmt.Println("Repository information saved to", filename)
}