2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-03 04:32:14 +01:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package pinboard // import "miniflux.app/integration/pinboard"
|
2017-12-03 04:32:14 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/client"
|
2017-12-03 04:32:14 +01:00
|
|
|
)
|
|
|
|
|
2017-12-03 06:12:03 +01:00
|
|
|
// Client represents a Pinboard client.
|
2017-12-03 04:32:14 +01:00
|
|
|
type Client struct {
|
|
|
|
authToken string
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:28:41 +02:00
|
|
|
// NewClient returns a new Pinboard client.
|
|
|
|
func NewClient(authToken string) *Client {
|
|
|
|
return &Client{authToken: authToken}
|
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
// AddBookmark sends a link to Pinboard.
|
|
|
|
func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error {
|
2018-04-30 02:58:09 +02:00
|
|
|
if c.authToken == "" {
|
|
|
|
return fmt.Errorf("pinboard: missing credentials")
|
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
toRead := "no"
|
|
|
|
if markAsUnread {
|
|
|
|
toRead = "yes"
|
|
|
|
}
|
|
|
|
|
|
|
|
values := url.Values{}
|
|
|
|
values.Add("auth_token", c.authToken)
|
|
|
|
values.Add("url", link)
|
|
|
|
values.Add("description", title)
|
|
|
|
values.Add("tags", tags)
|
|
|
|
values.Add("toread", toRead)
|
|
|
|
|
2018-04-28 19:51:07 +02:00
|
|
|
clt := client.New("https://api.pinboard.in/v1/posts/add?" + values.Encode())
|
|
|
|
response, err := clt.Get()
|
2018-05-21 21:24:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("pinboard: unable to send bookmark: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-03 04:32:14 +01:00
|
|
|
if response.HasServerFailure() {
|
2017-12-19 05:52:46 +01:00
|
|
|
return fmt.Errorf("pinboard: unable to send bookmark, status=%d", response.StatusCode)
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:24:48 +02:00
|
|
|
return nil
|
2017-12-03 04:32:14 +01:00
|
|
|
}
|