2020-08-02 15:59:25 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-08-20 14:37:59 -04:00
|
|
|
"github.com/vartanbeno/go-reddit/reddit"
|
2020-08-02 15:59:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() (err error) {
|
2020-08-23 22:25:29 -04:00
|
|
|
credentials := &reddit.Credentials{
|
|
|
|
ID: "id",
|
|
|
|
Secret: "secret",
|
|
|
|
Username: "username",
|
|
|
|
Password: "password",
|
|
|
|
}
|
2020-08-02 15:59:25 -04:00
|
|
|
|
2020-08-26 23:13:34 -04:00
|
|
|
client, err := reddit.NewClient(credentials)
|
2020-08-02 15:59:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's get the top 200 posts of r/golang.
|
|
|
|
// Reddit returns a maximum of 100 posts at a time,
|
|
|
|
// so we'll need to separate this into 2 requests.
|
2020-08-05 13:25:09 -04:00
|
|
|
result, _, err := client.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{
|
|
|
|
ListOptions: reddit.ListOptions{
|
|
|
|
Limit: 100,
|
|
|
|
},
|
|
|
|
Time: "all",
|
|
|
|
})
|
2020-08-02 15:59:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range result.Posts {
|
|
|
|
fmt.Println(post.Title)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The SetAfter option sets the id of an item that Reddit
|
|
|
|
// will use as an anchor point for the returned listing.
|
2020-08-05 13:25:09 -04:00
|
|
|
result, _, err = client.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{
|
|
|
|
ListOptions: reddit.ListOptions{
|
|
|
|
Limit: 100,
|
|
|
|
After: result.After,
|
|
|
|
},
|
|
|
|
Time: "all",
|
|
|
|
})
|
2020-08-02 15:59:25 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range result.Posts {
|
|
|
|
fmt.Println(post.Title)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|