forked from mingrammer/go-web-framework-stars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist2md.go
155 lines (132 loc) · 3.96 KB
/
list2md.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
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
153
154
155
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
"time"
"unicode"
)
// Repo describes a Github repository with additional field, last commit date
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
DefaultBranch string `json:"default_branch"`
Stars int `json:"stargazers_count"`
Forks int `json:"forks_count"`
Issues int `json:"open_issues_count"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
URL string `json:"html_url"`
LastCommitDate time.Time `json:"-"`
}
// HeadCommit describes a head commit of default branch
type HeadCommit struct {
Sha string `json:"sha"`
Commit struct {
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"committer"`
} `json:"commit"`
}
const (
head = `# Top Go Web Frameworks
A list of popular github projects related to Go web framework (ranked by stars automatically)
Please update **list.txt** (via Pull Request)
| Project Name | Stars | Forks | Open Issues | Description | Last Commit |
| ------------ | ----- | ----- | ----------- | ----------- | ----------- |
`
tail = "\n*Last Automatic Update: %v*"
warning = "⚠️ No longer maintained ⚠️ "
)
var (
deprecatedRepos = [2]string{"https://github.com/go-martini/martini", "https://github.com/pilu/traffic"}
repos []Repo
)
func main() {
accessToken := getAccessToken()
byteContents, err := ioutil.ReadFile("list.txt")
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(byteContents), "\n")
for _, url := range lines {
if strings.HasPrefix(url, "https://github.com/") {
var repo Repo
var commit HeadCommit
repoAPI := fmt.Sprintf("https://api.github.com/repos/%s?access_token=%s", strings.TrimFunc(url[19:], trimSpaceAndSlash), accessToken)
fmt.Println(repoAPI)
resp, err := http.Get(repoAPI)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal(resp.Status)
}
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(&repo); err != nil {
log.Fatal(err)
}
commitAPI := fmt.Sprintf("https://api.github.com/repos/%s/commits/%s?access_token=%s", strings.TrimFunc(url[19:], trimSpaceAndSlash), repo.DefaultBranch, accessToken)
fmt.Println(commitAPI)
resp, err = http.Get(commitAPI)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal(resp.Status)
}
decoder = json.NewDecoder(resp.Body)
if err = decoder.Decode(&commit); err != nil {
log.Fatal(err)
}
repo.LastCommitDate = commit.Commit.Committer.Date
repos = append(repos, repo)
fmt.Printf("Repository: %v\n", repo)
fmt.Printf("Head Commit: %v\n", commit)
}
}
sort.Slice(repos, func(i, j int) bool {
return repos[i].Stars > repos[j].Stars
})
saveRanking(repos)
}
func trimSpaceAndSlash(r rune) bool {
return unicode.IsSpace(r) || (r == rune('/'))
}
func getAccessToken() string {
tokenBytes, err := ioutil.ReadFile("access_token.txt")
if err != nil {
log.Fatal("Error occurs when getting access token")
}
return strings.TrimSpace(string(tokenBytes))
}
func saveRanking(repos []Repo) {
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 repos {
if isDeprecated(repo.URL) {
repo.Description = warning + repo.Description
}
readme.WriteString(fmt.Sprintf("| [%s](%s) | %d | %d | %d | %s | %v |\n", repo.Name, repo.URL, repo.Stars, repo.Forks, repo.Issues, repo.Description, repo.LastCommitDate.Format("2006-01-02 15:04:05")))
}
readme.WriteString(fmt.Sprintf(tail, time.Now().Format(time.RFC3339)))
}
func isDeprecated(repoURL string) bool {
for _, deprecatedRepo := range deprecatedRepos {
if repoURL == deprecatedRepo {
return true
}
}
return false
}