Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "log"
  7.     "os"
  8.     "os/signal"
  9.     "syscall"
  10.  
  11.     "github.com/dghubble/go-twitter/twitter"
  12.     "github.com/dghubble/oauth1"
  13.     "github.com/robfig/cron"
  14. )
  15.  
  16. func main() {
  17.     consumerKey := "KSgKopvcwQuoMyr19sTFbx2sv"
  18.     consumerSecret := "I2ypitHjRBk8wR2m59VKvDQdG9tJTMleOsodWIMFTmJ85p8KZZ"
  19.     accessToken := "2323966614-hshIJaj2HFDFiRLC1BhbPF1OFYqtTuFBKfcVAPr"
  20.     accessSecret := "xFcbPdyS9MbIh5TDOoqSmunp7mFN2IcxUJUUJt3eJpODP"
  21.  
  22.     config := oauth1.NewConfig(consumerKey, consumerSecret)
  23.     token := oauth1.NewToken(accessToken, accessSecret)
  24.     httpClient := config.Client(oauth1.NoContext, token)
  25.     client := twitter.NewClient(httpClient)
  26.  
  27.     var hashtags = map[string]int{}
  28.  
  29.     // Convenience Demux demultiplexed stream messages
  30.     demux := twitter.NewSwitchDemux()
  31.     demux.Tweet = func(tweet *twitter.Tweet) {
  32.         for _, hashtag := range tweet.Entities.Hashtags {
  33.             hashtags[hashtag.Text]++
  34.         }
  35.     }
  36.  
  37.     demux.DM = func(_ *twitter.DirectMessage) {}
  38.     demux.Event = func(_ *twitter.Event) {}
  39.     fmt.Println("Starting Stream...")
  40.  
  41.     // FILTER
  42.     filterParams := &twitter.StreamFilterParams{
  43.         Track:         []string{"#BNK48"},
  44.         StallWarnings: twitter.Bool(true),
  45.     }
  46.     stream, _ := client.Streams.Filter(filterParams)
  47.  
  48.     // Receive messages until stopped or stream quits
  49.     go demux.HandleChan(stream.Messages)
  50.  
  51.     c := cron.New()
  52.     c.AddFunc("@every 30s", func() {
  53.         b, _ := json.Marshal(hashtags)
  54.         fmt.Printf("%s\n", b)
  55.     })
  56.     c.Start()
  57.  
  58.     // Wait for SIGINT and SIGTERM (HIT CTRL-C)
  59.     ch := make(chan os.Signal)
  60.     signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  61.     log.Println(<-ch)
  62.  
  63.     fmt.Println("Stopping Stream...")
  64.     stream.Stop()
  65.     c.Stop()
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement