This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
forked from cameronstanley/go-reddit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlink.go
244 lines (209 loc) · 7.2 KB
/
link.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package reddit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
// Link contains information about a link.
type Link struct {
ApprovedBy string `json:"approved_by"`
Archived bool `json:"archived"`
Author string `json:"author"`
AuthorFlairCSSClass string `json:"author_flair_css_class"`
AuthorFlairText string `json:"author_flair_text"`
BannedBy string `json:"banned_by"`
Clicked bool `json:"clicked"`
ContestMode bool `json:"contest_mode"`
Created float64 `json:"created"`
CreatedUtc float64 `json:"created_utc"`
CrosspostParent string `json:"crosspost_parent"`
CrosspostParentList []*Link `json:"crosspost_parent_list"`
Distinguished string `json:"distinguished"`
Domain string `json:"domain"`
Downs int `json:"downs"`
// This can be a bool or a timestamp apperentely, what the fuck
Edited interface{} `json:"edited"`
Gilded int `json:"gilded"`
Hidden bool `json:"hidden"`
HideScore bool `json:"hide_score"`
ID string `json:"id"`
IsSelf bool `json:"is_self"`
Likes bool `json:"likes"`
LinkFlairCSSClass string `json:"link_flair_css_class"`
LinkFlairText string `json:"link_flair_text"`
Locked bool `json:"locked"`
Media Media `json:"media"`
MediaEmbed interface{} `json:"media_embed"`
ModReports []interface{} `json:"mod_reports"`
Name string `json:"name"`
NumComments int `json:"num_comments"`
NumReports int `json:"num_reports"`
Over18 bool `json:"over_18"`
Permalink string `json:"permalink"`
PostHint string `json:"post_hint"`
Quarantine bool `json:"quarantine"`
RemovalReason interface{} `json:"removal_reason"`
ReportReasons []interface{} `json:"report_reasons"`
Saved bool `json:"saved"`
Score int `json:"score"`
SecureMedia interface{} `json:"secure_media"`
SecureMediaEmbed interface{} `json:"secure_media_embed"`
SelftextHTML string `json:"selftext_html"`
Selftext string `json:"selftext"`
Stickied bool `json:"stickied"`
Subreddit string `json:"subreddit"`
SubredditID string `json:"subreddit_id"`
SuggestedSort string `json:"suggested_sort"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
URL string `json:"url"`
Ups int `json:"ups"`
UserReports []interface{} `json:"user_reports"`
Visited bool `json:"visited"`
IsRobotIndexable bool `json:"is_robot_indexable"`
Spoiler bool `json:"spoiler"`
}
const linkType = "t3"
type linkListing struct {
Kind string `json:"kind"`
Data struct {
Modhash string `json:"modhash"`
Children []struct {
Kind string `json:"kind"`
Data Link `json:"data"`
} `json:"children"`
After string `json:"after"`
Before interface{} `json:"before"`
} `json:"data"`
}
// CommentOnLink posts a top-level comment to the given link. Requires the 'submit' OAuth scope.
func (c *Client) CommentOnLink(linkID string, text string) error {
return c.commentOnThing(fmt.Sprintf("%s_%s", linkType, linkID), text)
}
// DeleteLink deletes a link submitted by the currently authenticated user. Requires the 'edit' OAuth scope.
func (c *Client) DeleteLink(linkID string) error {
return c.deleteThing(fmt.Sprintf("%s_%s", linkType, linkID))
}
// EditLinkText edits the text of a self post by the currently authenticated user. Requires the 'edit' OAuth scope.
func (c *Client) EditLinkText(linkID string, text string) error {
return c.editThingText(fmt.Sprintf("%s_%s", linkType, linkID), text)
}
// GetHotLinks retrieves a listing of hot links.
func (c *Client) GetHotLinks(subreddit string) ([]*Link, error) {
return c.getLinks(subreddit, "hot", "", "")
}
// GetNewLinks retrieves a listing of new links.
func (c *Client) GetNewLinks(subreddit, before, after string) ([]*Link, error) {
return c.getLinks(subreddit, "new", before, after)
}
// GetTopLinks retrieves a listing of top links.
func (c *Client) GetTopLinks(subreddit string) ([]*Link, error) {
return c.getLinks(subreddit, "top", "", "")
}
// HideLink removes the given link from the user's default view of subreddit listings. Requires the 'report' OAuth scope.
func (c *Client) HideLink(linkID string) error {
data := url.Values{}
data.Set("id", fmt.Sprintf("%s_%s", linkType, linkID))
url := fmt.Sprintf("%s/api/hide", baseAuthURL)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(data.Encode()))
if err != nil {
return err
}
req.Header.Add("User-Agent", c.userAgent)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
resp, err := c.http.Do(req)
if err != nil {
return err
} else if resp.StatusCode >= 400 {
return errors.New(fmt.Sprintf("HTTP Status Code: %d", resp.StatusCode))
}
defer resp.Body.Close()
return nil
}
func (c *Client) getLinks(subreddit string, sort, before, after string) ([]*Link, error) {
url := fmt.Sprintf("%s/r/%s/%s.json?limit=100&raw_json=1", baseURL, subreddit, sort)
if before != "" {
url += "&before=" + before
} else if after != "" {
url += "&after=" + after
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.userAgent)
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, NewError(resp)
}
defer resp.Body.Close()
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result linkListing
err = json.Unmarshal(d, &result)
if err != nil {
if _, ok := err.(*json.UnmarshalTypeError); ok {
log.Println(string(d))
log.Printf("%#v", err)
} else {
return nil, err
}
}
var links []*Link
for _, link := range result.Data.Children {
anotherCopy := link
links = append(links, &anotherCopy.Data)
}
return links, nil
}
func (c *Client) LinksInfo(fullnames []string) ([]*Link, error) {
uri := baseURL + "/api/info.json?"
param := strings.Join(fullnames, ",")
uri += "id=" + param
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.userAgent)
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, NewError(resp)
}
defer resp.Body.Close()
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result linkListing
err = json.Unmarshal(d, &result)
if err != nil {
if _, ok := err.(*json.UnmarshalTypeError); ok {
log.Println(string(d))
log.Printf("%#v", err)
} else {
return nil, err
}
}
var links []*Link
for _, link := range result.Data.Children {
anotherCopy := link
links = append(links, &anotherCopy.Data)
}
return links, nil
}