-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (151 loc) · 4.73 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/schollz/progressbar/v3"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.UseShortOptionHandling = true
app.Name = "Github User CLI"
app.Version = "1.0.0"
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "Afif-Dev",
Email: "[email protected]",
},
}
app.Copyright = "(c) 2022 Afif-Dev https://github.com/afif-dev"
app.Usage = "Get user infos"
app.UsageText = "Fetch Github user infos"
app.Commands = []cli.Command{
{
Name: "user",
Usage: "fetch user infos by username",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "save, s",
Usage: "save in json file with avatar",
},
},
Action: func(c *cli.Context) error {
user := c.Args().Get(0)
if user != "" {
url := "https://api.github.com/users/" + user
responseBytes := getData(url)
var objmap map[string]interface{}
if err := json.Unmarshal(responseBytes, &objmap); err != nil {
log.Fatalln(err)
}
avatar_url := objmap["avatar_url"].(string)
// fmt.Println(objmap["avatar_url"])
j, _ := json.MarshalIndent(objmap, "", " ");
fmt.Println(string(j))
if c.Bool("save") {
// save in json file
f, _ := os.Create("users/github_" + user + ".json")
defer f.Close()
f.WriteString(string(j))
// save avatar img
img, _ := os.Create("users/avatars/github_" + user + ".jpg")
defer img.Close()
resp, err := http.Get(avatar_url)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if _, err := io.Copy(img, resp.Body); err != nil {
log.Fatalln(err)
}
}
} else {
log.Fatalln("username is required")
}
return nil
},
},
{
Name: "users",
Usage: "fetch users infos",
Flags: []cli.Flag{
cli.Float64Flag{
Name: "startId, sid",
Value: 1,
Usage: "start with user id",
},
cli.IntFlag{
Name: "totalPage, tp",
Value: 1,
Usage: "total page, per page is 100 user data (up to 10 pages)",
},
cli.BoolFlag{
Name: "saveAvatar, sa",
Usage: "save avatars file",
},
},
Action: func(c *cli.Context) error {
var objFull []map[string]interface{}
total_page := c.Int("totalPage")
last_id := c.Float64("startId")
if total_page < 1 || total_page > 10 {
log.Fatalln("Error: total page is up to 10 pages")
}
for i := 0; i < total_page; i++ {
url := "https://api.github.com/users?since=" + fmt.Sprintf("%.0f", last_id) + "&per_page=100"
// fmt.Println(url)
responseBytes := getData(url)
var objmap []map[string]interface{}
if err := json.Unmarshal(responseBytes, &objmap); err != nil {
log.Fatal(err)
}
last_id = objmap[len(objmap)-1]["id"].(float64)
for idx := range objmap {
// append user obj into single obj
objFull = append(objFull, objmap[idx])
// save users avatar
if c.Bool("saveAvatar") {
// save avatar img
img, _ := os.Create("users/bulk/avatars/github_" + fmt.Sprintf("%s", objmap[idx]["login"]) + ".jpg")
defer img.Close()
resp, err := http.Get(fmt.Sprintf("%s", objmap[idx]["avatar_url"]))
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading avatar - " + fmt.Sprintf("%s", objmap[idx]["login"]),
)
if _, err := io.Copy(io.MultiWriter(img, bar), resp.Body); err != nil {
log.Fatalln(err)
}
}
}
time.Sleep(1 * time.Second)
}
j, _ := json.MarshalIndent(objFull, "", " ");
// fmt.Println(string(j))
// save user infos in json
f, err := os.Create("users/bulk/github_"+time.Now().Format("2006-01-02")+".json")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
f.WriteString(string(j))
fmt.Printf("\nSuccess -> Data saved in /users/bulk/ folder")
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}