示例#1
0
func GetTimeline(client *twittergo.Client) (t *twittergo.Timeline, err error) {
	var (
		req   *http.Request
		resp  *twittergo.APIResponse
		rle   twittergo.RateLimitError
		ok    bool
		query url.Values
		endpt string
	)
	query = url.Values{}
	query.Set("count", fmt.Sprintf("%v", COUNT))
	if client.User == nil {
		// With a user token, the user_timeline.json method
		// returns the current user.  Without, you need to specify
		// an explicit ID.
		query.Set("screen_name", SCREEN_NAME)
	}
	endpt = fmt.Sprintf("/1.1/statuses/user_timeline.json?%v", query.Encode())
	if req, err = http.NewRequest("GET", endpt, nil); err != nil {
		return
	}
	if resp, err = client.SendRequest(req); err != nil {
		return
	}
	t = &twittergo.Timeline{}
	if err = resp.Parse(t); err != nil {
		if rle, ok = err.(twittergo.RateLimitError); ok {
			err = fmt.Errorf("Rate limited. Reset at %v", rle.Reset)
		}
	}
	return
}
示例#2
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 CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	query := url.Values{}
	url := fmt.Sprintf("/1.1/statuses/home_timeline.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("There was an error: %v\n", err)
		os.Exit(1)
	}
}
示例#3
0
文件: tweet.go 项目: chdsbd/wruff.xyz
func SendTweet(username, message string) error {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		tweet  *twittergo.Tweet
	)
	client = loadCredentials()
	data := url.Values{}
	data.Set("status", fmt.Sprintf("@%s %s", username, message)) //TODO: INSERT STUFF HERE!!!
	body := strings.NewReader(data.Encode())
	req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err = client.SendRequest(req)
	if err != nil {
		return err
	}
	tweet = &twittergo.Tweet{}
	err = resp.Parse(tweet)
	if err != nil {
		return err
	}
	return nil
}
示例#4
0
func parseWithErrorHandling(resp *twittergo.APIResponse, out interface{}) {
	err := resp.Parse(out)
	// TODO recover from 2013/04/28 14:16:32 Problem parsing response: Error 187: Status is a duplicate.
	if err != nil {
		log.Fatalf("Problem parsing response: %v\n", err)
	}
}
示例#5
0
func SendMediaRequest(client *twittergo.Client, reqUrl string, params map[string]string, media []byte) (mediaResp twittergo.MediaResponse, err error) {
	var (
		req         *http.Request
		resp        *twittergo.APIResponse
		body        io.ReadWriter = bytes.NewBufferString("")
		mp          *multipart.Writer
		writer      io.Writer
		contentType string
	)
	mp = multipart.NewWriter(body)
	for key, value := range params {
		mp.WriteField(key, value)
	}
	if media != nil {
		if writer, err = mp.CreateFormField("media"); err != nil {
			return
		}
		writer.Write(media)
	}
	contentType = fmt.Sprintf("multipart/form-data;boundary=%v", mp.Boundary())
	mp.Close()
	if req, err = http.NewRequest("POST", reqUrl, body); err != nil {
		return
	}
	req.Header.Set("Content-Type", contentType)
	if resp, err = client.SendRequest(req); err != nil {
		return
	}
	err = resp.Parse(&mediaResp)
	return
}
示例#6
0
func main() {
	var (
		err     error
		client  *twittergo.Client
		req     *http.Request
		resp    *twittergo.APIResponse
		results *map[string]interface{}
	)
	client, err = LoadCredentials()
	if err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	url := fmt.Sprintf("/1.1/application/rate_limit_status.json")
	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)
	}
	fmt.Printf("Response: %v\n", resp)
	//fmt.Printf("%v\n", resp.ReadBody())

	results = &map[string]interface{}{}
	if err = resp.Parse(results); err != nil {
		fmt.Printf("Could not parse results: %v\n", err)
		os.Exit(1)
	}
	PrintMap(*results)
}
示例#7
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")
	}
}
示例#8
0
func main() {
	// Register command-line flags.
	numWords := flag.Int("words", 17, "maximum number of words to print")
	prefixLen := flag.Int("prefix", 2, "prefix length in words")

	flag.Parse()                     // Parse command-line flags.
	rand.Seed(time.Now().UnixNano()) // Seed the random number generator.

	c := NewChain(*prefixLen)     // Initialize a new Chain.
	c.Build(os.Stdin)             // Build chains from standard input.
	text := c.Generate(*numWords) // Generate text.

	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", text)
	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())
}
示例#9
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")
	}
}
示例#10
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")
	}
}
示例#11
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")
	}
}
示例#12
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()
}
示例#13
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")
	}
}
示例#14
0
func twitter(command *bot.Cmd, matches []string) (msg string, err error) {
	var (
		client  *twittergo.Client
		query   url.Values
		req     *http.Request
		resp    *twittergo.APIResponse
		results *twittergo.Timeline
		output  string
	)

	if client, err = LoadCredentials(); err != nil {
		return "Twitter | Could not get tweet", nil
	}

	query = url.Values{}
	query.Set("count", "1")
	query.Set("exclude_replies", "true")
	query.Set("screen_name", matches[1])

	endpoint := fmt.Sprintf("/1.1/statuses/user_timeline.json?%v", query.Encode())

	if req, err = http.NewRequest("GET", endpoint, nil); err != nil {
		return "Twitter | Could not get tweet", nil
	}
	if resp, err = client.SendRequest(req); err != nil {
		return "Twitter | Could not get tweet", nil
	}
	results = &twittergo.Timeline{}
	resp.Parse(results)
	for _, tweet := range *results {
		if _, err = json.Marshal(tweet); err != nil {
			return "Twitter | Could not get tweet", nil
		}

		reg := regexp.MustCompile("\\s+")
		text := reg.ReplaceAllString(tweet.Text(), " ") // Strip tabs and newlines
		output = fmt.Sprintf("Twitter | %s (@%s) | %s | %s",
			tweet.User().Name(),
			tweet.User().ScreenName(),
			text,
			Time(tweet.CreatedAt()))
	}

	return output, nil
}
示例#15
0
func fetchAndPrintCursoredList(client *twittergo.Client, path string, query url.Values) (err error) {
	var (
		req     *http.Request
		resp    *twittergo.APIResponse
		results twittergo.CursoredLists
		i       int64
	)
	i = 1
	query.Set("cursor", "-1")
	for {
		url := fmt.Sprintf("%v?%v", path, query.Encode())
		req, err = http.NewRequest("GET", url, nil)
		req.Header.Set("Accept-Encoding", "gzip, deflate")
		if err != nil {
			err = fmt.Errorf("Could not parse request: %v\n", err)
			break
		}
		resp, err = client.SendRequest(req)
		if err != nil {
			err = fmt.Errorf("Could not send request: %v\n", err)
			break
		}
		results = twittergo.CursoredLists{}
		if err = resp.Parse(&results); err != nil {
			if err = handleRateLimit(err); err != nil {
				err = fmt.Errorf("Problem parsing response: %v\n", err)
				break
			} else {
				continue
			}
		}
		fmt.Printf("\n")
		for _, list := range results.Lists() {
			fmt.Printf("%v.) ", i)
			printList(&list)
			i += 1
		}
		printRateLimit(resp)
		if results.NextCursorStr() == "0" {
			break
		}
		query.Set("cursor", results.NextCursorStr())
	}
	return
}
示例#16
0
func postTweet(status string) error {
	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", status)
	body := strings.NewReader(data.Encode())
	req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err = client.SendRequest(req)
	if err != nil {
		return err
	}
	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)
		}
	}
	return err
}
示例#17
0
func GetTweet(client *twittergo.Client, id string) (tweet *twittergo.Tweet, err error) {
	var (
		query = url.Values{"id": []string{id}}
		url   = fmt.Sprintf("/1.1/statuses/show.json?%v", query.Encode())
		req   *http.Request
		resp  *twittergo.APIResponse
	)
	if req, err = http.NewRequest("GET", url, nil); err != nil {
		err = fmt.Errorf("Could not parse request: %v\n", err)
		return
	}
	if resp, err = client.SendRequest(req); err != nil {
		err = fmt.Errorf("Could not send request: %v\n", err)
		return
	}
	tweet = &twittergo.Tweet{}
	if err = resp.Parse(tweet); err != nil {
		err = fmt.Errorf("There was an error: %v\n", err)
	}
	return
}
示例#18
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")
	}
}
示例#19
0
func fetchAndPrintList(client *twittergo.Client, path string, query url.Values) (err error) {
	var (
		req     *http.Request
		resp    *twittergo.APIResponse
		results twittergo.Lists
	)
	for {
		url := fmt.Sprintf("%v?%v", path, query.Encode())
		req, err = http.NewRequest("GET", url, nil)
		req.Header.Set("Accept-Encoding", "gzip, deflate")
		if err != nil {
			err = fmt.Errorf("Could not parse request: %v\n", err)
			return
		}
		resp, err = client.SendRequest(req)
		if err != nil {
			err = fmt.Errorf("Could not send request: %v\n", err)
			return
		}
		results = twittergo.Lists{}
		if err = resp.Parse(&results); err != nil {
			if err = handleRateLimit(err); err != nil {
				err = fmt.Errorf("Problem parsing response: %v\n", err)
				return
			} else {
				continue
			}
		}
		fmt.Printf("\n")
		for i, list := range results {
			fmt.Printf("%v.) ", i+1)
			printList(&list)
		}
		printRateLimit(resp)
		return
	}
	return
}
示例#20
0
func (tw *RealTwitterWrapper) RespondToTweet(tweet *twittergo.Tweet, message string) {
	var (
		err  error
		user twittergo.User
		req  *http.Request
		resp *twittergo.APIResponse
	)

	user = tweet.User()
	data := url.Values{}

	// set status
	status := fmt.Sprintf("@%v %v", user.ScreenName(), message)
	data.Set("status", status)
	// set in_reply_to_status_id
	status_id := fmt.Sprintf("%d", tweet.Id())
	data.Set("in_reply_to_status_id", status_id)

	log.Printf("Set status '%v' to '%v' in reply to %v", status, user.ScreenName(), status_id)

	body := strings.NewReader(data.Encode())
	req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body)
	if err != nil {
		log.Fatalf("Could not parse request: %v\n", err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err = tw.client.SendRequest(req)
	if err != nil {
		log.Fatalf("Could not send request: %v\n", err)
	}
	tweet = &twittergo.Tweet{}
	err = resp.Parse(tweet)
	if err != nil {
		log.Fatalf("Problem parsing response: %v\n", err)
	}
}
示例#21
0
func main() {
	var (
		err     error
		client  *twittergo.Client
		req     *http.Request
		resp    *twittergo.APIResponse
		args    *Args
		max_id  uint64
		out     *os.File
		query   url.Values
		results *twittergo.Timeline
		text    []byte
	)
	args = parseArgs()
	if client, err = LoadCredentials(); err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	if out, err = os.Create(args.OutputFile); err != nil {
		fmt.Printf("Could not create output file: %v\n", args.OutputFile)
		os.Exit(1)
	}
	defer out.Close()
	const (
		count   int = 200
		urltmpl     = "/1.1/favorites/list.json?%v"
		minwait     = time.Duration(10) * time.Second
	)
	query = url.Values{}
	query.Set("count", fmt.Sprintf("%v", count))
	query.Set("screen_name", args.ScreenName)
	total := 0
	for {
		if max_id != 0 {
			query.Set("max_id", fmt.Sprintf("%v", max_id))
		}
		endpoint := fmt.Sprintf(urltmpl, query.Encode())
		if req, err = http.NewRequest("GET", endpoint, nil); err != nil {
			fmt.Printf("Could not parse request: %v\n", err)
			os.Exit(1)
		}
		if resp, err = client.SendRequest(req); err != nil {
			fmt.Printf("Could not send request: %v\n", err)
			os.Exit(1)
		}
		results = &twittergo.Timeline{}
		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)
			}
		}
		batch := len(*results)
		if batch == 0 {
			fmt.Printf("No more results, end of timeline.\n")
			break
		}
		for _, tweet := range *results {
			if text, err = json.Marshal(tweet); err != nil {
				fmt.Printf("Could not encode Tweet: %v\n", err)
				os.Exit(1)
			}
			out.Write(text)
			out.Write([]byte("\n"))
			max_id = tweet.Id() - 1
			total += 1
		}
		fmt.Printf("Got %v Tweets", batch)
		if resp.HasRateLimit() {
			fmt.Printf(", %v calls available", resp.RateLimitRemaining())
		}
		fmt.Printf(".\n")
	}
	fmt.Printf("--------------------------------------------------------\n")
	fmt.Printf("Wrote %v Tweets to %v\n", total, args.OutputFile)
}
示例#22
0
func getTweets() {
	var (
		err    error
		client *twittergo.Client
		req    *http.Request
		resp   *twittergo.APIResponse
		max_id uint64
		out    *os.File
		query  url.Values

		text []byte
	)
	if client, err = LoadCredentials(); err != nil {
		ERROR.Println("Could not parse CREDENTIALS file:", err)
		os.Exit(1)
	}
	if out, err = os.Create(TweetCacheFile); err != nil {
		ERROR.Println("Could not create output file:", TweetCacheFile)
		os.Exit(1)
	}
	defer out.Close()
	const (
		count   int = 100
		urltmpl     = "/1.1/statuses/user_timeline.json?%v"
		minwait     = time.Duration(10) * time.Second
	)
	query = url.Values{}
	query.Set("count", fmt.Sprintf("%v", count))
	query.Set("screen_name", ScreenName)
	total := 0

	if max_id != 0 {
		query.Set("max_id", fmt.Sprintf("%v", max_id))
	}
	endpoint := fmt.Sprintf(urltmpl, query.Encode())
	if req, err = http.NewRequest("GET", endpoint, nil); err != nil {
		ERROR.Println("Could not parse request:", err)
	}
	if resp, err = client.SendRequest(req); err != nil {
		ERROR.Println("Could not send request:", err)
	}

	results = &twittergo.Timeline{}
	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
			}
			WARNING.Println("Rate limited. Reset at", rle.Reset, "Waiting for", dur)
			time.Sleep(dur)

		} else {
			ERROR.Println("Problem parsing response:", err)
		}
	}
	batch := len(*results)
	if batch == 0 {
		INFO.Println("No more results, end of timeline.")
	} else {
		cache = results
	}
	for _, tweet := range *results {
		if text, err = json.Marshal(tweet); err != nil {
			ERROR.Println("Could not encode Tweet:", err)
			os.Exit(1)
		}
		out.Write(text)
		out.Write([]byte("\n"))
		max_id = tweet.Id() - 1
		total += 1
	}
	INFO.Println("Got", batch, "Tweets")
	if resp.HasRateLimit() {
		INFO.Println(resp.RateLimitRemaining(), " calls available")
	}

	INFO.Println("--------------------------------------------------------")
	INFO.Println("Wrote", total, "Tweets to", TweetCacheFile)
}
示例#23
0
func main() {
	var (
		err      error
		client   *twittergo.Client
		req      *http.Request
		resp     *twittergo.APIResponse
		args     *Args
		ids      string
		out      *os.File
		in       *os.File
		query    url.Values
		results  TweetMapMap
		text     []byte
		scanner  *bufio.Scanner
		endpoint string
	)
	args = parseArgs()
	if client, err = LoadCredentials(); err != nil {
		fmt.Printf("Could not parse CREDENTIALS file: %v\n", err)
		os.Exit(1)
	}
	if in, err = os.Open(args.InputFile); err != nil {
		fmt.Printf("Could not read input file %v: %v\n", args.InputFile, err)
		os.Exit(1)
	}
	defer in.Close()
	scanner = bufio.NewScanner(in)
	if out, err = os.Create(args.OutputFile); err != nil {
		fmt.Printf("Could not create output file %v: %v\n", args.OutputFile, err)
		os.Exit(1)
	}
	defer out.Close()
	const (
		count   int = 100
		urltmpl     = "/1.1/statuses/lookup.json?%v"
		minwait     = time.Duration(10) * time.Second
	)
	query = url.Values{}
	query.Set("map", "true")
	query.Set("trim_user", "true")
	total := 0
	for {
		if ids, err = getIds(scanner, count); err != nil {
			fmt.Printf("Problem reading IDs: %v\n", err)
			os.Exit(1)
		}
		query.Set("id", ids)
		endpoint = fmt.Sprintf(urltmpl, query.Encode())
		if req, err = http.NewRequest("GET", endpoint, nil); err != nil {
			fmt.Printf("Could not parse request: %v\n", err)
			os.Exit(1)
		}
		if resp, err = client.SendRequest(req); err != nil {
			fmt.Printf("Could not send request: %v\n", err)
			os.Exit(1)
		}
		results = TweetMapMap{}
		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)
			}
		}
		batch := len(results.Id)
		if batch == 0 {
			fmt.Printf("No more results, end of list.\n")
			break
		}
		for id, tweet := range results.Id {
			if text, err = json.Marshal(tweet); err != nil {
				fmt.Printf("Could not encode Tweet: %v\n", err)
				os.Exit(1)
			}
			out.Write([]byte(fmt.Sprintf("%v\t", id)))
			out.Write(text)
			out.Write([]byte("\n"))
			total += 1
		}
		fmt.Printf("Got %v Tweets, %v total", batch, total)
		if resp.HasRateLimit() {
			fmt.Printf(", %v calls available", resp.RateLimitRemaining())
		}
		fmt.Printf(".\n")
	}
	fmt.Printf("--------------------------------------------------------\n")
	fmt.Printf("Wrote %v Tweets to %v\n", total, args.OutputFile)
}
示例#24
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
}
示例#25
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")
		}
	}
}
示例#26
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")
	}
}