archiveorg.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package archiveorg
  4. import (
  5. "log/slog"
  6. "net/http"
  7. "net/url"
  8. )
  9. // See https://docs.google.com/document/d/1Nsv52MvSjbLb2PCpHlat0gkzw0EvtSgpKHu4mk0MnrA/edit?tab=t.0
  10. const options = "delay_wb_availability=1&if_not_archived_within=15d"
  11. type Client struct{}
  12. func NewClient() *Client {
  13. return &Client{}
  14. }
  15. func (c *Client) SendURL(entryURL, title string) {
  16. // We're using a goroutine here as submissions to archive.org might take a long time
  17. // and trigger a timeout on miniflux' side.
  18. go func(entryURL string) {
  19. res, err := http.Get("https://web.archive.org/save/" + url.QueryEscape(entryURL) + "?" + options)
  20. if err != nil {
  21. slog.Error("archiveorg: unable to send request: %v",
  22. slog.Any("err", err),
  23. slog.String("title", title),
  24. slog.String("url", entryURL),
  25. )
  26. return
  27. }
  28. if res.StatusCode > 299 {
  29. slog.Error("archiveorg: failed with status code",
  30. slog.String("title", title),
  31. slog.String("url", entryURL),
  32. slog.Int("code", res.StatusCode),
  33. )
  34. }
  35. res.Body.Close()
  36. }(entryURL)
  37. }