-
Notifications
You must be signed in to change notification settings - Fork 12
/
link.go
165 lines (144 loc) · 5.66 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
package reddit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
// 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 int `json:"created"`
CreatedUtc int `json:"created_utc"`
Distinguished string `json:"distinguished"`
Domain string `json:"domain"`
Downs int `json:"downs"`
Edited bool `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"`
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"`
}
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 string) ([]*Link, error) {
return c.getLinks(subreddit, "new")
}
// 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 string) ([]*Link, error) {
url := fmt.Sprintf("%s/r/%s/%s.json", baseURL, subreddit, sort)
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
}
defer resp.Body.Close()
var result linkListing
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
var links []*Link
for _, link := range result.Data.Children {
links = append(links, &link.Data)
}
return links, nil
}