// Hashtags returns a channel over which tweets can be received, which match // at least one of the given hashtags. func Hashtags(cred *Credentials, Hashtags ...string) (<-chan *Tweet, error) { c := make(chan *Tweet) restclient := twittergo.NewClient(&oauth1a.ClientConfig{ ConsumerKey: cred.ConsumerKey, ConsumerSecret: cred.ConsumerSecret, }, &oauth1a.UserConfig{ AccessTokenKey: cred.AccessToken, AccessTokenSecret: cred.AccessSecret, }) httpstream.OauthCon = &oauth.OAuthConsumer{ ConsumerKey: cred.ConsumerKey, ConsumerSecret: cred.ConsumerSecret, } streamclient := httpstream.NewOAuthClient(&oauth.AccessToken{ Token: cred.AccessToken, Secret: cred.AccessSecret, }, newChannelConverter(c, restclient)) err := streamclient.Filter(nil, Hashtags, nil, nil, false, nil) if err != nil { close(c) } return c, err }
// Fills a channel with data from the Twitter gardenhose. func FillStream(channel chan Tweet, consumerKey, consumerSecret, ot, osec string) { httpstream.OauthCon = oauth.NewConsumer( consumerKey, consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{ Token: ot, Secret: osec, } client := httpstream.NewOAuthClient(&at, httpstream.OnlyTweetsFilter(func(line []byte) { channel <- JSONtoTweet(line) })) err := client.Sample(nil) if err != nil { httpstream.Log(httpstream.ERROR, err.Error()) } }
func main() { flag.Parse() httpstream.SetLogger(log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile), *logLevel) // Connectin to the local mongo database session, err := mgo.Dial("localhost") if err != nil { panic(err) } // We defer the session closing so we don't forget later defer session.Close() t := time.Now() collectionName := fmt.Sprintf("%d-%d-%d_%d:%d_%s", t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), *collection) fmt.Println("Collection:", collectionName) // Creating DB "twitter" and Collection "tweets" c := session.DB("mundial").C(collectionName) // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) // make a go channel for sending from listener to processor // we buffer it, to help ensure we aren't backing up twitter or else they cut us off stream := make(chan []byte, 1000) done := make(chan bool) httpstream.OauthCon = oauth.NewConsumer( *consumerKey, *consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{ Token: *ot, Secret: *osec, } // Creamos el cliente OAuth client := httpstream.NewOAuthClient(&at, httpstream.OnlyTweetsFilter(func(line []byte) { stream <- line })) fmt.Println("Cliente OAuth creado") // find list of userids we are going to search for userIds := make([]int64, 0) for _, userId := range strings.Split(*users, ",") { if id, err := strconv.ParseInt(userId, 10, 64); err == nil { userIds = append(userIds, id) } } var keywords []string if search != nil && len(*search) > 0 { keywords = strings.Split(*search, ",") } //err := client.Filter(userIds, keywords, []string{"es"}, nil, false, done) //var locats []string // locats := make([]string, 4) // locats[0] = "-180" // locats[1] = "-90" // locats[2] = "180" // locats[3] = "90" //err = client.Filter(userIds, keywords, nil, locats, false, done) err = client.Filter(userIds, keywords, nil, nil, false, done) //err := client.Filter(nil, keywords, nil, nil, false, done) if err != nil { httpstream.Log(httpstream.ERROR, err.Error()) } else { fmt.Println("Filter realizado, esperando datos") go func() { // while this could be in a different "thread(s)" ct := 0 //var tweet twittertypes.Tweet for tw := range stream { //println("Tweet", ct, "capturado") tweet := new(twittertypes.Tweet) json.Unmarshal(tw, &tweet) tweetstamp, err := time.Parse(time.RubyDate, tweet.Created_at) tweet.Epoch = tweetstamp.Unix() err = c.Insert(tweet) if err != nil { panic(err) } // heavy lifting ct++ if ct >= *maxCt { done <- true } } }() _ = <-done } }
func main() { flag.Parse() httpstream.SetLogger(log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile), *logLevel) // make a go channel for sending from listener to processor // we buffer it, to help ensure we aren't backing up twitter or else they cut us off stream := make(chan []byte, 1000) done := make(chan bool) httpstream.OauthCon = &oauth.OAuthConsumer{ Service: "twitter", RequestTokenURL: "http://twitter.com/oauth/request_token", AccessTokenURL: "http://twitter.com/oauth/access_token", AuthorizationURL: "http://twitter.com/oauth/authorize", ConsumerKey: *ck, ConsumerSecret: *cs, CallBackURL: "oob", UserAgent: "go/httpstream", } //at := goauthcon.GetAccessToken(rt.Token, pin) at := oauth.AccessToken{Id: "", Token: *ot, Secret: *osec, UserRef: *user, Verifier: "", Service: "twitter", } // the stream listener effectively operates in one "thread"/goroutine // as the httpstream Client processes inside a go routine it opens // That includes the handler func we pass in here client := httpstream.NewOAuthClient(&at, httpstream.OnlyTweetsFilter(func(line []byte) { stream <- line // although you can do heavy lifting here, it means you are doing all // your work in the same thread as the http streaming/listener // by using a go channel, you can send the work to a // different thread/goroutine })) keywords := strings.Split("android,golang,zeromq,javascript", ",") err := client.Filter([]int64{14230524, 783214}, keywords, false, done) if err != nil { httpstream.Log(httpstream.ERROR, err.Error()) } else { go func() { // while this could be in a different "thread(s)" ct := 0 for tw := range stream { println(string(tw)) // heavy lifting ct++ if ct > 10 { done <- true } } }() _ = <-done } }
func main() { flag.Parse() if len(*oauthConsumerKey) == 0 || len(*oauthConsumerSecret) == 0 || len(*oauthToken) == 0 || len(*oauthTokenSecret) == 0 || len(*streamSpigotHostname) == 0 || len(*streamSpigotSecret) == 0 { flag.Usage() return } baseUrl := fmt.Sprintf("http://%s/bird-feeder/pinger/", *streamSpigotHostname) followingUrl := fmt.Sprintf("%sfollowing?secret=%s", baseUrl, url.QueryEscape(*streamSpigotSecret)) pingUrl := baseUrl + "ping" var followingUserIds []int64 var followingUserIdMap map[int64]bool stream := make(chan []byte) done := make(chan bool) updateFollowingListTick := time.Tick(followingListUpdateIntervalNanosec) httpstream.OauthCon = &oauth.OAuthConsumer{ Service: "twitter", RequestTokenURL: "http://twitter.com/oauth/request_token", AccessTokenURL: "http://twitter.com/oauth/access_token", AuthorizationURL: "http://twitter.com/oauth/authorize", ConsumerKey: *oauthConsumerKey, ConsumerSecret: *oauthConsumerSecret, CallBackURL: "oob", UserAgent: "go/httpstream", } accessToken := oauth.AccessToken{ Id: "", Token: *oauthToken, Secret: *oauthTokenSecret, Verifier: "", Service: "twitter", } client := httpstream.NewOAuthClient(&accessToken, func(line []byte) { stream <- line }) updateFollowingList := func() { followingUserIds, followingUserIdMap = getFollowingList(followingUrl) fmt.Printf("Tracking updates for %d users...\n", len(followingUserIds)) client.Close() err := client.Filter( followingUserIds, nil, // no topic filter nil, // no language filter nil, // no location filter false, // don't watch for stalls done) if err != nil { fmt.Println(err) } } updateFollowingList() for { select { case <-updateFollowingListTick: updateFollowingList() case <-done: fmt.Printf("Client says it's done") case line := <-stream: switch { case bytes.HasPrefix(line, []byte(`{"event":`)): var event httpstream.Event json.Unmarshal(line, &event) case bytes.HasPrefix(line, []byte(`{"friends":`)): var friends httpstream.FriendList json.Unmarshal(line, &friends) default: tweet := httpstream.Tweet{} json.Unmarshal(line, &tweet) if tweet.User != nil && tweet.User.Id != nil { fmt.Printf("%s: %s\n", tweet.User.ScreenName, tweet.Text) userId := int64(*tweet.User.Id) // We ignore tweets that come from users that we're not following (the // Streaming API will also notify when tweets of theirs are retweeted or // replied to). if _, inMap := followingUserIdMap[userId]; inMap { // Similarly, we ignore tweets that are in reply to users that aren't // being followed. This will have false negatives: if user A follows X // and user B follows X and Z, a reply by X to Z will cause both A and // B's streams to get pinged, even though A won't actually see that // status. However, that should be rare. if tweet.In_reply_to_user_id != nil { if in_reply_to_user_id := int64(*tweet.In_reply_to_user_id); in_reply_to_user_id != 0 { if _, inMap := followingUserIdMap[in_reply_to_user_id]; !inMap { continue } } } go pingUser(userId, int64(*tweet.Id), pingUrl) } } else { fmt.Printf("No tweet?") } } } } }
func main() { flag.Parse() httpstream.SetLogger(log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile), *logLevel) // make a go channel for sending from listener to processor // we buffer it, to help ensure we aren't backing up twitter or else they cut us off stream := make(chan []byte, 1000) done := make(chan bool) httpstream.OauthCon = oauth.NewConsumer( *consumerKey, *consumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{ Token: *ot, Secret: *osec, } // the stream listener effectively operates in one "thread"/goroutine // as the httpstream Client processes inside a go routine it opens // That includes the handler func we pass in here client := httpstream.NewOAuthClient(&at, httpstream.OnlyTweetsFilter(func(line []byte) { stream <- line // although you can do heavy lifting here, it means you are doing all // your work in the same thread as the http streaming/listener // by using a go channel, you can send the work to a // different thread/goroutine })) // find list of userids we are going to search for userIds := make([]int64, 0) for _, userId := range strings.Split(*users, ",") { if id, err := strconv.ParseInt(userId, 10, 64); err == nil { userIds = append(userIds, id) } } var keywords []string if search != nil && len(*search) > 0 { keywords = strings.Split(*search, ",") } err := client.Filter(userIds, keywords, []string{"en"}, nil, false, done) if err != nil { httpstream.Log(httpstream.ERROR, err.Error()) } else { go func() { // while this could be in a different "thread(s)" ct := 0 for tw := range stream { println(string(tw)) // heavy lifting ct++ if ct > *maxCt { done <- true } } }() _ = <-done } }
func main() { conf, err := loadConfig("config.json") if err != nil { fmt.Println("Error loading config!") fmt.Println(err) return } configuration = conf p.RegisterKey(configuration.ProwlKey) two = twilio.NewClient(configuration.TwilioSID, configuration.TwilioAuthToken, nil) if two == nil { fmt.Println("Couldn't set up Twilio") return } amzses.Init(configuration.AWSAccessKey, configuration.AWSSecretKey) fmt.Println("Configured SES") db, err = database.Connect(configuration.MongoURL) if err != nil { fmt.Println("Couldn't connect to Mongo database") fmt.Println(err) return } fmt.Println("Connected to MongoDB") cheers := time.NewTicker(10 * time.Minute) scrapeInfo := time.NewTicker(2 * time.Minute) go func() { for { select { case <-cheers.C: events, err := db.GetEventsForCurrentWeek() if err != nil { sendProwl("Time Error", "Couldn't get current events when sending Cheers", err) break } for _, e := range events { sendCheersForEvent(e) } case <-scrapeInfo.C: events, err := db.GetEventsForCurrentWeek() if err != nil { sendProwl("Time Error", "Couldn't get current events when scraping event info", err) break } for _, e := range events { if !e.AlliancesSent { sendAlliancesForEvent(e) } sendAwardsForEvent(e) } } } }() stream := make(chan []byte, 1000) done := make(chan bool) httpstream.OauthCon = oauth.NewConsumer( configuration.ConsumerKey, configuration.ConsumerSecret, oauth.ServiceProvider{ RequestTokenUrl: "http://api.twitter.com/oauth/request_token", AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", AccessTokenUrl: "https://api.twitter.com/oauth/access_token", }) at := oauth.AccessToken{ Token: configuration.OAuthToken, Secret: configuration.OAuthSecret, } client := httpstream.NewOAuthClient(&at, httpstream.OnlyTweetsFilter(func(line []byte) { stream <- line })) fmt.Println("Connected to Twitter!") err = client.User(done) if err != nil { sendProwl("Error Starting Megaphone", "Couldn't start stream!", err) return } sendProwl("Started!", "Megaphone is running", nil) go func() { for line := range stream { handleLine(line) } }() _ = <-done }