|
| 1 | +// Package unsavory provides the types, functions and methods for interacting |
| 2 | +// with the Pinboard API and checking URLs for 404 status codes. |
| 3 | +package unsavory |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + baseURL = "https://api.pinboard.in/v1" |
| 16 | + userAgent = "UnsavoryNG" |
| 17 | +) |
| 18 | + |
| 19 | +// Client bundles all values necessary for API requests. |
| 20 | +type Client struct { |
| 21 | + baseQuery string |
| 22 | + client *http.Client |
| 23 | + |
| 24 | + DryRun bool |
| 25 | + Token string |
| 26 | +} |
| 27 | + |
| 28 | +type checkResponse struct { |
| 29 | + URL string |
| 30 | + Status string |
| 31 | + StatusCode int |
| 32 | +} |
| 33 | + |
| 34 | +// NewClient returns a configured unsavory.Client. |
| 35 | +func NewClient(token string, dryRun bool) *Client { |
| 36 | + client := &http.Client{ |
| 37 | + Timeout: time.Second * 10, |
| 38 | + } |
| 39 | + |
| 40 | + return &Client{ |
| 41 | + baseQuery: fmt.Sprintf("format=json&auth_token=%s", token), |
| 42 | + Token: token, |
| 43 | + DryRun: dryRun, |
| 44 | + client: client} |
| 45 | +} |
| 46 | + |
| 47 | +// Run fetches all URLs, checks them individually and removes saved links return |
| 48 | +// a 404 status code. |
| 49 | +func (c *Client) Run() { |
| 50 | + if c.DryRun { |
| 51 | + log.Printf("You are using dry run mode. No links will be deleted!\n\n") |
| 52 | + } |
| 53 | + |
| 54 | + log.Println("Retrieving URLs") |
| 55 | + urls := c.getAllURLs() |
| 56 | + log.Printf("Retrieved %d URLS\n", len(urls)) |
| 57 | + |
| 58 | + results := make(chan checkResponse) |
| 59 | + for _, url := range urls { |
| 60 | + go c.checkURL(url, results) |
| 61 | + } |
| 62 | + |
| 63 | + var result checkResponse |
| 64 | + for range urls { |
| 65 | + result = <-results |
| 66 | + switch result.StatusCode { |
| 67 | + case 200: |
| 68 | + continue |
| 69 | + case 404: |
| 70 | + log.Printf("Deleting %s\n", result.URL) |
| 71 | + c.deleteURL(result.URL) |
| 72 | + default: |
| 73 | + log.Printf("%s:%s\n", result.Status, result.URL) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (c *Client) getAllURLs() []string { |
| 79 | + var posts []struct { |
| 80 | + URL string `json:"href"` |
| 81 | + } |
| 82 | + |
| 83 | + resp := c.request("/posts/all") |
| 84 | + defer resp.Body.Close() |
| 85 | + |
| 86 | + if resp.StatusCode != 200 { |
| 87 | + log.Fatalln("Could not retrieve URLs!\nPlease check your API token.") |
| 88 | + } |
| 89 | + |
| 90 | + json.NewDecoder(resp.Body).Decode(&posts) |
| 91 | + |
| 92 | + urls := make([]string, len(posts)) |
| 93 | + |
| 94 | + for i, post := range posts { |
| 95 | + urls[i] = post.URL |
| 96 | + } |
| 97 | + return urls |
| 98 | +} |
| 99 | + |
| 100 | +func (c *Client) checkURL(url string, results chan<- checkResponse) { |
| 101 | + resp, err := c.client.Head(url) |
| 102 | + if err != nil { |
| 103 | + results <- checkResponse{url, err.Error(), 0} |
| 104 | + return |
| 105 | + } |
| 106 | + results <- checkResponse{url, resp.Status, resp.StatusCode} |
| 107 | +} |
| 108 | + |
| 109 | +func (c *Client) deleteURL(url string) { |
| 110 | + if !c.DryRun { |
| 111 | + c.request("/posts/delete", fmt.Sprintf("url=%s", url)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (c *Client) request(path string, query ...string) *http.Response { |
| 116 | + url := fmt.Sprintf("%s%s?%s", baseURL, path, c.baseQuery) |
| 117 | + if len(query) > 0 { |
| 118 | + url = url + "&" + strings.Join(query, "&") |
| 119 | + } |
| 120 | + |
| 121 | + req, err := http.NewRequest("GET", url, nil) |
| 122 | + if err != nil { |
| 123 | + log.Fatalln(err) |
| 124 | + } |
| 125 | + req.Header.Set("User-Agent", userAgent) |
| 126 | + |
| 127 | + resp, err := c.client.Do(req) |
| 128 | + if err != nil { |
| 129 | + log.Fatalln(err) |
| 130 | + } |
| 131 | + |
| 132 | + return resp |
| 133 | +} |
0 commit comments