-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
112 lines (97 loc) · 3.29 KB
/
client.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package twitter
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
//Basic OAuth related URLs
OAUTH_REQUES_TOKEN string = "https://api.twitter.com/oauth/request_token"
OAUTH_AUTH_TOKEN string = "https://api.twitter.com/oauth/authorize"
OAUTH_ACCESS_TOKEN string = "https://api.twitter.com/oauth/access_token"
//List API URLs
API_BASE string = "https://api.twitter.com/1.1/"
API_TIMELINE string = API_BASE + "statuses/home_timeline.json"
API_MENTIONS_TIMELINE string = API_BASE + "statuses/mentions_timeline.json"
API_USER_TIMELINE string = API_BASE + "statuses/user_timeline.json"
API_FOLLOWERS_IDS string = API_BASE + "followers/ids.json"
API_FOLLOWERS_LIST string = API_BASE + "followers/list.json"
API_FOLLOWER_INFO string = API_BASE + "users/show.json"
)
type Client struct {
HttpConn *http.Client
}
func (c *Client) HasAuth() bool {
return c.HttpConn != nil
}
func (c *Client) BasicQuery(queryString string) ([]byte, error) {
if c.HttpConn == nil {
return nil, errors.New("No Client OAuth")
}
response, err := c.HttpConn.Get(queryString)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
bits, err := ioutil.ReadAll(response.Body)
return bits, err
}
// User Timeline by UserID
func (c *Client) QueryUserTimelineByUserID(user_id string) (UserTimeline, []byte, error) {
requesURL := fmt.Sprintf("%s?user_id=%s", API_USER_TIMELINE, user_id)
data, err := c.BasicQuery(requesURL)
ret := UserTimeline{}
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// User Timeline by ScreenName
func (c *Client) QueryUserTimelineByScreenName(ScreeName string) (UserTimeline, []byte, error) {
requesURL := fmt.Sprintf("%s?screen_name=%s", API_USER_TIMELINE, ScreeName)
data, err := c.BasicQuery(requesURL)
ret := UserTimeline{}
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// Mentions Timeline.
func (c *Client) QueryMentionsTimeline(count int) (TimelineTweets, []byte, error) {
requesURL := fmt.Sprintf("%s?count=%d", API_MENTIONS_TIMELINE, count)
data, err := c.BasicQuery(requesURL)
ret := TimelineTweets{}
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// Query timeline by count
func (c *Client) QueryTimeLine(count int) (MentionsTimeline, []byte, error) {
requesURL := fmt.Sprintf("%s?count=%d", API_TIMELINE, count)
data, err := c.BasicQuery(requesURL)
ret := MentionsTimeline{}
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// Query follower timeline by count
func (c *Client) QueryFollower(count int) (Followers, []byte, error) {
requesURL := fmt.Sprintf("%s?count=%d", API_FOLLOWERS_LIST, count)
data, err := c.BasicQuery(requesURL)
ret := Followers{}
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// Query FollowerID by count
func (c *Client) QueryFollowerIDs(count int) (FollowerIDs, []byte, error) {
requesURL := fmt.Sprintf("%s?count=%d", API_FOLLOWERS_IDS, count)
data, err := c.BasicQuery(requesURL)
var ret FollowerIDs
err = json.Unmarshal(data, &ret)
return ret, data, err
}
// Query FollowerID by ID.
func (c *Client) QueryFollowerById(id int) (UserDetail, []byte, error) {
requesURL := fmt.Sprintf("%s?user_id=%d", API_FOLLOWER_INFO, id)
data, err := c.BasicQuery(requesURL)
var ret UserDetail
err = json.Unmarshal(data, &ret)
return ret, data, err
}