Skip to content

Commit

Permalink
a lot of small fixes post #15
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 6, 2019
1 parent 5121af2 commit 22e7cf9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 62 deletions.
16 changes: 9 additions & 7 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

var opts struct {
DB string `short:"c" long:"db" env:"FM_DB" default:"var/feed-master.bdb" description:"bolt db file"`
Conf string `short:"f" long:"conf" env:"FM_CONF" default:"feed-master.yml" description:"config file (yml)"`
Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"`
TG string `long:"telegram_token" env:"TELEGRAM_TOKEN" description:"Telegram token"`
TelegramTimeout int64 `long:"telegram_timeout" env:"TELEGRAM_TIMEOUT" description:"Telegram timeout"`
DB string `short:"c" long:"db" env:"FM_DB" default:"var/feed-master.bdb" description:"bolt db file"`
Conf string `short:"f" long:"conf" env:"FM_CONF" default:"feed-master.yml" description:"config file (yml)"`
TelegramToken string `long:"telegram_token" env:"TELEGRAM_TOKEN" description:"telegram token"`
TelegramTimeout time.Duration `long:"telegram_timeout" env:"TELEGRAM_TIMEOUT" default:"1m" description:"telegram timeout"`
Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"`
}

var revision = "local"
Expand All @@ -35,13 +35,15 @@ func main() {
if err != nil {
log.Fatalf("[ERROR] can't load config %s, %v", opts.Conf, err)
}

db, err := proc.NewBoltDB(opts.DB)
if err != nil {
log.Fatalf("[ERROR] can't open db %s, %v", opts.DB, err)
}
tg, err := proc.NewTelegramClient(opts.TG, time.Duration(opts.TelegramTimeout))

tg, err := proc.NewTelegramClient(opts.TelegramToken, opts.TelegramTimeout)
if err != nil {
log.Fatalf("[ERROR] failed initilization telegram client %s, %v", opts.TG, err)
log.Fatalf("[ERROR] failed to initialize telegram client %s, %v", opts.TelegramToken, err)
}

p := &proc.Processor{Conf: conf, Store: db, Notification: tg}
Expand Down
6 changes: 3 additions & 3 deletions app/proc/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func (p *Processor) Do() {
swg := syncs.NewSizedGroup(p.Conf.System.Concurrent, syncs.Preemptive)
for name, fm := range p.Conf.Feeds {
for _, src := range fm.Sources {
name, src, fm := name, src, fm
swg.Go(func(_ context.Context) {
p.feed(name, src.URL, fm.TelegramChannel, p.Conf.System.MaxItems)
name, src, tgChan := name, src, fm.TelegramChannel
swg.Go(func(context.Context) {
p.feed(name, src.URL, tgChan, p.Conf.System.MaxItems)
})
}
// keep up to MaxKeepInDB items in bucket
Expand Down
35 changes: 17 additions & 18 deletions app/proc/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import (

log "github.com/go-pkgz/lgr"
"github.com/microcosm-cc/bluemonday"
"github.com/pkg/errors"
tb "gopkg.in/tucnak/telebot.v2"

"github.com/umputun/feed-master/app/feed"
)

const (
maxTelegramFileSize = 50_000_000
)
const maxTelegramFileSize = 50_000_000

// TelegramClient client
type TelegramClient struct {
Expand Down Expand Up @@ -54,18 +53,17 @@ func NewTelegramClient(token string, timeout time.Duration) (*TelegramClient, er
}

// Send message, skip if telegram token empty
func (client TelegramClient) Send(channelID string, item feed.Item) error {
if client.Bot == nil {
return nil
}
func (client TelegramClient) Send(channelID string, item feed.Item) (err error) {

if channelID == "" {
if client.Bot == nil || channelID == "" {
return nil
}

contentLength, err := getContentLength(item.Enclosure.URL)
if err != nil {
return err
contentLength := item.Enclosure.Length
if contentLength <= 0 {
if contentLength, err = getContentLength(item.Enclosure.URL); err != nil {
return errors.Wrapf(err, "can't get length for %s", item.Enclosure.URL)
}
}

var message *tb.Message
Expand All @@ -77,27 +75,28 @@ func (client TelegramClient) Send(channelID string, item feed.Item) error {
}

if err != nil {
return err
return errors.Wrapf(err, "can't send to telegram for %+v", item.Enclosure)
}

log.Printf("[DEBUG] send telegram message: \n%s", message.Text)
return err
log.Printf("[DEBUG] telegram message sent: \n%s", message.Text)
return nil
}

func getContentLength(url string) (int64, error) {
// getContentLength uses HEAD request and called as a fallback in case of item.Enclosure.Length not populated
func getContentLength(url string) (int, error) {
resp, err := http.Head(url) //nolint:gosec
if err != nil {
return 0, err
return 0, errors.Wrapf(err, "can't HEAD %s", url)
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
return 0, fmt.Errorf("resp.StatusCode: %d, not equal 200", resp.StatusCode)
return 0, errors.Errorf("non-200 status, %d", resp.StatusCode)
}

log.Printf("[DEBUG] Content-Length: %d, url: %s", resp.ContentLength, url)
return resp.ContentLength, err
return int(resp.ContentLength), err
}

func (client TelegramClient) sendText(channelID string, item feed.Item) (*tb.Message, error) {
Expand Down
60 changes: 26 additions & 34 deletions app/proc/telegram_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package proc

import (
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tb "gopkg.in/tucnak/telebot.v2"

"github.com/umputun/feed-master/app/feed"
)

func TestNewTelegramClientIfTokenEmpty(t *testing.T) {
client, err := NewTelegramClient("", 0)

assert.Nil(t, err)
assert.NoError(t, err)
assert.Nil(t, client.Bot)
}

func TestNewTelegramClientCheckTimeout(t *testing.T) {
cases := []struct {
tbl := []struct {
timeout, expected time.Duration
}{
{0, 600},
Expand All @@ -27,33 +28,29 @@ func TestNewTelegramClientCheckTimeout(t *testing.T) {
}

//nolint:scopelint
for _, tc := range cases {
t.Run("", func(t *testing.T) {
client, err := NewTelegramClient("", tc.timeout)

assert.Nil(t, err)
assert.Equal(t, tc.expected, client.Timeout)
for i, tt := range tbl {
t.Run(strconv.Itoa(i), func(t *testing.T) {
client, err := NewTelegramClient("", tt.timeout)
assert.NoError(t, err)
assert.Equal(t, tt.expected, client.Timeout)
})
}
}

func TestSendIfBotIsNil(t *testing.T) {
client, err := NewTelegramClient("", 0)

got := client.Send("@channel", feed.Item{})

assert.Nil(t, err)
assert.Nil(t, got)
require.NoError(t, err)
err = client.Send("@channel", feed.Item{})
assert.NoError(t, err)
}

func TestSendIfChannelIDEmpty(t *testing.T) {
client := TelegramClient{
Bot: &tb.Bot{},
}

got := client.Send("", feed.Item{})

assert.Nil(t, got)
err := client.Send("", feed.Item{})
assert.NoError(t, err)
}

func TestTagLinkOnlySupport(t *testing.T) {
Expand All @@ -78,10 +75,8 @@ func TestTagLinkOnlySupport(t *testing.T) {
<a href="https://podcast.umputun.com/media/ump_podcast437.mp3">аудио</a>`

client := TelegramClient{}

got := client.tagLinkOnlySupport(html)

assert.Equal(t, got, htmlExpected, "support only html tag a")
assert.Equal(t, htmlExpected, got, "support only html tag a")
}

func TestGetMessageHTML(t *testing.T) {
Expand All @@ -96,26 +91,24 @@ func TestGetMessageHTML(t *testing.T) {
expected := "Podcast\n\nNews <a href=\"#\">Podcast Link</a>\n\nhttps://example.com"

client := TelegramClient{}
got := client.getMessageHTML(item)

assert.Equal(t, got, expected)
msg := client.getMessageHTML(item)
assert.Equal(t, expected, msg)
}

func TestRecipientChannelIDNotStartWithAt(t *testing.T) {
cases := []string{"channel", "@channel"}
expected := "@channel"

for _, channelID := range cases {
t.Run("", func(t *testing.T) {
for i, channelID := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := recipient{chatID: channelID} //nolint

assert.Equal(t, got.Recipient(), expected)
assert.Equal(t, expected, got.Recipient())
})
}
}

func TestGetFilenameByURL(t *testing.T) {
cases := []struct {
tbl := []struct {
url, expected string
}{
{"https://example.com/100500/song.mp3", "song.mp3"},
Expand All @@ -125,13 +118,12 @@ func TestGetFilenameByURL(t *testing.T) {
{"https://example.com/", ""},
}

//nolint:scopelint
for _, tc := range cases {
t.Run("", func(t *testing.T) {
// nolint:scopelint
for i, tt := range tbl {
t.Run(strconv.Itoa(i), func(t *testing.T) {
client := TelegramClient{}
got := client.getFilenameByURL(tc.url)

assert.Equal(t, got, tc.expected)
fname := client.getFilenameByURL(tt.url)
assert.Equal(t, tt.expected, fname)
})
}
}

0 comments on commit 22e7cf9

Please sign in to comment.