forked from git-cloner/gitcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstar.go
More file actions
74 lines (71 loc) · 2.25 KB
/
Copy pathstar.go
File metadata and controls
74 lines (71 loc) · 2.25 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)
func httpGet(url string, token string) string {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
return ""
}
req.Header.Set("Authorization", "token "+token)
resp, err := (&http.Client{}).Do(req)
if err != nil {
log.Println(err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return string(body)
}
func GetRepoStar(w http.ResponseWriter, r *http.Request) {
//sample: https://api.github.com/repos/git-cloner/gitcache
//test : http://127.0.0.1:5001/gitcache/star/git-cloner/gitcache
github_token := os.Getenv("GITHUB_TOKEN")
url := "https://api.github.com/repos/" + strings.Replace(r.URL.RequestURI(), "/gitcache/star/", "", -1)
log.Printf("get star url : %v \n", url)
contents := httpGet(url, github_token)
//get stat
const reg = `"stargazers_count":\s*(\d+),`
compile := regexp.MustCompile(reg)
submatch := compile.FindAllSubmatch([]byte(contents), -1)
var stargazers_count = 0
for _, m := range submatch {
stargazers_count, _ = strconv.Atoi(string(m[1]))
}
//get language
const reg_lang = `\"language\"\:\s*\"(\w+)\",`
compile_lang := regexp.MustCompile(reg_lang)
submatch_lang := compile_lang.FindAllSubmatch([]byte(contents), -1)
var language = ""
for _, m := range submatch_lang {
language = string(m[1])
}
//get description
const reg_desc = `\"description\"\:\s*\"([^\"]+)\",`
compile_desc := regexp.MustCompile(reg_desc)
submatch_desc := compile_desc.FindAllSubmatch([]byte(contents), -1)
var description = ""
for _, m := range submatch_desc {
description = string(m[1])
}
//get updated_at
const reg_update = `\"updated_at\"\:\s*\"([^\"]+)\",`
compile_update := regexp.MustCompile(reg_update)
submatch_update := compile_update.FindAllSubmatch([]byte(contents), -1)
var updated_at = ""
for _, m := range submatch_update {
updated_at = string(m[1])
}
w.WriteHeader(200)
json := `{"stargazers_count":` + strconv.Itoa(stargazers_count) + `,"language":"` + language +
`","description":"` + description + `","updated_at":"` + updated_at + `"}`
w.Write([]byte(json))
log.Printf("get repo info : %v \n", json)
}