archiveorg.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package archiveorg
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "time"
  9. "miniflux.app/v2/internal/http/client"
  10. "miniflux.app/v2/internal/version"
  11. )
  12. const defaultClientTimeout = 30 * time.Second
  13. // See https://docs.google.com/document/d/1Nsv52MvSjbLb2PCpHlat0gkzw0EvtSgpKHu4mk0MnrA/edit?tab=t.0
  14. const options = "delay_wb_availability=1&if_not_archived_within=15d"
  15. type Client struct{}
  16. func NewClient() *Client {
  17. return &Client{}
  18. }
  19. func (c *Client) SendURL(entryURL string) error {
  20. requestURL := "https://web.archive.org/save/" + url.QueryEscape(entryURL) + "?" + options
  21. request, err := http.NewRequest(http.MethodGet, requestURL, nil)
  22. if err != nil {
  23. return fmt.Errorf("archiveorg: unable to create request: %v", err)
  24. }
  25. request.Header.Set("User-Agent", "Miniflux/"+version.Version)
  26. httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout})
  27. response, err := httpClient.Do(request)
  28. if err != nil {
  29. return fmt.Errorf("archiveorg: unable to send request: %v", err)
  30. }
  31. defer response.Body.Close()
  32. if response.StatusCode >= 400 {
  33. return fmt.Errorf("archiveorg: unexpected status code: url=%s status=%d", requestURL, response.StatusCode)
  34. }
  35. return nil
  36. }