-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (131 loc) · 3.2 KB
/
main.go
File metadata and controls
152 lines (131 loc) · 3.2 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
)
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
Stars int `json:"stargazers_count"`
Forks int `json:"forks_count"`
OpenIssues int `json:"open_issues_count"`
URL string `json:"html_url"`
DefaultBranch string `json:"default_branch"`
LastCommit Commit `json:"-"`
}
type Commit struct {
Commit struct {
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"author"`
} `json:"commit"`
}
type RepoSlice struct {
sync.RWMutex
Repos []Repo
}
func (rs *RepoSlice) Append(repo Repo) {
rs.Lock()
defer rs.Unlock()
rs.Repos = append(rs.Repos, repo)
}
var (
Data RepoSlice
AccessToken = os.Getenv("ACCESS_TOKEN")
GithubAPI = "https://api.github.com"
wg sync.WaitGroup
)
func main() {
content, err := ioutil.ReadFile("repos.txt")
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(content), "\n")
Data.Repos = make([]Repo, 0)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
u, err := url.Parse(line)
if err != nil {
log.Fatal(err)
}
if u.Host != "github.com" {
continue
}
paths := strings.Split(u.Path, "/")
if len(paths) < 3 {
continue
}
wg.Add(1)
go run(paths)
}
wg.Wait()
sort.Slice(Data.Repos, func(i int, j int) bool {
return Data.Repos[i].Stars > Data.Repos[j].Stars
})
save()
}
func run(paths []string) {
defer wg.Done()
var repo Repo
var commit Commit
resp, err := http.Get(fmt.Sprintf("%s/repos/%s/%s?access_token=%s", GithubAPI, paths[1], paths[2], AccessToken))
if err != nil || resp == nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal(resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&repo)
if err != nil {
log.Fatal(err)
}
resp, err = http.Get(fmt.Sprintf("%s/repos/%s/%s/commits/%s?access_token=%s", GithubAPI, paths[1], paths[2], repo.DefaultBranch, AccessToken))
if err != nil || resp == nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal(resp.StatusCode)
}
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&commit)
if err != nil {
log.Fatal(err)
}
repo.LastCommit = commit
Data.Append(repo)
}
func save() {
head := `Go ORM Frameworks Ranking List
==========
**a list of the most github stars repositories related to Go ORM**
*ranked by stars*
| Project Name | Stars | Forks | Open Issues | Description | Last Commit |
| ------------ | ----- | ----- | ----------- | ----------- | ----------- |
`
readme, err := os.OpenFile("README.md", os.O_RDWR|os.O_TRUNC, 0666)
if err != nil {
log.Fatal(err)
}
defer readme.Close()
readme.WriteString(head)
for _, repo := range Data.Repos {
line := fmt.Sprintf("| [%s](%s) | %d | %d | %d | %s | %s |\n", repo.Name, repo.URL, repo.Stars, repo.Forks, repo.OpenIssues, repo.Description, repo.LastCommit.Commit.Author.Date.Format("2006-01-02 15:04:05"))
readme.WriteString(line)
}
readme.WriteString(fmt.Sprintf("\n*Last Automatic Update Time: %v*", time.Now().Format("2006-01-02 15:04:05")))
}