-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorending.go
143 lines (119 loc) · 3.18 KB
/
gorending.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
// Package gorending is cli tool that show github trendings and open browser with selected trending URL in Terminal at real-time.
package main
import (
"log"
"os"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/manifoldco/promptui"
"github.com/skratchdot/open-golang/open"
"github.com/urfave/cli"
)
// GithubURL is Github url
var GithubURL = "https://github.com"
// TrendingURL is Github trending url
var TrendingURL = GithubURL + "/trending/"
// Repository Structure
type Repository struct {
URL string
Name string
Author string
}
// CrawlTrending function requests to Github website and prints parsed trendings.
func CrawlTrending(lang string, count int) []Repository {
doc, err := goquery.NewDocument(TrendingURL + lang)
if err != nil {
log.Fatal(err)
}
rawRepoList := doc.Find(".d-inline-block > h3 > a").Slice(0, count)
repoList := []Repository{}
rawRepoList.Each(func(i int, s *goquery.Selection) {
repoURL, ok := s.Attr("href")
if ok {
trendingName := strings.Trim(s.Text(), " \n")
trendingNameList := strings.Split(trendingName, " / ")
authorName := trendingNameList[0]
repoName := trendingNameList[1]
repo := Repository{
URL: GithubURL + repoURL,
Name: repoName,
Author: authorName,
}
repoList = append(repoList, repo)
}
})
return repoList
}
// ShowPrompt function shows prompt about trendings and open browser about selected trending.
func ShowPrompt(repoList []Repository) error {
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "> {{ .Name | cyan }} ({{ .URL | red }})",
Inactive: " {{ .Name | cyan }} ({{ .URL | red }})",
Selected: "! {{ .Name | red | cyan }}",
Details: `
--------- Repository ----------
{{ "URL:" | faint }} {{ .URL }}
{{ "Name:" | faint }} {{ .Name }}
{{ "Author: " | faint }} {{ .Author }}`,
}
searcher := func(input string, index int) bool {
repo := repoList[index]
name := strings.Replace(strings.ToLower(repo.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "Repo",
Items: repoList,
Templates: templates,
Size: 5,
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
return err
}
open.Run(repoList[i].URL)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "gorending"
app.Usage = "Show Github trending in Terminal!"
app.Version = "1.0.1"
app.Compiled = time.Now()
app.Copyright = "(c) 2017 Myungseo Kang"
app.Authors = []cli.Author{
cli.Author{
Name: "Myungseo Kang",
Email: "l3opold7@gmail.com",
},
}
app.Flags = []cli.Flag{
// lang flag is language that you want to see
cli.StringFlag{
Name: "lang, L",
Value: "",
Usage: "language that you want to see (default: all language)",
},
// count flag is count that you want to see
cli.IntFlag{
Name: "count, C",
Value: 10,
Usage: "count that you want to see",
},
}
app.Action = func(c *cli.Context) error {
lang := c.String("lang")
count := c.Int("count")
repoList := CrawlTrending(lang, count)
err := ShowPrompt(repoList)
if err != nil {
return err
}
return nil
}
app.Run(os.Args)
}