-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (78 loc) · 2.18 KB
/
main.go
File metadata and controls
86 lines (78 loc) · 2.18 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
package main
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/github"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
// ECHO를 씀. GIN 사용하자.
e.GET("/", func(c echo.Context) error {
// 내 레포에 어떤 언어가 얼마나 있는지 확인할 수 있음.
userName := "vidigummy"
repoList, err := getRepo(userName)
if err != nil {
fmt.Println(err)
}
// 레포 리스트 확인용(무슨 레포가 있는지)
// fmt.Println(repoList)
// 랭귀지맵은 사용자의 언어 map이라고 할 수 있죠
languageMap := make(map[string]int)
// api들 동시에 호출, 빠르게 가져오기
// 403 뜨네? 큰일난걸지두...
repoChan := make(chan map[string]int)
for _, repoName := range repoList {
go getRepoLanguages(userName, repoName, repoChan)
}
// 가져온 리스트 싹 다 한 레포에 집어넣기
for i := 0; i < len(repoList); i++ {
tmpLanguageMap := <-repoChan
for key, value := range tmpLanguageMap {
checkData, exists := languageMap[key]
if !exists {
languageMap[key] = value
} else {
languageMap[key] = checkData + value
}
}
}
fmt.Println(languageMap)
return c.File("home.html")
})
e.Logger.Fatal(e.Start(":8080"))
}
func getRepo(username string) ([]string, error) {
client := github.NewClient(nil)
ctx := context.Background()
opt := &github.RepositoryListOptions{Type: "public"}
repos, _, err := client.Repositories.List(ctx, username, opt)
if err != nil {
fmt.Println(err)
return strings.Split("err err", " "), err
}
var reposList []string = make([]string, len(repos))
repoChan := make(chan string)
for _, repo := range repos {
go getUrl(*repo.LanguagesURL, repoChan)
}
for i := 0; i < len(repos)-1; i++ {
tmp := <-repoChan
reposList[i] = tmp
}
return reposList, nil
}
func getUrl(url string, c chan<- string) {
slicedURL := strings.Split(url, "/")
c <- slicedURL[5]
}
func getRepoLanguages(userName string, repoName string, c chan<- map[string]int) {
client := github.NewClient(nil)
ctx := context.Background()
languages, _, err := client.Repositories.ListLanguages(ctx, userName, repoName)
if err != nil {
fmt.Println(err)
}
c <- languages
}