-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar.go
139 lines (109 loc) · 2.6 KB
/
star.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
package main
import (
"flag"
"fmt"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/jaytaylor/html2text"
"github.com/kyokomi/emoji"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var githubURL = "https://github.com/#/#"
// command params
var (
username, reponame string
names bool
)
// results
var (
stars int
)
var (
boldred = color.New(color.FgRed, color.Bold)
boldgreen = color.New(color.FgHiGreen, color.Bold)
)
// bind flags to params
func bindFlags() {
flag.StringVar(&username, "u", "", "Give a username/organisation ex: dragonzurfer")
flag.StringVar(&reponame, "r", "", "Give a repository name")
flag.Parse()
}
// Get HTML page as string
func getContent(URL string) (string, bool) {
resp, err := http.Get(URL)
if err != nil {
fmt.Println("Error fetching page")
return "", true
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
ret := string(body)
if err != nil {
fmt.Println("Error :( Try Again")
return "", true
}
return ret, false
}
func loader() {
s := spinner.New(spinner.CharSets[1], 100*time.Millisecond) // Build our new spinner
s.Start() // Start the spinner
time.Sleep(2 * time.Second) // Run for some time to simulate work
s.Stop()
}
// Print to terminal
func PrintParams() {
// print the number of stars recieved
boldgreen.Printf(" %v's got %d", reponame, stars)
emoji.Printf(" :star2:'s \n")
if stars >= 10 && stars < 100 {
emoji.Println(" AWSM! :clap: ")
} else if stars >= 100 && stars < 1000 {
emoji.Printf(" %v Deserves a :beer:\n", username)
} else if stars >= 1000 {
emoji.Printf(" WOW! %v is popular :lollipop:\n", reponame)
}
}
func main() {
var url, resp string
var err bool
var errconv error
bindFlags()
if username == "" || reponame == "" {
boldred.Println("-u or -r parameter is empty!")
return
}
var wg sync.WaitGroup
wg.Add(1)
//get url content
go func() {
defer wg.Done()
url = strings.Replace(githubURL, "#", username, 1)
url = strings.Replace(url, "#", reponame, 1)
resp, err = getContent(url)
if err {
boldred.Printf("Error fetching ")
boldgreen.Printf("stars\n")
return
}
}()
loader()
wg.Wait()
//extract stars
text, _ := html2text.FromString(resp)
regex := `\*\s+Star[^)]*\)\s([\d,]+)`
re := regexp.MustCompile(regex)
result := re.FindAllStringSubmatch(text, -1)
stars, errconv = strconv.Atoi(strings.Replace(result[0][1], ",", "", -1))
if errconv != nil {
boldred.Println("Error")
return
}
//print to screen
PrintParams()
}