forked from yangwenmai/github-trending-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
325 lines (291 loc) · 7.04 KB
/
scraper.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
var tempDate string
// Alert type
type Alert struct {
Title, Content, URL, Priority, Source, Receiver string
}
//SendAlert to send notification
func (a *Alert) SendAlert() {
defer func() {
if r := recover(); r != nil {
println("SendAlert Recovered for", r)
a.SendAlert()
}
}()
data := url.Values{
"source": {a.Source},
"receiver": {a.Receiver},
"title": {a.Title},
"content": {a.Content},
"url": {a.URL},
"priority": {a.Priority},
}
resp, err := http.PostForm("https://api.alertover.com/v1/alert", data)
if err != nil {
println("alertover send message failure...")
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
println("alertover send message success.")
}
}
func main() {
//loop
// for {
tempDate = time.Now().Format("2006-01-02")
message := ""
if time.Now().Day() == 10 {
if ok, err := collectDocs(); ok {
message += "Collect the *.md files: OK!\n"
} else {
message += "collectDocs() is failed. " + err.Error() + "\n"
}
}
//set monitor targets
targets := []string{
"Go", "Rust", "Python", "Ruby",
"C++", "C", "Java",
"Shell", "Makefile",
"Swift", "Objective-C", "Kotlin",
"Jupyter-Notebook",
"HTML", "JavaScript", "TypeScript", "CSS", "Vue",
"TeX",
"Markdown"}
var content, readme string
jobs := make(chan string, 10)
backs := make(chan string, 10)
for w := 1; w <= 6; w++ {
go scrape(jobs, backs)
}
for j := 0; j < len(targets); j++ {
println(targets[j] + " is added to jobs.")
jobs <- targets[j]
}
for a := 0; a < len(targets); a++ {
content = content + <-backs
}
content = "### " + tempDate + "\n" + content
//close the channels
close(jobs)
close(backs)
//create markdown file
writeMarkDown(tempDate, content)
message += tempDate + ".md is completed.\n"
readme = "# Scraper\n\nWe scrape the github trending page of these languages: "
for _, v := range targets {
readme = readme + v + ", "
}
readme = readme + "and push a markdown result everyday.\n\n"
readme = readme + "[" + tempDate + ".md](https://github.com/yangwenmai/github-trending-backup/blob/master/" + tempDate + ".md)\n\n"
readme = readme + "Last Updated: " + time.Now().Format("2006-01-02 15:04:05")
writeMarkDown("README", readme)
println("README.md is updated.")
// gitPull()
// gitAddAll()
// gitCommit()
// gitPush()
// alert := Alert{
// //Get your unique ID from https://www.alertover.com to replace "xxxxxxxx" below
// Source: "u-2d71bf8d-c60c-40af-944e-60d120f2",
// Receiver: "u-2d71bf8d-c60c-40af-944e-60d120f2",
// Title: "Ok",
// Content: message,
// URL: "https://github.com/yangwenmai/github-trending-backup",
// Priority: "0", //优先级:0 普通,1 紧急
// }
// alert.SendAlert()
// time.Sleep(time.Duration(24) * time.Hour)
// }
}
//collectDocs
func collectDocs() (ok bool, err error) {
today := time.Now()
lastMonth := today.AddDate(0, -1, 0)
docName := lastMonth.Format("2006/01")
regType := lastMonth.Format("2006-01")
docPath, err := os.Getwd()
if err != nil {
return false, err
}
mdFiles, err := listDir(docPath, ".md")
if err != nil {
return false, err
}
var mdNewFiles []string
for _, v := range mdFiles {
if ok, _ := regexp.MatchString(regType, v); ok {
mdNewFiles = append(mdNewFiles, v)
}
}
err = os.MkdirAll(docName, os.ModePerm) //os.ModePerm 0777
if err != nil {
return false, err
}
for _, v := range mdNewFiles {
err = os.Rename(v, docName+string(os.PathSeparator)+v)
if err != nil {
return false, err
}
}
return true, nil
}
//listDir
func listDir(dirPth string, suffix string) (files []string, err error) {
files = make([]string, 0, 10)
dir, err := ioutil.ReadDir(dirPth)
if err != nil {
return nil, err
}
suffix = strings.ToUpper(suffix)
for _, fi := range dir {
if fi.IsDir() {
continue
}
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
files = append(files, fi.Name())
}
}
return files, nil
}
//interface to string
func interface2string(inter interface{}) string {
var tempStr string
switch inter.(type) {
case string:
tempStr = inter.(string)
break
case float64:
tempStr = strconv.FormatFloat(inter.(float64), 'f', -1, 64)
break
case int64:
tempStr = strconv.FormatInt(inter.(int64), 10)
break
case int:
tempStr = strconv.Itoa(inter.(int))
break
}
return tempStr
}
func writeMarkDown(fileName, content string) {
// open output file
fo, err := os.Create(fileName + ".md")
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
// make a write buffer
w := bufio.NewWriter(fo)
w.WriteString(content)
w.Flush()
}
func scrape(jobs chan string, backs chan<- string) {
defer func() {
if r := recover(); r != nil {
println("scrape Recovered for", interface2string(r))
jobs <- interface2string(r)
go scrape(jobs, backs)
}
}()
for j := range jobs {
language := j
var doc *goquery.Document
var e error
result := "\n#### " + language + "\n"
if doc, e = goquery.NewDocument("https://github.com/trending?l="+language); e != nil {
println("Error:", e.Error())
panic(language)
}
doc.Find(".Box-row").Each(func(i int, s *goquery.Selection) {
description := s.Find("p.col-9").Text()
repoURL, _ := s.Find("h2 a").Attr("href")
title := repoURL[1:]
url := "https://github.com" + repoURL
var stars = "0"
var forks = "0"
s.Find("a.Link--muted.d-inline-block.mr-3").Each(func(i int, contentSelection *goquery.Selection) {
if temp, ok := contentSelection.Find("svg").Attr("aria-label"); ok {
switch temp {
case "star":
stars = contentSelection.Text()
case "fork":
forks = contentSelection.Text()
}
}
})
result = result + fmt.Sprintf("%d.", i+1) + " [" + strings.Replace(strings.TrimSpace(title), " ", "", -1) + " (" + strings.TrimSpace(stars) + "s/" + strings.TrimSpace(forks) + "f)](" + url + ") : " + strings.TrimSpace(description) + "\n"
})
println(language + " is responsed to backs.")
backs <- result
}
}
func gitPull() {
app := "git"
arg0 := "pull"
arg1 := "origin"
arg2 := "master"
cmd := exec.Command(app, arg0, arg1, arg2)
out, err := cmd.Output()
if err != nil {
println(err.Error())
return
}
print(string(out))
}
func gitAddAll() {
app := "git"
arg0 := "add"
arg1 := "."
cmd := exec.Command(app, arg0, arg1)
out, err := cmd.Output()
if err != nil {
println(err.Error())
return
}
print(string(out))
}
func gitCommit() {
app := "git"
arg0 := "commit"
arg1 := "-am"
arg2 := time.Now().Format("2006-01-02 15:04:05")
cmd := exec.Command(app, arg0, arg1, arg2)
out, err := cmd.Output()
if err != nil {
println(err.Error())
return
}
print(string(out))
}
func gitPush() {
app := "git"
arg0 := "push"
arg1 := "origin"
arg2 := "master"
cmd := exec.Command(app, arg0, arg1, arg2)
out, err := cmd.Output()
if err != nil {
println(err.Error())
return
}
print(string(out))
}