-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
108 lines (86 loc) · 2.19 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
105
106
107
108
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func main() {
// Check for stdin input
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No users detected. Hint: cat users.txt | gistz")
os.Exit(1)
}
// Read stdin
var users []string
for {
var user string
_, err := fmt.Scan(&user)
if err != nil {
break
}
users = append(users, user)
}
// Download all gists for each user
for _, user := range users {
downloadGists(user)
}
}
// Download all gists for a user using Github API
func downloadGists(user string) {
fmt.Printf("Downloading gists for %s\n", user)
token := os.Getenv("GH_AUTH_TOKEN")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.GistListOptions{}
gists, resp, err := client.Gists.List(ctx, user, opt)
if err != nil {
if resp.StatusCode == 403 {
checkRateLimit(resp)
return
}
fmt.Printf("Error: %s\n", err)
return
}
for _, gist := range gists {
gist, resp, err := client.Gists.Get(ctx, gist.GetID())
if err != nil {
if resp.StatusCode == 403 {
checkRateLimit(resp)
return
}
fmt.Printf("Error: %s\n", err)
return
}
for _, f := range gist.Files {
// Create directory if it doesn't exist
os.MkdirAll("gists/"+user+"/"+gist.GetID(), 0755)
// Write file to disk with filename and content
fmt.Printf("Writing %s to %s\n", f.GetFilename(), "gists/"+user+"/"+gist.GetID()+"/"+f.GetFilename())
file, err := os.Create("gists/" + user + "/" + gist.GetID() + "/" + f.GetFilename())
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
_, err = file.WriteString(f.GetContent())
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
file.Close()
}
}
}
func checkRateLimit(resp *github.Response) {
if resp.Remaining == 0 {
fmt.Printf("Rate limit exceeded, waitting for %+v minutes\n", time.Until(resp.Rate.Reset.Time).Round(time.Minute).Minutes())
time.Sleep(time.Duration(time.Until(resp.Rate.Reset.Time).Minutes()) * time.Minute)
}
}