/
twitter.go
77 lines (69 loc) · 1.87 KB
/
twitter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/garyburd/go-oauth/oauth"
"io/ioutil"
"net/http"
"net/url"
"os"
)
type Tweet struct {
Text string `json:"text"`
}
type Creds struct {
ConsumerToken string `json:"consumer_token"`
ConsumerSecret string `json:"consumer_secret"`
AccessToken string `json:"access_token"`
AccessSecret string `json:"secret_token"`
}
func getCreds() Creds {
// Loads the creds out of creds.json
file, _ := os.Open("creds.json")
contents, _ := ioutil.ReadAll(file)
var creds Creds
json.Unmarshal(contents, &creds)
return creds
}
func getTweets(username string, count string, consumer oauth.Credentials, access oauth.Credentials) (tweets []Tweet, err error) {
// Pull a X number of tweets associated with a user.
// Docs: https://dev.twitter.com/rest/reference/get/statuses/user_timeline
client := oauth.Client{Credentials: consumer}
params := url.Values{"screen_name": {username}, "count": {count}}
resp, err := client.Get(
http.DefaultClient,
&access,
"https://api.twitter.com/1.1/statuses/user_timeline.json",
params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &tweets)
if err != nil {
return nil, err
}
return tweets, nil
}
func main() {
// Parse the user out of the command line flag -user.
username := flag.String(
"user", "cameronmaske", "The user to retrieve the latest tweet from.")
flag.Parse()
// Set up an OAuth client with the various tokens.
creds := getCreds()
consumer := oauth.Credentials{
Token: creds.ConsumerToken,
Secret: creds.ConsumerSecret}
access := oauth.Credentials{
Token: creds.AccessToken,
Secret: creds.AccessSecret}
// Get the lastest tweets for the username.
tweets, _ := getTweets(*username, "1", consumer, access)
fmt.Println(tweets[0])
}