本文整理匯總了Golang中github.com/kurrik/twittergo.Client類的典型用法代碼示例。如果您正苦於以下問題:Golang Client類的具體用法?Golang Client怎麽用?Golang Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
示例2: 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
}
示例3: main
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)
}
}
示例4: 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
}
示例5: sendTweet
func sendTweet(client *twittergo.Client, text string) {
var (
err error
req *http.Request
)
// Build Params
query := url.Values{}
query.Set("status", text)
// Bulid body
body := strings.NewReader(query.Encode())
// Prepare request
if req, err = http.NewRequest("POST", "/1.1/statuses/update.json", body); err != nil {
fmt.Printf("Could not parse request: %v\n", err)
os.Exit(1)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Send Request
if _, err = client.SendRequest(req); err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
}
示例6: sendTweet
func sendTweet(client *twittergo.Client, message string) {
data := url.Values{}
data.Set("status", message)
body := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", "/1.1/statuses/update.json", body)
if err != nil {
log.Printf("Could not parse request: %v", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.SendRequest(req)
if err != nil {
log.Printf("Could not send request: %v", err)
return
}
tweet := &twittergo.Tweet{}
err = resp.Parse(tweet)
if err != nil {
if rle, ok := err.(twittergo.RateLimitError); ok {
log.Printf("Rate limited, reset at %v\n", rle.Reset)
} else if errs, ok := err.(twittergo.Errors); ok {
for i, val := range errs.Errors() {
log.Printf("Error #%v - ", i+1)
log.Printf("Code: %v ", val.Code())
log.Printf("Msg: %v\n", val.Message())
}
} else {
log.Printf("Problem parsing response: %v\n", err)
}
}
}
示例7: Tweet
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: 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())
}
示例9: GetTweets
// Gets tweets from max_id backwards.
// max_id==0 means latest tweet.
func GetTweets(client *twittergo.Client, user string, max_id uint64, since_id uint64) (tweets []map[string]interface{}, err error) {
args := url.Values{
"screen_name": []string{user},
"trim_user": []string{"true"},
"include_rts": []string{"true"},
"include_entities": []string{"true"},
"count": []string{"200"},
}
if max_id != 0 {
args["max_id"] = []string{strconv.FormatUint(max_id, 10)}
}
if since_id != 0 {
args["since_id"] = []string{strconv.FormatUint(since_id, 10)}
}
query := args.Encode()
url := "https://api.twitter.com/1.1/statuses/user_timeline.json?" + query
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return
}
log.Printf("Fetching url %v\n", url)
r, err := client.SendRequest(req)
if err != nil {
return
}
ctype := r.Header.Get("Content-Type")
mtype, params, err := mime.ParseMediaType(ctype)
if mtype != "application/json" {
log.Printf("Response is not JSON: %q", ctype)
return
}
charset, ok := params["charset"]
if ok && charset != "utf-8" {
log.Printf("JSON has to be UTF-8: %q", ctype)
return
}
//TODO ponder rate-limiting http headers
//X-Ratelimit-Remaining: 143
//X-Ratelimit-Limit: 150
//X-Ratelimit-Reset: 1303182615
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
err = r.Body.Close()
if err != nil {
return
}
err = json.Unmarshal(buf, &tweets)
return
}
示例10: main
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: tweetMessage
func tweetMessage(message string, client *twittergo.Client) error {
data := url.Values{}
data.Set("status", message)
body := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", "/1.1/statuses/update.json", body)
if noError(err) {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = client.SendRequest(req)
}
return err
}
示例12: 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")
}
}
示例13: fetchNewTweets
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()
}
示例14: SendApiRequest
func SendApiRequest(client *twittergo.Client, reqUrl string, params map[string]string) (resp *twittergo.APIResponse, err error) {
var (
body io.Reader
data url.Values = url.Values{}
req *http.Request
)
for key, value := range params {
data.Set(key, value)
}
body = strings.NewReader(data.Encode())
if req, err = http.NewRequest("POST", reqUrl, body); err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err = client.SendRequest(req)
return
}
示例15: Connect
func Connect(client *twittergo.Client, path string, query url.Values) (resp *twittergo.APIResponse, err error) {
var (
req *http.Request
)
url := fmt.Sprintf("https://stream.twitter.com%v?%v", path, query.Encode())
req, err = http.NewRequest("GET", url, nil)
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
}
return
}