Esempio n. 1
0
func getTweetsSync(c RequestContext, bookList []Book) {
	anaconda.SetConsumerKey(twitterConsumerKey)
	anaconda.SetConsumerSecret(twitterConsumerSecret)
	api := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
	api.HttpClient = urlfetch.Client(c)
	defer api.Close()

	for i := 0; i < len(bookList); i++ {
		book := &bookList[i]
		term := getTwitterKeyword(book.Title)
		c.Infof("Getting tweets for %s", term)

		v := url.Values{}
		v.Add("lang", "en")
		tweets, err := api.GetSearch(term, nil)

		if err != nil {
			c.Errorf("Twitter call failed for %s: %s", term, err)
			return
		}
		book.Tweets = tweets
		c.Infof("Got %d tweets for %s", len(book.Tweets), term)
	}

	sort.Sort(sort.Reverse(BooksByTweets(bookList)))
	PrintAsJson(c, bookList)
}
Esempio n. 2
0
func getTweets__WaitWithChannel(c RequestContext, bookList []Book) {
	anaconda.SetConsumerKey(twitterConsumerKey)
	anaconda.SetConsumerSecret(twitterConsumerSecret)
	api := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
	api.HttpClient = urlfetch.Client(c)
	defer api.Close()

	twitterc := make(chan error)

	for i := 0; i < len(bookList); i++ {
		go func(book *Book) {
			term := getTwitterKeyword(book.Title)
			c.Infof("Getting tweets for %s", term)

			v := url.Values{}
			v.Add("lang", "en")

			tweets, err := api.GetSearchNoQueue(term, v)

			if err != nil {
				c.Errorf("Twitter call failed for %s: %s", term, err)
			} else {
				book.Tweets = tweets
				c.Infof("Got %d tweets for %s", len(book.Tweets), term)
			}
			twitterc <- err

		}(&bookList[i])
	}

	for i := 0; i < len(bookList); i++ {
		<-twitterc
	}

	sort.Sort(sort.Reverse(BooksByTweets(bookList)))
	PrintAsJson(c, bookList)
}