-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharank.go
74 lines (63 loc) · 1.4 KB
/
arank.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
type items struct {
index int
rank int
url string
response *http.Response
err error
}
func getRanks(urls []string) <-chan items {
ch := make(chan items, len(urls)) // buffered
for i, url := range urls {
go func(url string, i int) {
alexaurl := "https://www.alexa.com/minisiteinfo/" + url
resp, err := http.Get(alexaurl)
defer resp.Body.Close()
lastrank := 0
if resp.StatusCode == 200 {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
doc.Find("table#siteStats").Each(func(i int, s *goquery.Selection) {
rank := s.Find("tr").First().Find("a").First().Text()
if endrank, err := strconv.Atoi(strings.TrimSpace(strings.ReplaceAll(rank, ",", ""))); err == nil {
lastrank = endrank
}
})
}
ch <- items{i, lastrank, url, resp, err}
}(url, i)
}
return ch
}
func main() {
urls := os.Args[1:]
var output []items
results := getRanks(urls)
for _ = range urls {
res := <-results
if res.rank > 0 {
output = append(output, res)
}
if res.err != nil {
fmt.Println(res.url, " error")
}
}
sort.Slice(output, func(i, j int) bool {
return output[i].rank < output[j].rank
})
for _, element := range output {
fmt.Println(element.url, "=>", element.rank)
}
}