-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreddit.go
132 lines (119 loc) · 3.54 KB
/
reddit.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package mira
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/thecsw/mira/v4/models"
)
// MiraRequest Reddit API is always developing and I can't implement all endpoints;
// It will be a bit of a bloat; Instead, you have accessto *Reddit.MiraRequest
// method that will let you to do any custom reddit api calls!
//
// Here is the signature:
//
// func (c *Reddit) MiraRequest(method string, target string, payload map[string]string) ([]byte, error) {...}
//
// It is pretty straight-forward, the return is a slice of bytes; Parse it yourself.
func (c *Reddit) MiraRequest(method string, target string, payload map[string]string) ([]byte, error) {
values := "?"
for i, v := range payload {
v = url.QueryEscape(v)
values += fmt.Sprintf("%s=%s&", i, v)
}
values = values[:len(values)-1]
r, err := http.NewRequest(method, target+values, nil)
if err != nil {
return nil, err
}
r.Header.Set("User-Agent", c.Creds.UserAgent)
r.Header.Set("Authorization", "Bearer "+c.Token)
response, err := c.Client.Do(r)
if err != nil {
return nil, err
}
defer response.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
data := buf.Bytes()
if err := findRedditError(data); err != nil {
return nil, err
}
return data, nil
}
// Me pushes a new Redditor value.
func (c *Reddit) Me() *Reddit {
return c.addQueue(c.Creds.Username, meType)
}
// Subreddit pushes a new subreddit value to the internal queue.
func (c *Reddit) Subreddit(name ...string) *Reddit {
return c.addQueue(strings.Join(name, "+"), subredditType)
}
// Submission pushes a new submission value to the internal queue.
func (c *Reddit) Submission(name string) *Reddit {
return c.addQueue(name, submissionType)
}
// Comment pushes a new comment value to the internal queue.
func (c *Reddit) Comment(name string) *Reddit {
return c.addQueue(name, commentType)
}
// Redditor pushes a new redditor value to the internal queue.
func (c *Reddit) Redditor(name string) *Reddit {
return c.addQueue(name, redditorType)
}
// Info returns MiraInterface of last pushed object.
func (c *Reddit) Info() (MiraInterface, error) {
name, ttype := c.getQueue()
switch ttype {
case meType:
return c.getMe()
case submissionType:
return c.getSubmission(name)
case commentType:
return c.getComment(name)
case subredditType:
return c.getSubreddit(name)
case redditorType:
return c.getUser(name)
default:
return nil, fmt.Errorf("returning type is not defined")
}
}
func (c *Reddit) getMe() (models.Me, error) {
target := RedditOauth + "/api/v1/me"
ret := &models.Me{}
ans, err := c.MiraRequest("GET", target, nil)
if err != nil {
return *ret, err
}
json.Unmarshal(ans, ret)
return *ret, nil
}
func (c *Reddit) getSubmission(id string) (models.PostListingChild, error) {
target := RedditOauth + "/api/info.json"
ans, err := c.MiraRequest("GET", target, map[string]string{
"id": id,
})
ret := &models.PostListing{}
json.Unmarshal(ans, ret)
if len(ret.GetChildren()) < 1 {
return models.PostListingChild{}, fmt.Errorf("id not found")
}
return ret.GetChildren()[0], err
}
func (c *Reddit) getUser(name string) (models.Redditor, error) {
target := RedditOauth + "/user/" + name + "/about"
ans, err := c.MiraRequest(http.MethodGet, target, nil)
ret := &models.Redditor{}
json.Unmarshal(ans, ret)
return *ret, err
}
func (c *Reddit) getSubreddit(name string) (models.Subreddit, error) {
target := RedditOauth + "/r/" + name + "/about"
ans, err := c.MiraRequest(http.MethodGet, target, nil)
ret := &models.Subreddit{}
json.Unmarshal(ans, ret)
return *ret, err
}