コード例 #1
0
ファイル: poller.go プロジェクト: zchee/scuttlebutt
// Poll returns new messages since a given message ID.
func (p *Poller) Poll(sinceID uint64) ([]*scuttlebutt.Message, error) {
	// Send request.
	resp, err := p.Client.SendRequest(NewSearchRequest(sinceID))
	if err != nil {
		return nil, fmt.Errorf("send request: %s", err)
	}
	defer resp.Body.Close()

	// Convert to search results.
	var res twittergo.SearchResults
	if err := resp.Parse(&res); err != nil {
		return nil, fmt.Errorf("twitter search results error: %s", err)
	}

	// Convert search results to messages.
	var messages []*scuttlebutt.Message
	for _, tweet := range res.Statuses() {
		m := encodeTweet(tweet)
		if m.RepositoryID == "" {
			continue
		}
		messages = append(messages, m)
	}

	return messages, nil
}
コード例 #2
0
ファイル: find.go プロジェクト: blueskywalker/GoLang
func main() {
	var (
		err     error
		client  *twittergo.Client
		req     *http.Request
		resp    *twittergo.APIResponse
		results *twittergo.SearchResults
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse account.yaml file: %v\n", err)
		os.Exit(1)
	}
	if len(os.Args) < 2 {
		fmt.Printf("need args\n")
		os.Exit(1)
	}

	query := url.Values{}

	query.Set("q", os.Args[1])

	url := fmt.Sprintf("/1.1/search/tweets.json?%v", query.Encode())
	req, err = http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("Could not parse request: %v\n", err)
		os.Exit(1)
	}
	resp, err = client.SendRequest(req)
	if err != nil {
		fmt.Printf("Could not send request: %v\n", err)
		os.Exit(1)
	}
	results = &twittergo.SearchResults{}
	err = resp.Parse(results)
	if err != nil {
		fmt.Printf("Problem parsing response: %v\n", err)
		os.Exit(1)
	}

	for i, tweet := range results.Statuses() {
		user := tweet.User()
		fmt.Printf("%v.) %v\n", i+1, tweet.Text())
		fmt.Printf("From %v (@%v) ", user.Name(), user.ScreenName())
		fmt.Printf("at %v\n\n", tweet.CreatedAt().Format(time.RFC1123))
	}
	if resp.HasRateLimit() {
		fmt.Printf("Rate limit:           %v\n", resp.RateLimit())
		fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
		fmt.Printf("Rate limit reset:     %v\n", resp.RateLimitReset())
	} else {
		fmt.Printf("Could not parse rate limit from response.\n")
	}
}
コード例 #3
0
ファイル: TwitterHAL.go プロジェクト: etu/TwitterHAL
func fetchNewTweets(client *twittergo.Client, lastTweet uint64) []twittergo.Tweet {
	var (
		err     error
		req     *http.Request
		resp    *twittergo.APIResponse
		results *twittergo.SearchResults
	)

	// Build search
	query := url.Values{}
	query.Set("q", "#svpol -rt")
	query.Set("lang", "sv")
	query.Set("result_type", "recent")
	query.Set("count", "20")
	query.Set("since_id", fmt.Sprintf("%d", lastTweet))

	// Build URI
	url := fmt.Sprintf("/1.1/search/tweets.json?%v", query.Encode())

	// Prepare request
	if req, err = http.NewRequest("GET", url, nil); err != nil {
		fmt.Printf("Could not parse request: %v\n", err)
		os.Exit(1)
	}

	// Sign and send request
	if resp, err = client.SendRequest(req); err != nil {
		fmt.Printf("Could not send request: %v\n", err)
		os.Exit(1)
	}

	// Parse requests
	results = &twittergo.SearchResults{}
	if err = resp.Parse(results); err != nil {
		fmt.Printf("Problem parsing response: %v\n", err)
		os.Exit(1)
	}

	// Print ratelimit data
	if resp.HasRateLimit() {
		fmt.Printf("Rate limit:           %v\n", resp.RateLimit())
		fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
		fmt.Printf("Rate limit reset:     %v\n", resp.RateLimitReset())
	}

	return results.Statuses()
}
コード例 #4
0
func main() {
	var (
		err     error
		client  *twittergo.Client
		req     *http.Request
		resp    *twittergo.APIResponse
		results *twittergo.SearchResults
		args    *Args
		i       int
	)
	args = parseArgs()
	if client, err = LoadCredentials(); err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	query := url.Values{}
	query.Set("q", args.Query)
	if args.ResultType != "" {
		query.Set("result_type", args.ResultType)
	}
	i = 1
	for {
		url := fmt.Sprintf("/1.1/search/tweets.json?%v", query.Encode())
		req, err = http.NewRequest("GET", url, nil)
		if err != nil {
			fmt.Printf("Could not parse request: %v\n", err)
			break
		}
		resp, err = client.SendRequest(req)
		if err != nil {
			fmt.Printf("Could not send request: %v\n", err)
			break
		}
		results = &twittergo.SearchResults{}
		if err = resp.Parse(results); err != nil {
			if rle, ok := err.(twittergo.RateLimitError); ok {
				dur := rle.Reset.Sub(time.Now()) + time.Second
				if dur < MINWAIT {
					// Don't wait less than minwait.
					dur = MINWAIT
				}
				msg := "Rate limited. Reset at %v. Waiting for %v\n"
				fmt.Printf(msg, rle.Reset, dur)
				time.Sleep(dur)
				continue // Retry request.
			} else {
				fmt.Printf("Problem parsing response: %v\n", err)
				break
			}
		}
		fmt.Printf("\n")
		for _, tweet := range results.Statuses() {
			user := tweet.User()
			fmt.Printf("%v.) %v\n", i, tweet.Text())
			fmt.Printf("From %v (@%v) ", user.Name(), user.ScreenName())
			fmt.Printf("at %v\n\n", tweet.CreatedAt().Format(time.RFC1123))
			i += 1
		}
		if query, err = results.NextQuery(); err != nil {
			fmt.Printf("No next query: %v\n", err)
			break
		}
		if resp.HasRateLimit() {
			fmt.Printf("Rate limit:           %v\n", resp.RateLimit())
			fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
			fmt.Printf("Rate limit reset:     %v\n", resp.RateLimitReset())
		} else {
			fmt.Printf("Could not parse rate limit from response.\n")
		}
	}
}