func (tc *TwitterController) GetSearchStream(api *anaconda.TwitterApi, query string) chan anaconda.Tweet { c := make(chan anaconda.Tweet) go func(chanNotify chan anaconda.Tweet) { var since_id int64 for { v := url.Values{} v.Set("local", "ja") v.Set("count", "20") if since_id > 0 { ssince_id := strconv.FormatInt(since_id, 10) v.Set("since_id", ssince_id) } resp, err := api.GetSearch(query, v) if err == nil { for _, status := range resp.Statuses { chanNotify <- status if status.Id > since_id { since_id = status.Id } } } else { log.Printf("An error occured while searching. err:%v", err) } time.Sleep(time.Second * 30) } }(c) return c }
func twitterHandler(api *anaconda.TwitterApi) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { re := regexp.MustCompile("(http|ftp|https)://([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?") query := r.URL.Query().Get("q") v := url.Values{} //v.Set("f", "news") v.Set("lang", "en") v.Set("count", "100") result, err := api.GetSearch(query, v) if err != nil { handleWebErr(w, err) return } var results []string for _, tweet := range result.Statuses { results = append(results, re.ReplaceAllLiteralString(tweet.Text, "")) } msg, err := json.Marshal(results) if err != nil { handleWebErr(w, err) return } w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, string(msg)) } }
func twitterSearch(twitter *anaconda.TwitterApi, query string) ([]anaconda.Tweet, error) { v := url.Values{} //v.Set("f", "news") v.Set("lang", "en") v.Set("count", "100") result, err := twitter.GetSearch(query, v) if err != nil { return result.Statuses, err } return result.Statuses, nil }
func doSearch(api *anaconda.TwitterApi, topic string) { // Test that the GetSearch function actually works and returns non-empty results search_result, err := api.GetSearch(topic, nil) if err != nil { ui.Error(fmt.Sprintf("GetSearch yielded error %v", err)) panic(err) } // Unless something is seriously wrong, there should be at least two tweets if len(search_result.Statuses) < 2 { ui.Error(fmt.Sprintf("Expected 2 or more tweets, and found %d", len(search_result.Statuses))) } // Check that at least one tweet is non-empty for i, tweet := range search_result.Statuses { if tweet.Text != "" { ui.Info(fmt.Sprintf("[%d] %s", i, tweet.Text)) } } }
func loadRecentTweets(api *anaconda.TwitterApi, conf *Config) error { val := url.Values{ "count": []string{"100"}, "include_entities": []string{"true"}, } for _, tsi := range conf.StreamInfo { tweets, err := api.GetSearch(tsi.GetTweetFilter(), val) if err != nil { return err } for _, t := range tweets.Statuses { tweet, err := convertTweet(t) if err != nil { // log here continue } tsi.Add(&tweet) } } return nil }