-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
98 lines (84 loc) · 1.93 KB
/
client.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
package mystars
import (
"context"
"log"
"github.com/google/go-github/v48/github"
"golang.org/x/oauth2"
)
type Github struct {
client *github.Client
NextPage int
LastPage int
}
type Abstract struct {
Name string
Url string
Desc string
StarCount int
License string
}
type Following struct {
Url string
Name string
}
func GetLicense(l *github.License) string {
if l == nil {
return ""
}
return *l.Key
}
func GetString(ptr *string, placeholder string, limit int) string {
if ptr == nil {
return placeholder
}
// 限制长度
rs := []rune(*ptr)
if limit > 0 && len(rs) > limit {
return string(rs[:limit]) + "……"
}
return *ptr
}
func NewGithub(ctx context.Context, accessToken string) *Github {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return &Github{
client: client,
NextPage: 1,
}
}
func (g *Github) ResetPage() {
g.NextPage, g.LastPage = 1, 0
}
func (g *Github) HasNext() bool {
log.Printf("WIP… Next Page: %d, Last Page: %d\n", g.NextPage, g.LastPage)
if g.NextPage == 0 {
return false
}
return g.NextPage == 1 || g.NextPage <= g.LastPage
}
func (g *Github) MyStars(ctx context.Context) ([]*github.StarredRepository, error) {
// list all repositories for the authenticated user
opts := &github.ActivityListStarredOptions{
// Sort: "full_name",
// Direction: "desc",
ListOptions: github.ListOptions{
Page: g.NextPage,
// PerPage: 30,
},
}
repos, resp, err := g.client.Activity.ListStarred(ctx, "", opts)
g.NextPage, g.LastPage = resp.NextPage, resp.LastPage
return repos, err
}
func (g *Github) MyFollowing(ctx context.Context) ([]*github.User, error) {
opts := &github.ListOptions{
Page: g.NextPage,
// PerPage: 30,
}
users, resp, err := g.client.Users.ListFollowing(ctx, "", opts)
g.NextPage, g.LastPage = resp.NextPage, resp.LastPage
return users, err
}