func main() { // Load credentials loadCredentials() // Initialize Twitter streaming client. twitterStream := twitterstream.NewClient(config.ConsumerKey, config.ConsumerSecret, config.AccessToken, config.AccessSecret) // Initialize e-mail "client" gmail = gogmail.GmailConnection(config.GmailUser, config.GmailPassword) for { stream, err := twitterStream.Track(*topics) if err != nil { log.Printf("Connecting to the streaming API failed, reconnecting in %s...\n", RECONNECT_TIME) time.Sleep(RECONNECT_TIME) continue } for { tweet, err := stream.Next() if err != nil { log.Println("Connection died with error:", err) log.Printf("Reconnecting in %s...\n", RECONNECT_TIME) break } // Handle the Tweet that just came in handleIncomingTweet(tweet) } // We sleep a while before reconnecting to keep Twitter happy. time.Sleep(RECONNECT_TIME) } }
func (tp *TwitterPlugin) Initialise() error { if err := ReadPluginConfig("twitter_plugin_config.json", &tp.config); err != nil { return err } if tp.config.ConsumerKey == "" || tp.config.ConsumerSecret == "" || tp.config.AccessToken == "" || tp.config.AccessSecret == "" { return errors.New("Not all twitter secrets defined") } tp.Subscribed = make(map[*Client]byte) tp.client = twitterstream.NewClient(tp.config.ConsumerKey, tp.config.ConsumerSecret, tp.config.AccessToken, tp.config.AccessSecret) return nil }
func main() { client := twitterstream.NewClient("FyrFbLeZYyTu8pwpIw4YVA", "Pw2SjQIDSP5MbRu5aDVpIYptwqPSOpTMIsFrRFWJMy4", "264661368-zyGo2jqkD4FKNHwWIHbcKQsX2cSTqWA3jEJnPozc", "UXxRfmlPtnCE3bjHmLrPpOnPbA2LAnoVllv3ACuUrL1Cl") for { conn, err := client.Track("scala,akka") if err != nil { log.Println("Tracking failed, sleeping for 1 minute") time.Sleep(1 * time.Minute) continue } decode(conn) } }
func startStreaming() { //start stream listener fmt.Println("start twitter stream", consumerKey, consumerSecret, accessToken, accessSecret) client := twitterstream.NewClient(consumerKey, consumerSecret, accessToken, accessSecret) for { conn, err := client.Track(os.Getenv("hashtag")) if err != nil { fmt.Println("Tracking failed, sleeping for 1 minute %s", err) log.Println("Tracking failed, sleeping for 1 minute") time.Sleep(1 * time.Minute) continue } decode(conn) } }
func StreamTweets(messages <-chan bool) { var err error c, err = redis.Dial("tcp", ":6379") if err != nil { panic(err) } c.Do("AUTH", "foobared") trimThreshold, err = redis.Int(c.Do("LLEN", "tweets")) if err != nil { fmt.Println("Some error occured") } if trimThreshold >= trimCount { trimTweets() } consumerKey := os.Getenv("CONSUMERKEY") consumerSecret := os.Getenv("CONSUMERSECRET") accessToken := os.Getenv("ACCESSTOKEN") accessSecret := os.Getenv("ACCESSSECRET") keywords := "why,when,how,then" client := twitterstream.NewClient(consumerKey, consumerSecret, accessToken, accessSecret) for { log.Printf("tracking keywords %s", keywords) conn, err := client.Track(keywords) if err != nil { log.Printf("tracking failed: %s", err) wait = wait << 1 log.Printf("waiting for %d seconds before reconnect", min(wait, maxWait)) time.Sleep(time.Duration(min(wait, maxWait)) * time.Second) continue } else { wait = 1 } stop := decodeTweets(conn, messages) if stop { log.Printf("stopping") break } } }
func main() { client := twitterstream.NewClient(*consumerKey, *consumerSecret, *accessToken, *accessSecret) for { log.Printf("tracking keywords %s", *keywords) conn, err := client.Track(*keywords) if err != nil { log.Printf("tracking failed: %s", err) wait = wait << 1 log.Printf("waiting for %d seconds before reconnect", min(wait, maxWait)) time.Sleep(time.Duration(min(wait, maxWait)) * time.Second) continue } else { wait = 1 } decodeTweets(conn) } }
func loves(c *cli.Context) { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } client := twitterstream.NewClient( os.Getenv("TW_CONSUMER_KEY"), os.Getenv("TW_CONSUMER_SECRET"), os.Getenv("TW_CLIENT_KEY"), os.Getenv("TW_CLIENT_SECRET"), ) for { conn, err := client.Follow(string(YURIKO_ID)) if err != nil { time.Sleep(1 * time.Minute) continue } kanojoru(conn) } }
func ServerAction(c *cli.Context) { server := Server{ TwitterClient: twitterstream.NewClient(c.String("twitter-consumer-key"), c.String("twitter-consumer-secret"), c.String("twitter-access-token"), c.String("twitter-access-secret")), RedisClient: redis.NewClient(&redis.Options{ Addr: c.String("redis-server"), Password: c.String("redis-password"), DB: 0, MaxRetries: 1000, }), Track: c.String("twitter-track"), Messages: make(chan twitterstream.Tweet, 42), Done: make(chan bool, 1), } defer server.RedisClient.Close() logrus.Infof("Starting ReadFromStream routine...") go server.ReadFromStream() logrus.Infof("Starting WriteToRedis routine...") go server.WriteToRedis() <-server.Done logrus.Fatalf("Done received") }
// read each message from twitter func (d *Twitter) readTwitter() (err error) { d.client = twitterstream.NewClient(d.consumerKey, d.consumerSecret, d.accessToken, d.accessSecret) for { var conn *twitterstream.Connection if d.keywords == "" { log.Println("Sampling Twitter") conn, err = d.client.Sample() } else { log.Printf("Tracking %s", d.keywords) conn, err = d.client.Track(d.keywords) } if err == nil { for { if tweet, err := conn.Next(); err == nil { doc := structs.Map(tweet) msg := message.NewMsg(message.Insert, doc) if msg != nil { d.pipe.Send(msg) } else { break } } else { break } } } else { log.Println("Sleeping before reconnecting") time.Sleep(time.Duration(10 * time.Second)) } } }
func main() { flag.Parse() if *filterText == "" { isFilterText() } client := twitterstream.NewClient(twiConnectConf.ConsumerKey, twiConnectConf.ConsumerSecret, twiConnectConf.AccessToken, twiConnectConf.AccessTokenSecret) ct.ChangeColor(ct.Green, true, ct.None, false) fmt.Println(timeNow, " Client connected to twitter") fmt.Println(timeNow, " Start for parsing text: ", *filterText) for { conn, err := client.Track(*filterText) if err != nil { ct.ChangeColor(ct.Red, true, ct.None, false) log.Println(timeNow, " Tracking failed, sleeping for 1 minute") ct.ResetColor() time.Sleep(1 * time.Minute) continue } decode(conn) } }