mirror of
https://github.com/miniflux/v2.git
synced 2025-09-15 18:57:04 +00:00
Move internal packages to an internal folder
For reference: https://go.dev/doc/go1.4#internalpackages
This commit is contained in:
parent
c234903255
commit
168a870c02
433 changed files with 1121 additions and 1123 deletions
54
internal/integration/apprise/apprise.go
Normal file
54
internal/integration/apprise/apprise.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package apprise
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
// Client represents a Apprise client.
|
||||
type Client struct {
|
||||
servicesURL string
|
||||
baseURL string
|
||||
}
|
||||
|
||||
// NewClient returns a new Apprise client.
|
||||
func NewClient(serviceURL, baseURL string) *Client {
|
||||
return &Client{serviceURL, baseURL}
|
||||
}
|
||||
|
||||
// PushEntry pushes entry to apprise
|
||||
func (c *Client) PushEntry(entry *model.Entry) error {
|
||||
if c.baseURL == "" || c.servicesURL == "" {
|
||||
return fmt.Errorf("apprise: missing credentials")
|
||||
}
|
||||
timeout := time.Duration(1 * time.Second)
|
||||
_, err := net.DialTimeout("tcp", c.baseURL, timeout)
|
||||
if err != nil {
|
||||
clt := client.New(c.baseURL + "/notify")
|
||||
message := "[" + entry.Title + "]" + "(" + entry.URL + ")" + "\n\n"
|
||||
data := &Data{
|
||||
Urls: c.servicesURL,
|
||||
Body: message,
|
||||
}
|
||||
response, error := clt.PostJSON(data)
|
||||
if error != nil {
|
||||
return fmt.Errorf("apprise: ending message failed: %v", error)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("apprise: request failed, status=%d", response.StatusCode)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("%s %s %s", c.baseURL, "responding on port:", strings.Split(c.baseURL, ":")[1])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
9
internal/integration/apprise/wrapper.go
Normal file
9
internal/integration/apprise/wrapper.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package apprise
|
||||
|
||||
type Data struct {
|
||||
Urls string `json:"urls"`
|
||||
Body string `json:"body"`
|
||||
}
|
72
internal/integration/espial/espial.go
Normal file
72
internal/integration/espial/espial.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package espial // import "miniflux.app/v2/internal/integration/espial"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Document structure of an Espial document
|
||||
type Document struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Url string `json:"url,omitempty"`
|
||||
ToRead bool `json:"toread,omitempty"`
|
||||
Tags string `json:"tags,omitempty"`
|
||||
}
|
||||
|
||||
// Client represents an Espial client.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
}
|
||||
|
||||
// NewClient returns a new Espial client.
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{baseURL: baseURL, apiKey: apiKey}
|
||||
}
|
||||
|
||||
// AddEntry sends an entry to Espial.
|
||||
func (c *Client) AddEntry(link, title, content, tags string) error {
|
||||
if c.baseURL == "" || c.apiKey == "" {
|
||||
return fmt.Errorf("espial: missing credentials")
|
||||
}
|
||||
|
||||
doc := &Document{
|
||||
Title: title,
|
||||
Url: link,
|
||||
ToRead: true,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
apiURL, err := getAPIEndpoint(c.baseURL, "/api/add")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clt := client.New(apiURL)
|
||||
clt.WithAuthorization("ApiKey " + c.apiKey)
|
||||
response, err := clt.PostJSON(doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("espial: unable to send entry: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("espial: unable to send entry, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAPIEndpoint(baseURL, pathURL string) (string, error) {
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("espial: invalid API endpoint: %v", err)
|
||||
}
|
||||
u.Path = path.Join(u.Path, pathURL)
|
||||
return u.String(), nil
|
||||
}
|
47
internal/integration/instapaper/instapaper.go
Normal file
47
internal/integration/instapaper/instapaper.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package instapaper // import "miniflux.app/v2/internal/integration/instapaper"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Client represents an Instapaper client.
|
||||
type Client struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
// NewClient returns a new Instapaper client.
|
||||
func NewClient(username, password string) *Client {
|
||||
return &Client{username: username, password: password}
|
||||
}
|
||||
|
||||
// AddURL sends a link to Instapaper.
|
||||
func (c *Client) AddURL(link, title string) error {
|
||||
if c.username == "" || c.password == "" {
|
||||
return fmt.Errorf("instapaper: missing credentials")
|
||||
}
|
||||
|
||||
values := url.Values{}
|
||||
values.Add("url", link)
|
||||
values.Add("title", title)
|
||||
|
||||
apiURL := "https://www.instapaper.com/api/add?" + values.Encode()
|
||||
clt := client.New(apiURL)
|
||||
clt.WithCredentials(c.username, c.password)
|
||||
response, err := clt.Get()
|
||||
if err != nil {
|
||||
return fmt.Errorf("instapaper: unable to send url: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("instapaper: unable to send url, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
176
internal/integration/integration.go
Normal file
176
internal/integration/integration.go
Normal file
|
@ -0,0 +1,176 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package integration // import "miniflux.app/v2/internal/integration"
|
||||
|
||||
import (
|
||||
"miniflux.app/v2/internal/config"
|
||||
"miniflux.app/v2/internal/integration/apprise"
|
||||
"miniflux.app/v2/internal/integration/espial"
|
||||
"miniflux.app/v2/internal/integration/instapaper"
|
||||
"miniflux.app/v2/internal/integration/linkding"
|
||||
"miniflux.app/v2/internal/integration/matrixbot"
|
||||
"miniflux.app/v2/internal/integration/notion"
|
||||
"miniflux.app/v2/internal/integration/nunuxkeeper"
|
||||
"miniflux.app/v2/internal/integration/pinboard"
|
||||
"miniflux.app/v2/internal/integration/pocket"
|
||||
"miniflux.app/v2/internal/integration/readwise"
|
||||
"miniflux.app/v2/internal/integration/telegrambot"
|
||||
"miniflux.app/v2/internal/integration/wallabag"
|
||||
"miniflux.app/v2/internal/logger"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
// SendEntry sends the entry to third-party providers when the user click on "Save".
|
||||
func SendEntry(entry *model.Entry, integration *model.Integration) {
|
||||
if integration.PinboardEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Pinboard", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := pinboard.NewClient(integration.PinboardToken)
|
||||
err := client.AddBookmark(
|
||||
entry.URL,
|
||||
entry.Title,
|
||||
integration.PinboardTags,
|
||||
integration.PinboardMarkAsUnread,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.InstapaperEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Instapaper", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := instapaper.NewClient(integration.InstapaperUsername, integration.InstapaperPassword)
|
||||
if err := client.AddURL(entry.URL, entry.Title); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.WallabagEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Wallabag", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := wallabag.NewClient(
|
||||
integration.WallabagURL,
|
||||
integration.WallabagClientID,
|
||||
integration.WallabagClientSecret,
|
||||
integration.WallabagUsername,
|
||||
integration.WallabagPassword,
|
||||
integration.WallabagOnlyURL,
|
||||
)
|
||||
|
||||
if err := client.AddEntry(entry.URL, entry.Title, entry.Content); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.NotionEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Notion", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := notion.NewClient(
|
||||
integration.NotionToken,
|
||||
integration.NotionPageID,
|
||||
)
|
||||
if err := client.AddEntry(entry.URL, entry.Title); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.NunuxKeeperEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to NunuxKeeper", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := nunuxkeeper.NewClient(
|
||||
integration.NunuxKeeperURL,
|
||||
integration.NunuxKeeperAPIKey,
|
||||
)
|
||||
|
||||
if err := client.AddEntry(entry.URL, entry.Title, entry.Content); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.EspialEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Espial", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := espial.NewClient(
|
||||
integration.EspialURL,
|
||||
integration.EspialAPIKey,
|
||||
)
|
||||
|
||||
if err := client.AddEntry(entry.URL, entry.Title, entry.Content, integration.EspialTags); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.PocketEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Pocket", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := pocket.NewClient(config.Opts.PocketConsumerKey(integration.PocketConsumerKey), integration.PocketAccessToken)
|
||||
if err := client.AddURL(entry.URL, entry.Title); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.LinkdingEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Linkding", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := linkding.NewClient(
|
||||
integration.LinkdingURL,
|
||||
integration.LinkdingAPIKey,
|
||||
integration.LinkdingTags,
|
||||
integration.LinkdingMarkAsUnread,
|
||||
)
|
||||
if err := client.AddEntry(entry.Title, entry.URL); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if integration.ReadwiseEnabled {
|
||||
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Readwise Reader", entry.ID, entry.URL, integration.UserID)
|
||||
|
||||
client := readwise.NewClient(
|
||||
integration.ReadwiseAPIKey,
|
||||
)
|
||||
|
||||
if err := client.AddEntry(entry.URL); err != nil {
|
||||
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PushEntries pushes an entry array to third-party providers during feed refreshes.
|
||||
func PushEntries(entries model.Entries, integration *model.Integration) {
|
||||
if integration.MatrixBotEnabled {
|
||||
logger.Debug("[Integration] Sending %d entries for User #%d to Matrix", len(entries), integration.UserID)
|
||||
|
||||
err := matrixbot.PushEntries(entries, integration.MatrixBotURL, integration.MatrixBotUser, integration.MatrixBotPassword, integration.MatrixBotChatID)
|
||||
if err != nil {
|
||||
logger.Error("[Integration] push entries to matrix bot failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PushEntry pushes an entry to third-party providers during feed refreshes.
|
||||
func PushEntry(entry *model.Entry, integration *model.Integration) {
|
||||
if integration.TelegramBotEnabled {
|
||||
logger.Debug("[Integration] Sending Entry %q for User #%d to Telegram", entry.URL, integration.UserID)
|
||||
|
||||
err := telegrambot.PushEntry(entry, integration.TelegramBotToken, integration.TelegramBotChatID)
|
||||
if err != nil {
|
||||
logger.Error("[Integration] push entry to telegram bot failed: %v", err)
|
||||
}
|
||||
}
|
||||
if integration.AppriseEnabled {
|
||||
logger.Debug("[Integration] Sending Entry %q for User #%d to apprise", entry.URL, integration.UserID)
|
||||
|
||||
client := apprise.NewClient(
|
||||
integration.AppriseServicesURL,
|
||||
integration.AppriseURL,
|
||||
)
|
||||
err := client.PushEntry(entry)
|
||||
if err != nil {
|
||||
logger.Error("[Integration] push entry to apprise failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
84
internal/integration/linkding/linkding.go
Normal file
84
internal/integration/linkding/linkding.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package linkding // import "miniflux.app/v2/internal/integration/linkding"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Document structure of a Linkding document
|
||||
type Document struct {
|
||||
Url string `json:"url,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
TagNames []string `json:"tag_names,omitempty"`
|
||||
Unread bool `json:"unread,omitempty"`
|
||||
}
|
||||
|
||||
// Client represents an Linkding client.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
tags string
|
||||
unread bool
|
||||
}
|
||||
|
||||
// NewClient returns a new Linkding client.
|
||||
func NewClient(baseURL, apiKey, tags string, unread bool) *Client {
|
||||
return &Client{baseURL: baseURL, apiKey: apiKey, tags: tags, unread: unread}
|
||||
}
|
||||
|
||||
// AddEntry sends an entry to Linkding.
|
||||
func (c *Client) AddEntry(title, url string) error {
|
||||
if c.baseURL == "" || c.apiKey == "" {
|
||||
return fmt.Errorf("linkding: missing credentials")
|
||||
}
|
||||
|
||||
tagsSplitFn := func(c rune) bool {
|
||||
return c == ',' || c == ' '
|
||||
}
|
||||
|
||||
doc := &Document{
|
||||
Url: url,
|
||||
Title: title,
|
||||
TagNames: strings.FieldsFunc(c.tags, tagsSplitFn),
|
||||
Unread: c.unread,
|
||||
}
|
||||
|
||||
apiURL, err := getAPIEndpoint(c.baseURL, "/api/bookmarks/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clt := client.New(apiURL)
|
||||
clt.WithAuthorization("Token " + c.apiKey)
|
||||
response, err := clt.PostJSON(doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("linkding: unable to send entry: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("linkding: unable to send entry, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAPIEndpoint(baseURL, pathURL string) (string, error) {
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("linkding: invalid API endpoint: %v", err)
|
||||
}
|
||||
|
||||
relative, err := url.Parse(pathURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("linkding: invalid API endpoint: %v", err)
|
||||
}
|
||||
|
||||
u = u.ResolveReference(relative)
|
||||
return u.String(), nil
|
||||
}
|
50
internal/integration/matrixbot/matrixbot.go
Normal file
50
internal/integration/matrixbot/matrixbot.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package matrixbot // import "miniflux.app/v2/internal/integration/matrixbot"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"miniflux.app/v2/internal/logger"
|
||||
"miniflux.app/v2/internal/model"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
// PushEntry pushes entries to matrix chat using integration settings provided
|
||||
func PushEntries(entries model.Entries, serverURL, botLogin, botPassword, chatID string) error {
|
||||
bot, err := gomatrix.NewClient(serverURL, "", "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("matrixbot: bot creation failed: %w", err)
|
||||
}
|
||||
|
||||
resp, err := bot.Login(&gomatrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: botLogin,
|
||||
Password: botPassword,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Debug("matrixbot: login failed: %w", err)
|
||||
return fmt.Errorf("matrixbot: login failed, please check your credentials or turn on debug mode")
|
||||
}
|
||||
|
||||
bot.SetCredentials(resp.UserID, resp.AccessToken)
|
||||
defer func() {
|
||||
bot.Logout()
|
||||
bot.ClearCredentials()
|
||||
}()
|
||||
|
||||
message := ""
|
||||
for _, entry := range entries {
|
||||
message = message + entry.Title + " " + entry.URL + "\n"
|
||||
}
|
||||
|
||||
if _, err = bot.SendText(chatID, message); err != nil {
|
||||
logger.Debug("matrixbot: sending message failed: %w", err)
|
||||
return fmt.Errorf("matrixbot: sending message failed, turn on debug mode for more informations")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
54
internal/integration/notion/notion.go
Normal file
54
internal/integration/notion/notion.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package notion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Client represents a Notion client.
|
||||
type Client struct {
|
||||
token string
|
||||
pageID string
|
||||
}
|
||||
|
||||
// NewClient returns a new Notion client.
|
||||
func NewClient(token, pageID string) *Client {
|
||||
return &Client{token, pageID}
|
||||
}
|
||||
|
||||
func (c *Client) AddEntry(entryURL string, entryTitle string) error {
|
||||
if c.token == "" || c.pageID == "" {
|
||||
return fmt.Errorf("notion: missing credentials")
|
||||
}
|
||||
clt := client.New("https://api.notion.com/v1/blocks/" + c.pageID + "/children")
|
||||
block := &Data{
|
||||
Children: []Block{
|
||||
{
|
||||
Object: "block",
|
||||
Type: "bookmark",
|
||||
Bookmark: Bookmark{
|
||||
Caption: []interface{}{},
|
||||
URL: entryURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
clt.WithAuthorization("Bearer " + c.token)
|
||||
customHeaders := map[string]string{
|
||||
"Notion-Version": "2022-06-28",
|
||||
}
|
||||
clt.WithCustomHeaders(customHeaders)
|
||||
response, error := clt.PatchJSON(block)
|
||||
if error != nil {
|
||||
return fmt.Errorf("notion: unable to patch entry: %v", error)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("notion: request failed, status=%d", response.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
19
internal/integration/notion/wrapper.go
Normal file
19
internal/integration/notion/wrapper.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package notion
|
||||
|
||||
type Data struct {
|
||||
Children []Block `json:"children"`
|
||||
}
|
||||
|
||||
type Block struct {
|
||||
Object string `json:"object"`
|
||||
Type string `json:"type"`
|
||||
Bookmark Bookmark `json:"bookmark"`
|
||||
}
|
||||
|
||||
type Bookmark struct {
|
||||
Caption []interface{} `json:"caption"` // Assuming the "caption" field can have different types
|
||||
URL string `json:"url"`
|
||||
}
|
72
internal/integration/nunuxkeeper/nunuxkeeper.go
Normal file
72
internal/integration/nunuxkeeper/nunuxkeeper.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package nunuxkeeper // import "miniflux.app/v2/internal/integration/nunuxkeeper"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Document structure of a Nununx Keeper document
|
||||
type Document struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Origin string `json:"origin,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
ContentType string `json:"contentType,omitempty"`
|
||||
}
|
||||
|
||||
// Client represents an Nunux Keeper client.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
}
|
||||
|
||||
// NewClient returns a new Nunux Keeepr client.
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{baseURL: baseURL, apiKey: apiKey}
|
||||
}
|
||||
|
||||
// AddEntry sends an entry to Nunux Keeper.
|
||||
func (c *Client) AddEntry(link, title, content string) error {
|
||||
if c.baseURL == "" || c.apiKey == "" {
|
||||
return fmt.Errorf("nunux-keeper: missing credentials")
|
||||
}
|
||||
|
||||
doc := &Document{
|
||||
Title: title,
|
||||
Origin: link,
|
||||
Content: content,
|
||||
ContentType: "text/html",
|
||||
}
|
||||
|
||||
apiURL, err := getAPIEndpoint(c.baseURL, "/v2/documents")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clt := client.New(apiURL)
|
||||
clt.WithCredentials("api", c.apiKey)
|
||||
response, err := clt.PostJSON(doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("nunux-keeper: unable to send entry: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("nunux-keeper: unable to send entry, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAPIEndpoint(baseURL, pathURL string) (string, error) {
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("nunux-keeper: invalid API endpoint: %v", err)
|
||||
}
|
||||
u.Path = path.Join(u.Path, pathURL)
|
||||
return u.String(), nil
|
||||
}
|
52
internal/integration/pinboard/pinboard.go
Normal file
52
internal/integration/pinboard/pinboard.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pinboard // import "miniflux.app/v2/internal/integration/pinboard"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Client represents a Pinboard client.
|
||||
type Client struct {
|
||||
authToken string
|
||||
}
|
||||
|
||||
// NewClient returns a new Pinboard client.
|
||||
func NewClient(authToken string) *Client {
|
||||
return &Client{authToken: authToken}
|
||||
}
|
||||
|
||||
// AddBookmark sends a link to Pinboard.
|
||||
func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error {
|
||||
if c.authToken == "" {
|
||||
return fmt.Errorf("pinboard: missing credentials")
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
clt := client.New("https://api.pinboard.in/v1/posts/add?" + values.Encode())
|
||||
response, err := clt.Get()
|
||||
if err != nil {
|
||||
return fmt.Errorf("pinboard: unable to send bookmark: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("pinboard: unable to send bookmark, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
102
internal/integration/pocket/connector.go
Normal file
102
internal/integration/pocket/connector.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pocket // import "miniflux.app/v2/internal/integration/pocket"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Connector manages the authorization flow with Pocket to get a personal access token.
|
||||
type Connector struct {
|
||||
consumerKey string
|
||||
}
|
||||
|
||||
// NewConnector returns a new Pocket Connector.
|
||||
func NewConnector(consumerKey string) *Connector {
|
||||
return &Connector{consumerKey}
|
||||
}
|
||||
|
||||
// RequestToken fetches a new request token from Pocket API.
|
||||
func (c *Connector) RequestToken(redirectURL string) (string, error) {
|
||||
type req struct {
|
||||
ConsumerKey string `json:"consumer_key"`
|
||||
RedirectURI string `json:"redirect_uri"`
|
||||
}
|
||||
|
||||
clt := client.New("https://getpocket.com/v3/oauth/request")
|
||||
response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, RedirectURI: redirectURL})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to fetch request token: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return "", fmt.Errorf("pocket: unable to fetch request token, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to read response body: %v", err)
|
||||
}
|
||||
|
||||
values, err := url.ParseQuery(string(body))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to parse response: %v", err)
|
||||
}
|
||||
|
||||
code := values.Get("code")
|
||||
if code == "" {
|
||||
return "", errors.New("pocket: code is empty")
|
||||
}
|
||||
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// AccessToken fetches a new access token once the end-user authorized the application.
|
||||
func (c *Connector) AccessToken(requestToken string) (string, error) {
|
||||
type req struct {
|
||||
ConsumerKey string `json:"consumer_key"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
clt := client.New("https://getpocket.com/v3/oauth/authorize")
|
||||
response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, Code: requestToken})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to fetch access token: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return "", fmt.Errorf("pocket: unable to fetch access token, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to read response body: %v", err)
|
||||
}
|
||||
|
||||
values, err := url.ParseQuery(string(body))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("pocket: unable to parse response: %v", err)
|
||||
}
|
||||
|
||||
token := values.Get("access_token")
|
||||
if token == "" {
|
||||
return "", errors.New("pocket: access_token is empty")
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// AuthorizationURL returns the authorization URL for the end-user.
|
||||
func (c *Connector) AuthorizationURL(requestToken, redirectURL string) string {
|
||||
return fmt.Sprintf(
|
||||
"https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s",
|
||||
requestToken,
|
||||
redirectURL,
|
||||
)
|
||||
}
|
54
internal/integration/pocket/pocket.go
Normal file
54
internal/integration/pocket/pocket.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pocket // import "miniflux.app/v2/internal/integration/pocket"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Client represents a Pocket client.
|
||||
type Client struct {
|
||||
consumerKey string
|
||||
accessToken string
|
||||
}
|
||||
|
||||
// NewClient returns a new Pocket client.
|
||||
func NewClient(consumerKey, accessToken string) *Client {
|
||||
return &Client{consumerKey, accessToken}
|
||||
}
|
||||
|
||||
// AddURL sends a single link to Pocket.
|
||||
func (c *Client) AddURL(link, title string) error {
|
||||
if c.consumerKey == "" || c.accessToken == "" {
|
||||
return fmt.Errorf("pocket: missing credentials")
|
||||
}
|
||||
|
||||
type body struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ConsumerKey string `json:"consumer_key"`
|
||||
Title string `json:"title,omitempty"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
data := &body{
|
||||
AccessToken: c.accessToken,
|
||||
ConsumerKey: c.consumerKey,
|
||||
Title: title,
|
||||
URL: link,
|
||||
}
|
||||
|
||||
clt := client.New("https://getpocket.com/v3/add")
|
||||
response, err := clt.PostJSON(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pocket: unable to send url: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("pocket: unable to send url, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
66
internal/integration/readwise/readwise.go
Normal file
66
internal/integration/readwise/readwise.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Readwise Reader API documentation: https://readwise.io/reader_api
|
||||
|
||||
package readwise // import "miniflux.app/v2/internal/integration/readwise"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Document structure of a Readwise Reader document
|
||||
// This initial version accepts only the one required field, the URL
|
||||
type Document struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// Client represents a Readwise Reader client.
|
||||
type Client struct {
|
||||
apiKey string
|
||||
}
|
||||
|
||||
// NewClient returns a new Readwise Reader client.
|
||||
func NewClient(apiKey string) *Client {
|
||||
return &Client{apiKey: apiKey}
|
||||
}
|
||||
|
||||
// AddEntry sends an entry to Readwise Reader.
|
||||
func (c *Client) AddEntry(link string) error {
|
||||
if c.apiKey == "" {
|
||||
return fmt.Errorf("readwise: missing credentials")
|
||||
}
|
||||
|
||||
doc := &Document{
|
||||
Url: link,
|
||||
}
|
||||
|
||||
apiURL, err := getAPIEndpoint("https://readwise.io/api/v3/save/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clt := client.New(apiURL)
|
||||
clt.WithAuthorization("Token " + c.apiKey)
|
||||
response, err := clt.PostJSON(doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("readwise: unable to send entry: %v", err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("readwise: unable to send entry, status=%d", response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAPIEndpoint(pathURL string) (string, error) {
|
||||
u, err := url.Parse(pathURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("readwise: invalid API endpoint: %v", err)
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
50
internal/integration/telegrambot/telegrambot.go
Normal file
50
internal/integration/telegrambot/telegrambot.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package telegrambot // import "miniflux.app/v2/internal/integration/telegrambot"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strconv"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
// PushEntry pushes entry to telegram chat using integration settings provided
|
||||
func PushEntry(entry *model.Entry, botToken, chatID string) error {
|
||||
bot, err := tgbotapi.NewBotAPI(botToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("telegrambot: bot creation failed: %w", err)
|
||||
}
|
||||
|
||||
tpl, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>")
|
||||
if err != nil {
|
||||
return fmt.Errorf("telegrambot: template parsing failed: %w", err)
|
||||
}
|
||||
|
||||
var result bytes.Buffer
|
||||
if err := tpl.Execute(&result, entry); err != nil {
|
||||
return fmt.Errorf("telegrambot: template execution failed: %w", err)
|
||||
}
|
||||
|
||||
chatIDInt, _ := strconv.ParseInt(chatID, 10, 64)
|
||||
msg := tgbotapi.NewMessage(chatIDInt, result.String())
|
||||
msg.ParseMode = tgbotapi.ModeHTML
|
||||
msg.DisableWebPagePreview = false
|
||||
|
||||
if entry.CommentsURL != "" {
|
||||
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
|
||||
tgbotapi.NewInlineKeyboardRow(
|
||||
tgbotapi.NewInlineKeyboardButtonURL("Comments", entry.CommentsURL),
|
||||
))
|
||||
}
|
||||
|
||||
if _, err := bot.Send(msg); err != nil {
|
||||
return fmt.Errorf("telegrambot: sending message failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
118
internal/integration/wallabag/wallabag.go
Normal file
118
internal/integration/wallabag/wallabag.go
Normal file
|
@ -0,0 +1,118 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package wallabag // import "miniflux.app/v2/internal/integration/wallabag"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/v2/internal/http/client"
|
||||
)
|
||||
|
||||
// Client represents a Wallabag client.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
clientID string
|
||||
clientSecret string
|
||||
username string
|
||||
password string
|
||||
onlyURL bool
|
||||
}
|
||||
|
||||
// NewClient returns a new Wallabag client.
|
||||
func NewClient(baseURL, clientID, clientSecret, username, password string, onlyURL bool) *Client {
|
||||
return &Client{baseURL, clientID, clientSecret, username, password, onlyURL}
|
||||
}
|
||||
|
||||
// AddEntry sends a link to Wallabag.
|
||||
// Pass an empty string in `content` to let Wallabag fetch the article content.
|
||||
func (c *Client) AddEntry(link, title, content string) error {
|
||||
if c.baseURL == "" || c.clientID == "" || c.clientSecret == "" || c.username == "" || c.password == "" {
|
||||
return fmt.Errorf("wallabag: missing credentials")
|
||||
}
|
||||
|
||||
accessToken, err := c.getAccessToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.createEntry(accessToken, link, title, content)
|
||||
}
|
||||
|
||||
func (c *Client) createEntry(accessToken, link, title, content string) error {
|
||||
endpoint, err := url.JoinPath(c.baseURL, "/api/entries.json")
|
||||
if err != nil {
|
||||
return fmt.Errorf("wallbag: unable to generate entries endpoint using %q: %v", c.baseURL, err)
|
||||
}
|
||||
|
||||
data := map[string]string{"url": link, "title": title}
|
||||
if !c.onlyURL {
|
||||
data["content"] = content
|
||||
}
|
||||
|
||||
clt := client.New(endpoint)
|
||||
clt.WithAuthorization("Bearer " + accessToken)
|
||||
response, err := clt.PostJSON(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("wallabag: unable to post entry using %q endpoint: %v", endpoint, err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return fmt.Errorf("wallabag: request failed using %q, status=%d", endpoint, response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) getAccessToken() (string, error) {
|
||||
values := url.Values{}
|
||||
values.Add("grant_type", "password")
|
||||
values.Add("client_id", c.clientID)
|
||||
values.Add("client_secret", c.clientSecret)
|
||||
values.Add("username", c.username)
|
||||
values.Add("password", c.password)
|
||||
|
||||
endpoint, err := url.JoinPath(c.baseURL, "/oauth/v2/token")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("wallbag: unable to generate token endpoint using %q: %v", c.baseURL, err)
|
||||
}
|
||||
|
||||
clt := client.New(endpoint)
|
||||
response, err := clt.PostForm(values)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("wallabag: unable to get access token using %q endpoint: %v", endpoint, err)
|
||||
}
|
||||
|
||||
if response.HasServerFailure() {
|
||||
return "", fmt.Errorf("wallabag: request failed using %q, status=%d", endpoint, response.StatusCode)
|
||||
}
|
||||
|
||||
token, err := decodeTokenResponse(response.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token.AccessToken, nil
|
||||
}
|
||||
|
||||
type tokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
Expires int `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Scope string `json:"scope"`
|
||||
TokenType string `json:"token_type"`
|
||||
}
|
||||
|
||||
func decodeTokenResponse(body io.Reader) (*tokenResponse, error) {
|
||||
var token tokenResponse
|
||||
|
||||
decoder := json.NewDecoder(body)
|
||||
if err := decoder.Decode(&token); err != nil {
|
||||
return nil, fmt.Errorf("wallabag: unable to decode token response: %v", err)
|
||||
}
|
||||
|
||||
return &token, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue