-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy patheditorLinks.go
More file actions
89 lines (84 loc) · 2.12 KB
/
Copy patheditorLinks.go
File metadata and controls
89 lines (84 loc) · 2.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"net/http"
"net/url"
"sort"
"strings"
)
const editorLinksPath = "/links"
type linkDomainStat struct {
Domain string
Posts []*post
LinkCount int
}
func (a *goBlog) collectExternalDomains(blog string) ([]*linkDomainStat, error) {
posts, err := a.getPosts(&postsRequestConfig{
blogs: []string{blog},
})
if err != nil {
return nil, err
}
pls, err := a.extractPostLinks(posts)
if err != nil {
return nil, err
}
domainPosts := map[string][]*post{}
domainLinks := map[string]int{}
for _, pl := range pls {
seen := map[string]bool{}
for _, link := range pl.links {
u, err := url.Parse(link)
if err != nil {
continue
}
host := strings.ToLower(u.Hostname())
if host == "" {
continue
}
domainLinks[host]++
if !seen[host] {
seen[host] = true
domainPosts[host] = append(domainPosts[host], pl.post)
}
}
}
stats := make([]*linkDomainStat, 0, len(domainPosts))
for host, ps := range domainPosts {
sort.Slice(ps, func(i, j int) bool { return ps[i].Path < ps[j].Path })
stats = append(stats, &linkDomainStat{Domain: host, Posts: ps, LinkCount: domainLinks[host]})
}
sort.Slice(stats, func(i, j int) bool {
if len(stats[i].Posts) != len(stats[j].Posts) {
return len(stats[i].Posts) > len(stats[j].Posts)
}
if stats[i].LinkCount != stats[j].LinkCount {
return stats[i].LinkCount > stats[j].LinkCount
}
return stats[i].Domain < stats[j].Domain
})
return stats, nil
}
func (a *goBlog) serveEditorLinks(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
stats, err := a.collectExternalDomains(blog)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
if domain := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("domain"))); domain != "" {
var match *linkDomainStat
for _, s := range stats {
if s.Domain == domain {
match = s
break
}
}
a.render(w, r, a.renderEditorLinkDomain, &renderData{
Data: &editorLinkDomainRenderData{domain: domain, stat: match},
})
return
}
a.render(w, r, a.renderEditorLinks, &renderData{
Data: &editorLinksRenderData{domains: stats},
})
}