-
Notifications
You must be signed in to change notification settings - Fork 1
/
reddit.go
96 lines (82 loc) · 2.76 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
package opinions
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/macie/opinions/internal/html"
)
// RedditResponse represents some interesting fields of response from Reddit API.
type RedditResponse struct {
Data struct {
Children []struct {
Data struct {
ID string `json:"permalink"`
Title string `json:"title"`
URL string `json:"url"`
NumComments int `json:"num_comments"`
} `json:"data"`
} `json:"children"`
} `json:"data"`
}
// UnmarshalJSON deserialize inconsistent JSON responses to RedditResponse.
// Reddit returns empty object ("{}") when there are no search results.
func (r *RedditResponse) UnmarshalJSON(b []byte) error {
isEmptyResponse := len(b) == 4 && string(b) == "\"{}\""
if isEmptyResponse {
return nil
}
// new type prevents recursive calls to RedditResponse.UnmarshalJSON()
type resp *RedditResponse
return json.Unmarshal(b, resp(r))
}
// SearchReddit searches Reddit for given query and returns list of discussions
// sorted by relevance.
//
// See: https://www.reddit.com/dev/api#GET_search
func SearchReddit(ctx context.Context, client GetRequester, query string) (discussions []Discussion, err error) {
searchURL := "https://www.reddit.com/search.json?sort=relevance&t=all&q="
r, err := client.Get(ctx, searchURL+url.QueryEscape(query))
if err != nil {
return noDiscussions, err
}
defer func() {
if closeErr := r.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
if r.StatusCode != http.StatusOK {
if r.Header.Get("X-Ratelimit-Remaining") == "0" { // https://support.reddithelp.com/hc/en-us/articles/16160319875092-Reddit-Data-API-Wiki
return noDiscussions, fmt.Errorf("too many requests. Wait %s seconds", r.Header.Get("X-Ratelimit-Reset"))
}
if r.StatusCode == http.StatusForbidden {
var details string
body, err := html.Parse(r.Body)
if err != nil {
details = ""
}
details = html.Text(html.First(body, "div > div > div > div > div"))
if details == "" {
details = "status 403"
}
return noDiscussions, fmt.Errorf("your IP address seems to be banned by Reddit: `GET %s` responded '%s'", r.Request.URL, details)
}
return noDiscussions, fmt.Errorf("`GET %s` responded with status code %d", r.Request.URL, r.StatusCode)
}
var response RedditResponse
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
return noDiscussions, err
}
discussions = make([]Discussion, 0, len(response.Data.Children))
for _, entry := range response.Data.Children {
discussions = append(discussions, Discussion{
Service: "Reddit",
URL: "https://reddit.com" + entry.Data.ID,
Title: entry.Data.Title,
Source: entry.Data.URL,
Comments: entry.Data.NumComments,
})
}
return discussions, nil
}