Пример #1
0
func main() {
	var tw *twitter.Twitter

	var credentials = &social.Credentials{
		Key:    "sCCiDYz9iNtm2KVw4jHAA",
		Secret: "M8oCAm1KNd04RlPrkmGRGjoWvYUcIqY8my8eS3txcjQ",
	}

	tw, _ = twitter.New(credentials)

	var tag = social.Tag("Maserati")

	var options = social.DefaultOptions()
	options.Count = 1000
	options.MaxPages = 0
	// options.Paginate = false

	posts, pages, err := tw.GetByTag(tag, options, nil)
	if err != nil {
		log.Error(err)
	}
	log.Infof("Got %d posts from %d page/s", len(posts), pages)
}
Пример #2
0
// Get is the generic method used to perform a call to the REST APIs
func (tw Twitter) Get(query Query, options *social.Options) (posts social.Posts, pages uint, err error) {
	var req *http.Request
	var resp *twittergo.APIResponse

	log.Debugln("Performing a request")

	// Init options
	if options == nil {
		log.Debugln("No option passed, using default ones")
		options = social.DefaultOptions()
	}
	if options.Paginate == false {
		options.MaxPages = 1
	}
	log.WithFields(options.ToFields()).Debugf("Options")

	// Init query
	query.Set("count", strconv.FormatUint(uint64(options.Count), 10))
	log.WithFields(query.ToFields()).Debugf("Query")

	posts = make(social.Posts, 0)
	for pages < options.MaxPages || options.MaxPages == 0 {

		// Create the URL
		url := fmt.Sprintf("/1.1/search/tweets.json?%v", query.Encode())
		// Create the HTTP Request
		req, err = http.NewRequest("GET", url, nil)
		if err != nil {
			return
		}
		// Send the Auth request
		resp, err = tw.api.SendRequest(req)
		if err != nil {
			return
		}
		// Increment the total number of pages retrieved
		pages++

		// Parse results
		var results = &twittergo.SearchResults{}
		err = resp.Parse(results)
		if err != nil {
			return
		}

		// Convert data to posts
		for _, tweet := range results.Statuses() {
			post, _ := tw.ToPost(tweet)
			posts = append(posts, post)
		}

		log.Debugf("Got page %d with %d tweets", pages, len(results.Statuses()))

		// Handle pagination, chack if a new page is needed
		values, nextQueryError := results.NextQuery()
		if nextQueryError != nil {
			log.Debug(nextQueryError)
			break
		}
		query = Query{values}
		log.WithFields(query.ToFields()).Debugf("New page query")
	}

	log.Debugf("Query ended, retrieved %d posts", len(posts))
	return
}