Update README.md and license. Add examples
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/vartanbeno/go-reddit"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() (err error) {
|
||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
||||
|
||||
client, err := reddit.NewClient(nil, withCredentials)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client.OnRequestCompleted(logResponse)
|
||||
|
||||
client.Subreddit.Search(ctx, "programming", reddit.SetLimit(10))
|
||||
client.Subreddit.SearchNames(ctx, "monitor")
|
||||
client.Subreddit.SearchPosts(ctx, "react", "webdev", reddit.SortByNumberOfComments)
|
||||
client.User.Posts(ctx, reddit.SetLimit(50))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func logResponse(req *http.Request, res *http.Response) {
|
||||
fmt.Printf("%s %s %s\n", req.Method, req.URL, res.Status)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/vartanbeno/go-reddit"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() (err error) {
|
||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
||||
|
||||
client, err := reddit.NewClient(nil, withCredentials)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sr, _, err := client.Subreddit.Get(ctx, "golang")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%s was created on %s and has %d subscribers.\n", sr.NamePrefixed, sr.Created.Local(), sr.Subscribers)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/vartanbeno/go-reddit"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() (err error) {
|
||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
||||
|
||||
client, err := reddit.NewClient(nil, withCredentials)
|
||||
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.
|
||||
result, _, err := client.Subreddit.Top(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime)
|
||||
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.
|
||||
result, _, err = client.Subreddit.Top(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime, reddit.SetAfter(result.After))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, post := range result.Posts {
|
||||
fmt.Println(post.Title)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/vartanbeno/go-reddit"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() (err error) {
|
||||
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
|
||||
|
||||
client, err := reddit.NewClient(nil, withCredentials)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
post, _, err := client.Post.SubmitText(ctx, reddit.SubmitTextOptions{
|
||||
Subreddit: "test",
|
||||
Title: "This is a title",
|
||||
Text: "This is some text",
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("The text post is available at: %s\n", post.URL)
|
||||
|
||||
post, _, err = client.Post.SubmitLink(ctx, reddit.SubmitLinkOptions{
|
||||
Subreddit: "test",
|
||||
Title: "This is a title",
|
||||
URL: "http://example.com",
|
||||
Resubmit: true,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("The link post is available at: %s\n", post.URL)
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user