-
Notifications
You must be signed in to change notification settings - Fork 1
/
unsavory.go
145 lines (120 loc) · 2.68 KB
/
unsavory.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
// Package unsavory provides the types, functions and methods for interacting
// with the Pinboard API and checking URLs for 404 status codes.
package unsavory
import (
"encoding/json"
"log"
"net/http"
"net/url"
"time"
)
const (
baseURL = "https://api.pinboard.in/v1"
userAgent = "UnsavoryNG"
workerCount = 50
)
// Client bundles all values necessary for API requests.
type Client struct {
client *http.Client
dryRun bool
token string
}
// NewClient returns a configured unsavory.Client.
func NewClient(token string, dryRun bool) *Client {
client := &http.Client{
Timeout: time.Second * 15,
}
return &Client{
token: token,
dryRun: dryRun,
client: client}
}
// Run fetches all URLs and kicks off the check process.
func (c *Client) Run() {
if c.dryRun {
log.Printf("You are using dry run mode. No links will be deleted!\n\n")
}
log.Println("Retrieving URLs")
urls := c.getURLs()
log.Printf("Retrieved %d URLS\n", len(urls))
c.checkURLs(urls)
}
func (c *Client) getURLs() []string {
var posts []struct {
URL string `json:"href"`
}
resp := c.request("/posts/all")
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalln("Could not retrieve URLs!\nPlease check your API token.")
}
json.NewDecoder(resp.Body).Decode(&posts)
count := len(posts)
urls := make([]string, count)
for i, post := range posts {
urls[i] = post.URL
}
return urls
}
func (c *Client) checkURLs(urls []string) {
ch := make(chan string)
for i := 0; i < workerCount; i++ {
go func(urls chan string) {
for {
u, ok := <-urls
if !ok {
break
}
c.checkURL(u)
}
}(ch)
}
for _, url := range urls {
ch <- url
}
close(ch)
}
func (c *Client) checkURL(u string) {
resp, err := c.client.Head(u)
if err != nil {
if _, ok := err.(*url.Error); ok {
log.Printf("Deleting (%v): %s\n", u, err)
c.deleteURL(u)
}
} else {
switch resp.StatusCode {
case http.StatusNotFound, http.StatusGone:
log.Printf("Deleting (404): %s\n", u)
c.deleteURL(u)
default:
log.Printf("%d: %s\n", resp.StatusCode, u)
}
}
}
func (c *Client) deleteURL(url string) {
if !c.dryRun {
c.request("/posts/delete", url)
}
}
func (c *Client) request(path string, query ...string) *http.Response {
base, _ := url.Parse(baseURL)
base.Path += path
// Query params
params := url.Values{}
params.Add("format", "json")
params.Add("auth_token", c.token)
if len(query) > 0 {
params.Add("url", query[0])
}
base.RawQuery = params.Encode()
req, err := http.NewRequest("GET", base.String(), nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("User-Agent", userAgent)
resp, err := c.client.Do(req)
if err != nil {
log.Fatalln(err)
}
return resp
}