Guest User

Untitled

a guest
Nov 20th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import (
  2. "net/url"
  3. "os"
  4.  
  5. "github.com/ChimeraCoder/anaconda"
  6. "github.com/Sirupsen/logrus"
  7. )
  8.  
  9. var (
  10. consumerKey = getenv("TWITTER_CONSUMER_KEY")
  11. consumerSecret = getenv("TWITTER_CONSUMER_SECRET")
  12. accessToken = getenv("TWITTER_ACCESS_TOKEN")
  13. accessTokenSecret = getenv("TWITTER_ACCESS_TOKEN_SECRET")
  14. )
  15.  
  16. type logger struct {
  17. *logrus.Logger
  18. }
  19.  
  20. func (log *logger) Critical(args ...interface{}) {
  21. log.Error(args...)
  22. }
  23.  
  24. func (log *logger) Criticalf(format string, args ...interface{}) {
  25. log.Errorf(format, args...)
  26. }
  27.  
  28. func (log *logger) Notice(args ...interface{}) {
  29. log.Info(args...)
  30. }
  31.  
  32. func (log *logger) Noticef(format string, args ...interface{}) {
  33. log.Infof(format, args...)
  34. }
  35.  
  36. func getenv(name string) string {
  37. v := os.Getenv(name)
  38. if v == "" {
  39. panic("environment variable not set " + name)
  40. }
  41. return v
  42.  
  43. }
  44.  
  45. func main() {
  46. anaconda.SetConsumerKey(consumerKey)
  47. anaconda.SetConsumerSecret(consumerSecret)
  48. api := anaconda.NewTwitterApi(accessToken, accessTokenSecret)
  49.  
  50. log := &logger{logrus.New()}
  51. api.SetLogger(log)
  52.  
  53. stream := api.PublicStreamFilter(url.Values{
  54. "track": []string{"#2017In4Words"},
  55. })
  56.  
  57. defer stream.Stop()
  58.  
  59. for v := range stream.C {
  60. t, ok := v.(anaconda.Tweet)
  61. if !ok {
  62. log.Warningf("unexpected value type %T", v)
  63. continue
  64. }
  65.  
  66. if t.RetweetedStatus != nil {
  67. continue
  68. }
  69.  
  70. //log.Info(t.Text)
  71.  
  72. _, err := api.Retweet(t.Id, false)
  73. if err != nil {
  74. log.Errorf("could not retweet %d %v", t.Id, err)
  75. continue
  76. }
  77. log.Infof("retweeted %d ", t.Id)
  78.  
  79. rt, err := api.Favorite(t.Id)
  80. if err != nil {
  81. log.Errorf("could not favorite tweet %d ", rt.Id)
  82. }
  83. log.Infof("favorited %d ", rt.Id)
  84.  
  85. }
  86.  
  87. }
Add Comment
Please, Sign In to add comment