Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update README.md and license. Add examples
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
- Loading branch information
1 parent
730da46
commit 7336778
Showing
6 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |