mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package twitter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
/* NOTE: Currently single application ONLY
|
|
* bearer tokens are only supported for applications, not single-users
|
|
*/
|
|
|
|
// Client represents the data required to connect to the Twitter API
|
|
type Client struct {
|
|
apiBase string
|
|
bearerToken string
|
|
count int
|
|
screenName string
|
|
}
|
|
|
|
// NewClient creates and returns a new Twitter client
|
|
func NewClient(url string) *Client {
|
|
client := Client{
|
|
apiBase: url,
|
|
screenName: "wtfutil",
|
|
count: 5,
|
|
bearerToken: os.Getenv("WTF_TWITTER_BEARER_TOKEN"),
|
|
}
|
|
|
|
return &client
|
|
}
|
|
|
|
/* -------------------- Public Functions -------------------- */
|
|
|
|
// Tweets returns a list of tweets of a user
|
|
func (client *Client) Tweets() []Tweet {
|
|
tweets, err := client.tweets()
|
|
if err != nil {
|
|
return []Tweet{}
|
|
}
|
|
|
|
return tweets
|
|
}
|
|
|
|
/* -------------------- Private Functions -------------------- */
|
|
|
|
// tweets is the private interface for retrieving the list of user tweets
|
|
func (client *Client) tweets() (tweets []Tweet, err error) {
|
|
apiURL := fmt.Sprintf(
|
|
"%s/statuses/user_timeline.json?screen_name=%s&count=%s",
|
|
client.apiBase,
|
|
client.screenName,
|
|
strconv.Itoa(client.count),
|
|
)
|
|
|
|
data, err := Request(client.bearerToken, apiURL)
|
|
if err != nil {
|
|
return tweets, err
|
|
}
|
|
err = json.Unmarshal(data, &tweets)
|
|
|
|
return
|
|
}
|