當前位置: 首頁>>代碼示例>>Golang>>正文


Golang twittergo.APIResponse類代碼示例

本文整理匯總了Golang中github.com/kurrik/twittergo.APIResponse的典型用法代碼示例。如果您正苦於以下問題:Golang APIResponse類的具體用法?Golang APIResponse怎麽用?Golang APIResponse使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了APIResponse類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: SendMediaRequest

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
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:31,代碼來源:main.go

示例2: main

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)
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:34,代碼來源:main.go

示例3: SendTweet

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
}
開發者ID:chdsbd,項目名稱:wruff.xyz,代碼行數:28,代碼來源:tweet.go

示例4: parseWithErrorHandling

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)
	}
}
開發者ID:joho,項目名稱:goadventure,代碼行數:7,代碼來源:real_twitter_wrapper.go

示例5: responseData

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
}
開發者ID:Melinysh,項目名稱:Project-Clay,代碼行數:34,代碼來源:TwitterComm.go

示例6: GetTimeline

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
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:32,代碼來源:app.go

示例7: main

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())
}
開發者ID:carriercomm,項目名稱:cyborgmanifesto,代碼行數:57,代碼來源:borgia.go

示例8: printResponseRateLimits

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")
	}
}
開發者ID:joho,項目名稱:goadventure,代碼行數:9,代碼來源:real_twitter_wrapper.go

示例9: twitter

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
}
開發者ID:0x263b,項目名稱:Porygon2,代碼行數:45,代碼來源:twitter.go

示例10: fetchAndPrintCursoredList

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
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:45,代碼來源:main.go

示例11: main

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", madTweet())
	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)
	}
}
開發者ID:dillonhafer,項目名稱:mad-wisdom,代碼行數:44,代碼來源:main.go

示例12: GetTweet

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
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:21,代碼來源:main.go

示例13: fetchAndPrintList

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
}
開發者ID:carriercomm,項目名稱:twittergo-examples,代碼行數:38,代碼來源:main.go

示例14: RespondToTweet

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)
	}
}
開發者ID:joho,項目名稱:goadventure,代碼行數:36,代碼來源:real_twitter_wrapper.go

示例15: main

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")
	}
}
開發者ID:kurrik,項目名稱:twittergo-examples,代碼行數:53,代碼來源:main.go


注:本文中的github.com/kurrik/twittergo.APIResponse類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。