Minor improvements in integration package

This commit is contained in:
Frédéric Guillot 2021-09-07 20:28:41 -07:00 committed by fguillot
parent 34dd358eb0
commit 49119eff00
16 changed files with 57 additions and 108 deletions

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package integration implements API clients for third-party services.
*/
package integration // import "miniflux.app/integration"

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package instapaper provides an integration with Instapaper.
*/
package instapaper // import "miniflux.app/integration/instapaper"

View file

@ -17,6 +17,11 @@ type Client struct {
password 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. // AddURL sends a link to Instapaper.
func (c *Client) AddURL(link, title string) error { func (c *Client) AddURL(link, title string) error {
if c.username == "" || c.password == "" { if c.username == "" || c.password == "" {
@ -41,8 +46,3 @@ func (c *Client) AddURL(link, title string) error {
return nil return nil
} }
// NewClient returns a new Instapaper client.
func NewClient(username, password string) *Client {
return &Client{username: username, password: password}
}

View file

@ -16,9 +16,11 @@ import (
"miniflux.app/model" "miniflux.app/model"
) )
// SendEntry send the entry to the activated providers. // SendEntry sends the entry to third-party providers when the user click on "Save".
func SendEntry(entry *model.Entry, integration *model.Integration) { func SendEntry(entry *model.Entry, integration *model.Integration) {
if integration.PinboardEnabled { 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) client := pinboard.NewClient(integration.PinboardToken)
err := client.AddBookmark( err := client.AddBookmark(
entry.URL, entry.URL,
@ -33,6 +35,8 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
} }
if integration.InstapaperEnabled { 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) client := instapaper.NewClient(integration.InstapaperUsername, integration.InstapaperPassword)
if err := client.AddURL(entry.URL, entry.Title); err != nil { if err := client.AddURL(entry.URL, entry.Title); err != nil {
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err) logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
@ -40,6 +44,8 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
} }
if integration.WallabagEnabled { if integration.WallabagEnabled {
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to Wallabag", entry.ID, entry.URL, integration.UserID)
client := wallabag.NewClient( client := wallabag.NewClient(
integration.WallabagURL, integration.WallabagURL,
integration.WallabagClientID, integration.WallabagClientID,
@ -54,6 +60,8 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
} }
if integration.NunuxKeeperEnabled { if integration.NunuxKeeperEnabled {
logger.Debug("[Integration] Sending Entry #%d %q for User #%d to NunuxKeeper", entry.ID, entry.URL, integration.UserID)
client := nunuxkeeper.NewClient( client := nunuxkeeper.NewClient(
integration.NunuxKeeperURL, integration.NunuxKeeperURL,
integration.NunuxKeeperAPIKey, integration.NunuxKeeperAPIKey,
@ -65,6 +73,8 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
} }
if integration.PocketEnabled { 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) client := pocket.NewClient(config.Opts.PocketConsumerKey(integration.PocketConsumerKey), integration.PocketAccessToken)
if err := client.AddURL(entry.URL, entry.Title); err != nil { if err := client.AddURL(entry.URL, entry.Title); err != nil {
logger.Error("[Integration] UserID #%d: %v", integration.UserID, err) logger.Error("[Integration] UserID #%d: %v", integration.UserID, err)
@ -72,11 +82,11 @@ func SendEntry(entry *model.Entry, integration *model.Integration) {
} }
} }
// PushEntry pushes new entry to the activated providers. // PushEntry pushes an entry to third-party providers during feed refreshes.
// This function should be wrapped in a goroutine to avoid block of program execution.
func PushEntry(entry *model.Entry, integration *model.Integration) { func PushEntry(entry *model.Entry, integration *model.Integration) {
if integration.TelegramBotEnabled { if integration.TelegramBotEnabled {
logger.Debug("[Integration] Sending Entry #%d for User #%d to telegram", entry.ID, integration.UserID) logger.Debug("[Integration] Sending Entry %q for User #%d to Telegram", entry.URL, integration.UserID)
err := telegrambot.PushEntry(entry, integration.TelegramBotToken, integration.TelegramBotChatID) err := telegrambot.PushEntry(entry, integration.TelegramBotToken, integration.TelegramBotChatID)
if err != nil { if err != nil {
logger.Error("[Integration] push entry to telegram bot failed: %v", err) logger.Error("[Integration] push entry to telegram bot failed: %v", err)

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package nunuxkeeper provides an integration with the Nunux Keeper application.
*/
package nunuxkeeper // import "miniflux.app/integration/nunuxkeeper"

View file

@ -26,6 +26,11 @@ type Client struct {
apiKey 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. // AddEntry sends an entry to Nunux Keeper.
func (c *Client) AddEntry(link, title, content string) error { func (c *Client) AddEntry(link, title, content string) error {
if c.baseURL == "" || c.apiKey == "" { if c.baseURL == "" || c.apiKey == "" {
@ -58,11 +63,6 @@ func (c *Client) AddEntry(link, title, content string) error {
return nil return nil
} }
// NewClient returns a new Nunux Keeepr client.
func NewClient(baseURL, apiKey string) *Client {
return &Client{baseURL: baseURL, apiKey: apiKey}
}
func getAPIEndpoint(baseURL, pathURL string) (string, error) { func getAPIEndpoint(baseURL, pathURL string) (string, error) {
u, err := url.Parse(baseURL) u, err := url.Parse(baseURL)
if err != nil { if err != nil {

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package pinboard provides an integration with Pinboard.
*/
package pinboard // import "miniflux.app/integration/pinboard"

View file

@ -16,6 +16,11 @@ type Client struct {
authToken string authToken string
} }
// NewClient returns a new Pinboard client.
func NewClient(authToken string) *Client {
return &Client{authToken: authToken}
}
// AddBookmark sends a link to Pinboard. // AddBookmark sends a link to Pinboard.
func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error { func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error {
if c.authToken == "" { if c.authToken == "" {
@ -46,8 +51,3 @@ func (c *Client) AddBookmark(link, title, tags string, markAsUnread bool) error
return nil return nil
} }
// NewClient returns a new Pinboard client.
func NewClient(authToken string) *Client {
return &Client{authToken: authToken}
}

View file

@ -18,6 +18,11 @@ type Connector struct {
consumerKey string 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. // RequestToken fetches a new request token from Pocket API.
func (c *Connector) RequestToken(redirectURL string) (string, error) { func (c *Connector) RequestToken(redirectURL string) (string, error) {
type req struct { type req struct {
@ -96,8 +101,3 @@ func (c *Connector) AuthorizationURL(requestToken, redirectURL string) string {
redirectURL, redirectURL,
) )
} }
// NewConnector returns a new Pocket Connector.
func NewConnector(consumerKey string) *Connector {
return &Connector{consumerKey}
}

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package pocket provides an integration with Pocket.
*/
package pocket // import "miniflux.app/integration/pocket"

View file

@ -16,6 +16,11 @@ type Client struct {
accessToken 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. // AddURL sends a single link to Pocket.
func (c *Client) AddURL(link, title string) error { func (c *Client) AddURL(link, title string) error {
if c.consumerKey == "" || c.accessToken == "" { if c.consumerKey == "" || c.accessToken == "" {
@ -48,8 +53,3 @@ func (c *Client) AddURL(link, title string) error {
return nil return nil
} }
// NewClient returns a new Pocket client.
func NewClient(consumerKey, accessToken string) *Client {
return &Client{consumerKey, accessToken}
}

View file

@ -1,2 +0,0 @@
// Package telegrambot provides a simple entry-to-telegram push
package telegrambot

View file

@ -1,4 +1,8 @@
package telegrambot // Copyright 2021 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package telegrambot // import "miniflux.app/integration/telegrambot"
import ( import (
"bytes" "bytes"
@ -14,27 +18,25 @@ import (
func PushEntry(entry *model.Entry, botToken, chatID string) error { func PushEntry(entry *model.Entry, botToken, chatID string) error {
bot, err := tgbotapi.NewBotAPI(botToken) bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil { if err != nil {
return fmt.Errorf("telegrambot: create bot failed: %w", err) return fmt.Errorf("telegrambot: bot creation failed: %w", err)
} }
t, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>") tpl, err := template.New("message").Parse("{{ .Title }}\n<a href=\"{{ .URL }}\">{{ .URL }}</a>")
if err != nil { if err != nil {
return fmt.Errorf("telegrambot: parse template failed: %w", err) return fmt.Errorf("telegrambot: template parsing failed: %w", err)
} }
var result bytes.Buffer var result bytes.Buffer
if err := tpl.Execute(&result, entry); err != nil {
err = t.Execute(&result, entry) return fmt.Errorf("telegrambot: template execution failed: %w", err)
if err != nil {
return fmt.Errorf("telegrambot: execute template failed: %w", err)
} }
chatId, _ := strconv.ParseInt(chatID, 10, 64) chatIDInt, _ := strconv.ParseInt(chatID, 10, 64)
msg := tgbotapi.NewMessage(chatId, result.String()) msg := tgbotapi.NewMessage(chatIDInt, result.String())
msg.ParseMode = tgbotapi.ModeHTML msg.ParseMode = tgbotapi.ModeHTML
msg.DisableWebPagePreview = false msg.DisableWebPagePreview = false
if _, err := bot.Send(msg); err != nil { if _, err := bot.Send(msg); err != nil {
return fmt.Errorf("telegrambot: send message failed: %w", err) return fmt.Errorf("telegrambot: sending message failed: %w", err)
} }
return nil return nil

View file

@ -1,10 +0,0 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
/*
Package wallabag provides an integration with the Wallabag application.
*/
package wallabag // import "miniflux.app/integration/wallabag"

View file

@ -22,6 +22,11 @@ type Client struct {
password string password string
} }
// NewClient returns a new Wallabag client.
func NewClient(baseURL, clientID, clientSecret, username, password string) *Client {
return &Client{baseURL, clientID, clientSecret, username, password}
}
// AddEntry sends a link to Wallabag. // AddEntry sends a link to Wallabag.
// Pass an empty string in `content` to let Wallabag fetch the article content. // Pass an empty string in `content` to let Wallabag fetch the article content.
func (c *Client) AddEntry(link, title, content string) error { func (c *Client) AddEntry(link, title, content string) error {
@ -88,11 +93,6 @@ func (c *Client) getAccessToken() (string, error) {
return token.AccessToken, nil return token.AccessToken, nil
} }
// NewClient returns a new Wallabag client.
func NewClient(baseURL, clientID, clientSecret, username, password string) *Client {
return &Client{baseURL, clientID, clientSecret, username, password}
}
func getAPIEndpoint(baseURL, path string) (string, error) { func getAPIEndpoint(baseURL, path string) (string, error) {
u, err := url.Parse(baseURL) u, err := url.Parse(baseURL)
if err != nil { if err != nil {

View file

@ -153,7 +153,6 @@
</div> </div>
</div> </div>
</form> </form>
<h3>{{ t "page.integration.bookmarklet" }}</h3> <h3>{{ t "page.integration.bookmarklet" }}</h3>