Skip to content

Commit 4800339

Browse files
authored
Merge pull request #2 from thecsw/big-update
Big update to mira. Breaking changes! Big version bump
2 parents 5dc6373 + 05a2a01 commit 4800339

File tree

16 files changed

+414
-163
lines changed

16 files changed

+414
-163
lines changed

README.md

+28-23
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ USER_AGENT =
5050

5151
## Examples
5252

53+
Note: Error checking is omitted for brevity.
54+
5355
### Streaming comment replies
5456

5557
Below is an example on how to make a simple bot that
@@ -60,15 +62,15 @@ package main
6062

6163
import (
6264
"github.com/thecsw/mira"
63-
"fmt"
6465
)
6566

6667
func main() {
68+
// Good practice is to check if the login errors out or not
6769
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
6870
c, _ := r.StreamCommentReplies()
6971
for {
70-
msg := <- c
71-
r.Reply(msg.GetId(), "I got your message!")
72+
msg := <-c
73+
r.Comment(msg.GetId()).Reply("I got your message!")
7274
}
7375
}
7476
```
@@ -83,15 +85,14 @@ package main
8385

8486
import (
8587
"github.com/thecsw/mira"
86-
"fmt"
8788
)
8889

8990
func main() {
9091
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
91-
c, _ := r.StreamNewPosts("subredditname")
92+
c, _, _ := r.Subreddit("all").StreamSubmissions()
9293
for {
93-
post := <- c
94-
r.Comment(post.GetId(), "I saw your submission!")
94+
post := <-c
95+
r.Submission(post.GetId()).Save("hello there")
9596
}
9697
}
9798
```
@@ -105,24 +106,32 @@ edit a comment.
105106
package main
106107

107108
import (
108-
"github.com/thecsw/mira"
109109
"fmt"
110+
111+
"github.com/thecsw/mira"
110112
)
111113

114+
// Errors are omitted for brevity
112115
func main() {
113116
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
117+
114118
// Make a submission
115-
post, _ := r.Submit("memeinvestor_test", "mypost", "my text")
119+
post, _ := r.Subreddit("mysubreddit").Submit("mytitle", "mytext")
120+
116121
// Comment on our new submission
117-
comment, _ := r.Comment(post.GetId(), "My First Comment")
122+
comment, _ := r.Submission(post.GetId()).Save("mycomment")
123+
118124
// Reply to our own comment
119-
reply, _ := r.Reply(comment.GetId(), "My Reply to the First Comment")
125+
reply, _ := r.Comment(comment.GetId()).Reply("myreply")
126+
120127
// Delete the reply
121-
r.DeleteComment(reply.GetId())
128+
r.Comment(reply.GetId()).Delete()
129+
122130
// Edit the first comment
123-
new_comment, _ := r.EditComment(comment.GetId(), "I Edited This!!")
131+
newComment, _ := r.Comment(comment.GetId()).Edit("myedit")
132+
124133
// Show the comment's body
125-
fmt.Println(new_comment.GetBody())
134+
fmt.Println(newComment.GetBody())
126135
}
127136
```
128137

@@ -135,16 +144,12 @@ package main
135144

136145
import (
137146
"github.com/thecsw/mira"
138-
"fmt"
139147
)
140148

141149
func main() {
142150
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
143151

144-
r.Compose("thecsw", "my subject", "hello, world")
145-
// or
146-
user, _ := r.GetUser("thecsw")
147-
r.Compose(user.GetName(), "my subject", "hello, world")
152+
r.Redditor("myuser").Compose("mytitle", "mytext")
148153
}
149154
```
150155

@@ -157,18 +162,18 @@ one of our methods.
157162
package main
158163

159164
import (
160-
"github.com/thecsw/mira"
161165
"fmt"
166+
167+
"github.com/thecsw/mira"
162168
)
163169

164170
func main() {
165171
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
166172
sort := "top"
167173
var limit int = 25
168174
duration := "all"
169-
subs, _ := r.GetSubredditPosts("subredditname", sort, duration, limit)
170-
171-
for _, v := range subs.GetChildren() {
175+
subs, _ := r.Subreddit("all").Submissions(sort, duration, limit)
176+
for _, v := range subs {
172177
fmt.Println("Submission Title: ", v.GetTitle())
173178
}
174179
}

auth.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ func Authenticate(c *Credentials) (*Reddit, error) {
5555
buf := new(bytes.Buffer)
5656
buf.ReadFrom(response.Body)
5757

58+
data := buf.Bytes()
59+
if err := findRedditError(data); err != nil {
60+
return nil, err
61+
}
62+
5863
auth := Reddit{}
59-
json.Unmarshal(buf.Bytes(), &auth)
64+
json.Unmarshal(data, &auth)
6065
auth.Creds = *c
6166
return &auth, nil
6267
}

examples/comments.go

-23
This file was deleted.

examples/comments/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/thecsw/mira"
7+
)
8+
9+
// Errors are omitted for brevity
10+
func main() {
11+
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
12+
13+
// Make a submission
14+
post, _ := r.Subreddit("mysubreddit").Submit("mytitle", "mytext")
15+
16+
// Comment on our new submission
17+
comment, _ := r.Submission(post.GetId()).Save("mycomment")
18+
19+
// Reply to our own comment
20+
reply, _ := r.Comment(comment.GetId()).Reply("myreply")
21+
22+
// Delete the reply
23+
r.Comment(reply.GetId()).Delete()
24+
25+
// Edit the first comment
26+
newComment, _ := r.Comment(comment.GetId()).Edit("myedit")
27+
28+
// Show the comment's body
29+
fmt.Println(newComment.GetBody())
30+
}

examples/composing.go

-14
This file was deleted.

examples/composing/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"github.com/thecsw/mira"
5+
)
6+
7+
func main() {
8+
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
9+
10+
r.Redditor("myuser").Compose("mytitle", "mytext")
11+
}

examples/get_submission_from_comment.go

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package examples
1+
package main
22

33
import (
44
"github.com/thecsw/mira"
55
)
66

7-
func StreamComments() {
7+
func main() {
8+
// Good practice is to check if the login errors out or not
89
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
910
c, _ := r.StreamCommentReplies()
1011
for {
1112
msg := <-c
12-
r.Reply(msg.GetId(), "I got your message!")
13+
r.Comment(msg.GetId()).Reply("I got your message!")
1314
}
1415
}

examples/stream_comments/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/thecsw/mira"
5+
)
6+
7+
func main() {
8+
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
9+
c, _, _ := r.Subreddit("all").StreamComments()
10+
for {
11+
msg := <-c
12+
r.Comment(msg.GetId()).Reply("myreply")
13+
}
14+
}

examples/stream_submissions.go

-14
This file was deleted.

examples/stream_submissions/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/thecsw/mira"
5+
)
6+
7+
func main() {
8+
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
9+
c, _, _ := r.Subreddit("all").StreamSubmissions()
10+
for {
11+
post := <-c
12+
r.Submission(post.GetId()).Save("hello there")
13+
}
14+
}

examples/sort_submissions.go examples/submissions/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
package examples
1+
package main
22

33
import (
44
"fmt"
55

66
"github.com/thecsw/mira"
77
)
88

9-
func SortSubmissions() {
9+
func main() {
1010
r, _ := mira.Init(mira.ReadCredsFromFile("login.conf"))
1111
sort := "top"
1212
var limit int = 25
1313
duration := "all"
14-
subs, _ := r.GetSubredditPosts("subredditname", sort, duration, limit)
15-
14+
subs, _ := r.Subreddit("all").Submissions(sort, duration, limit)
1615
for _, v := range subs {
1716
fmt.Println("Submission Title: ", v.GetTitle())
1817
}

0 commit comments

Comments
 (0)