예제 #1
0
func twitterSearch(tweetCh chan *twitterstream.Tweet, doneCh chan bool, query string) {
	// Twitter streaming APIに接続_
	client := twitterstream.NewClientTimeout(
		*consumerKey,
		*consumerSecret,
		*accessToken,
		*accessTokenSecret,
		timeout,
	)
	conn, err := client.Track(query)
	if err != nil {
		log.Printf("Tracking failed: %s", err)
		return
	}
	defer conn.Close()

	for {
		if tweet, err := conn.Next(); err == nil {
			tweetCh <- tweet
		} else {
			log.Printf("Decoding tweet failed: %s", err)
			break
		}
	}
	doneCh <- true
}
예제 #2
0
// Twitter検索
func twitterSearchHandler(ws *websocket.Conn) {
	defer ws.Close()

	// 検索キーワードの取得
	req := ws.Request() // http.Requestが返る
	query := req.FormValue("q")
	log.Printf("query: %s", query)

	// Twitter streaming APIに接続
	client := twitterstream.NewClientTimeout(
		*consumerKey,
		*consumerSecret,
		*accessToken,
		*accessTokenSecret,
		timeout,
	)
	conn, err := client.Track(query)
	if err != nil {
		log.Printf("Tracking failed: %s", err)
		return
	}
	defer conn.Close()

	for {
		if tweet, err := conn.Next(); err == nil {
			// Tweetが公式Retweetだった場合はなにもしない
			if tweet.RetweetedStatus != nil {
				continue
			}

			// Websocketに流すJSONを作成
			data := WSTweet{
				tweet.Text,
				tweet.User.Name,
				tweet.User.ScreenName,
				tweet.User.ProfileImageUrl,
			}
			json, _ := json.Marshal(data)

			// Websocketに流す
			_, err = ws.Write(json)
			if err != nil {
				log.Printf("Writing to Websocket failed: %s", err)
				return
			}
		} else {
			log.Printf("Decoding tweet failed: %s", err)
			return
		}
	}
}