publisher.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package publisher
  2. import (
  3. "net/url"
  4. "github.com/ChimeraCoder/anaconda"
  5. log "github.com/go-pkgz/lgr"
  6. "github.com/umputun/rss2twitter/app/rss"
  7. )
  8. // Interface for publishers
  9. type Interface interface {
  10. Publish(event rss.Event, formatter func(rss.Event) string) error
  11. }
  12. // Stdout implements publisher.Interface and sends to stdout
  13. type Stdout struct{}
  14. // Publish to logger
  15. func (s Stdout) Publish(event rss.Event, formatter func(rss.Event) string) error {
  16. log.Printf("[INFO] event - %s", formatter(event))
  17. return nil
  18. }
  19. // Twitter implements publisher.Interface and sends to twitter
  20. type Twitter struct {
  21. ConsumerKey, ConsumerSecret string
  22. AccessToken, AccessSecret string
  23. }
  24. // Publish to twitter
  25. func (t Twitter) Publish(event rss.Event, formatter func(rss.Event) string) error {
  26. log.Printf("[INFO] publish to twitter %+v", event)
  27. api := anaconda.NewTwitterApiWithCredentials(t.AccessToken, t.AccessSecret, t.ConsumerKey, t.ConsumerSecret)
  28. _, err := api.PostTweet(formatter(event), url.Values{})
  29. return err
  30. }