Beispiel #1
0
func (twComm *twitterComm) responseData(url string) ([]byte, error) {
	fmt.Println("Getting response for", url)
	var req *http.Request
	var err error
	if req, err = http.NewRequest("GET", url, nil); err != nil {
		fmt.Println("Error occured on creating request url", url, err)
		return nil, err
	}

	var resp *twittergo.APIResponse
	if resp, err = twComm.client.SendRequest(req); err != nil {
		fmt.Println("Error occured on sending request", url, err)
		return nil, err
	}
	defer resp.Body.Close()

	var body = []byte{}                                    //need to parse own response for JSON
	if body, err = ioutil.ReadAll(resp.Body); err != nil { //read all the data as []byte
		fmt.Println("Error in ioutil Reading of response", url, err)
		return nil, err
	}

	if strings.Contains(string(body), "Rate limit exceeded") {
		fmt.Println("---------------------------------------------")
		fmt.Println("URL: ", url)
		fmt.Printf("Rate limit:           %v\n", resp.RateLimit())
		fmt.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
		fmt.Printf("Rate limit reset:     %v\n---------------------------------------------\n", resp.RateLimitReset())
		twComm.handleRateLimit()
		return twComm.responseData(url)
	}

	return body, nil
}
Beispiel #2
0
func Tweet(newTweet string) {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		tweet  *twittergo.Tweet
	)

	fmt.Println("Loading credentials ...")

	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		panic(err)
	}

	data := url.Values{}
	data.Set("status", newTweet)
	body := strings.NewReader(data.Encode())
	req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body)
	if err != nil {
		fmt.Printf("Could not parse request: %v\n", err)
		os.Exit(1)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err = client.SendRequest(req)
	if err != nil {
		fmt.Printf("Could not send request: %v\n", err)
		os.Exit(1)
	}
	tweet = &twittergo.Tweet{}
	err = resp.Parse(tweet)
	if err != nil {
		if rle, ok := err.(twittergo.RateLimitError); ok {
			fmt.Printf("Rate limited, reset at %v\n", rle.Reset)
		} else if errs, ok := err.(twittergo.Errors); ok {
			for i, val := range errs.Errors() {
				fmt.Printf("Error #%v - ", i+1)
				fmt.Printf("Code: %v ", val.Code())
				fmt.Printf("Msg: %v\n", val.Message())
			}
		} else {
			fmt.Printf("Problem parsing response: %v\n", err)
		}
		os.Exit(1)
	}
	fmt.Printf("ID:                   %v\n", tweet.Id())
	fmt.Printf("Tweet:                %v\n", tweet.Text())
	fmt.Printf("User:                 %v\n", tweet.User().Name())
	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")
	}
}
func printResponseRateLimits(resp *twittergo.APIResponse) {
	if resp.HasRateLimit() {
		log.Printf("Rate limit:           %v\n", resp.RateLimit())
		log.Printf("Rate limit remaining: %v\n", resp.RateLimitRemaining())
		log.Printf("Rate limit reset:     %v\n", resp.RateLimitReset())
	} else {
		log.Printf("Could not parse rate limit from response.\n")
	}
}
Beispiel #4
0
func main() {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		tweet  *twittergo.Tweet
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}

	body, header, err := GetBody()
	if err != nil {
		fmt.Printf("Problem loading body: %v\n", err)
		os.Exit(1)
	}

	endpoint := "/1.1/statuses/update_with_media.json"
	req, err = http.NewRequest("POST", endpoint, body)
	if err != nil {
		fmt.Printf("Could not parse request: %v\n", err)
		os.Exit(1)
	}
	req.Header.Set("Content-Type", header)

	resp, err = client.SendRequest(req)
	if err != nil {
		fmt.Printf("Could not send request: %v\n", err)
		os.Exit(1)
	}
	tweet = &twittergo.Tweet{}
	err = resp.Parse(tweet)
	if err != nil {
		fmt.Printf("Problem parsing response: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("ID:                         %v\n", tweet.Id())
	fmt.Printf("Tweet:                      %v\n", tweet.Text())
	fmt.Printf("User:                       %v\n", tweet.User().Name())
	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")
	}
	if resp.HasMediaRateLimit() {
		fmt.Printf("Media Rate limit:           %v\n", resp.MediaRateLimit())
		fmt.Printf("Media Rate limit remaining: %v\n", resp.MediaRateLimitRemaining())
		fmt.Printf("Media Rate limit reset:     %v\n", resp.MediaRateLimitReset())
	} else {
		fmt.Printf("Could not parse media rate limit from response.\n")
	}
}
Beispiel #5
0
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")
	}
}
Beispiel #6
0
func main() {
	var (
		err         error
		client      *twittergo.Client
		req         *http.Request
		resp        *twittergo.APIResponse
		customTweet *CustomTweet
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}

	query := url.Values{}
	query.Set("id", "641727937201311744") // https://twitter.com/kurrik/status/641727937201311744
	url := fmt.Sprintf("%v?%v", "/1.1/statuses/show.json", 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)
	}
	customTweet = &CustomTweet{}
	err = resp.Parse(customTweet)
	if err != nil {
		if rle, ok := err.(twittergo.RateLimitError); ok {
			fmt.Printf("Rate limited, reset at %v\n", rle.Reset)
		} else if errs, ok := err.(twittergo.Errors); ok {
			for i, val := range errs.Errors() {
				fmt.Printf("Error #%v - ", i+1)
				fmt.Printf("Code: %v ", val.Code())
				fmt.Printf("Msg: %v\n", val.Message())
			}
		} else {
			fmt.Printf("Problem parsing response: %v\n", err)
		}
		os.Exit(1)
	}
	fmt.Printf("ID:                   %v\n", customTweet.CustomID)
	fmt.Printf("Tweet:                %v\n", customTweet.CustomText)
	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")
	}
}
Beispiel #7
0
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()
}
Beispiel #8
0
func main() {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		tweet  *twittergo.Tweet
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	data := url.Values{}
	data.Set("status", fmt.Sprintf("Fakekurrik-Fakekurrik %v", time.Now()))
	data.Set("place_id", "15cbc94209abe896")
	body := strings.NewReader(data.Encode())
	req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body)
	if err != nil {
		fmt.Printf("Could not parse request: %v\n", err)
		os.Exit(1)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err = client.SendRequest(req)
	if err != nil {
		fmt.Printf("Could not send request: %v\n", err)
		os.Exit(1)
	}
	tweet = &twittergo.Tweet{}
	err = resp.Parse(tweet)
	if err != nil {
		fmt.Printf("Problem parsing response: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("ID:                   %v\n", tweet.Id())
	fmt.Printf("Tweet:                %v\n", tweet.Text())
	fmt.Printf("User:                 %v\n", tweet.User().Name())
	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")
	}
}
Beispiel #9
0
func main() {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		user   *twittergo.User
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	req, err = http.NewRequest("GET", "/1.1/account/verify_credentials.json", 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)
	}
	user = &twittergo.User{}
	err = resp.Parse(user)
	if err != nil {
		fmt.Printf("Problem parsing response: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("ID:                   %v\n", user.Id())
	fmt.Printf("Name:                 %v\n", user.Name())
	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")
	}
}
Beispiel #10
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")
		}
	}
}
Beispiel #11
0
func main() {
	var (
		err        error
		client     *twittergo.Client
		apiResp    *twittergo.APIResponse
		mediaResp  twittergo.MediaResponse
		mediaId    string
		mediaBytes []byte
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	if mediaBytes, err = ioutil.ReadFile("video_upload/twitter_media_upload.mp4"); err != nil {
		fmt.Printf("Error reading media: %v\n", err)
		os.Exit(1)
	}
	if mediaResp, err = SendMediaRequest(
		client,
		"https://upload.twitter.com/1.1/media/upload.json",
		map[string]string{
			"command":     "INIT",
			"media_type":  "video/mp4",
			"total_bytes": fmt.Sprintf("%d", len(mediaBytes)),
		},
		nil,
	); err != nil {
		fmt.Printf("Problem sending INIT request: %v\n", err)
		os.Exit(1)
	}
	mediaId = fmt.Sprintf("%v", mediaResp.MediaId())
	if mediaResp, err = SendMediaRequest(
		client,
		"https://upload.twitter.com/1.1/media/upload.json",
		map[string]string{
			"command":       "APPEND",
			"media_id":      mediaId,
			"segment_index": "0",
		},
		mediaBytes,
	); err != nil {
		fmt.Printf("Problem sending APPEND request: %v\n", err)
		os.Exit(1)
	}
	if mediaResp, err = SendMediaRequest(
		client,
		"https://upload.twitter.com/1.1/media/upload.json",
		map[string]string{
			"command":  "FINALIZE",
			"media_id": mediaId,
		},
		nil,
	); err != nil {
		fmt.Printf("Problem sending FINALIZE request: %v\n", err)
		os.Exit(1)
	}
	if apiResp, err = SendApiRequest(
		client,
		"/1.1/statuses/update.json",
		map[string]string{
			"status":    fmt.Sprintf("Media! %v", time.Now()),
			"media_ids": mediaId,
		},
	); err != nil {
		fmt.Printf("Problem sending Tweet request: %v\n", err)
		os.Exit(1)
	}
	var (
		tweet *twittergo.Tweet = &twittergo.Tweet{}
	)
	if err = apiResp.Parse(tweet); err != nil {
		fmt.Printf("Problem parsing Tweet response: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("ID:                         %v\n", tweet.Id())
	fmt.Printf("Tweet:                      %v\n", tweet.Text())
	fmt.Printf("User:                       %v\n", tweet.User().Name())
	fmt.Printf("Media Id:                   %v\n", mediaResp.MediaId())
	if apiResp.HasRateLimit() {
		fmt.Printf("Rate limit:                 %v\n", apiResp.RateLimit())
		fmt.Printf("Rate limit remaining:       %v\n", apiResp.RateLimitRemaining())
		fmt.Printf("Rate limit reset:           %v\n", apiResp.RateLimitReset())
	} else {
		fmt.Printf("Could not parse rate limit from response.\n")
	}
	if apiResp.HasMediaRateLimit() {
		fmt.Printf("Media Rate limit:           %v\n", apiResp.MediaRateLimit())
		fmt.Printf("Media Rate limit remaining: %v\n", apiResp.MediaRateLimitRemaining())
		fmt.Printf("Media Rate limit reset:     %v\n", apiResp.MediaRateLimitReset())
	} else {
		fmt.Printf("Could not parse media rate limit from response.\n")
	}
}