Skip to content

Commit 39b8cdf

Browse files
committed
Added new MiraInterface for Info()
1 parent eb07f6e commit 39b8cdf

12 files changed

+155
-289
lines changed

auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (c *Reddit) SetDefault() {
8888
c.Stream = Streaming{
8989
CommentListInterval: 8,
9090
PostListInterval: 10,
91-
PostListSlice: 8,
91+
PostListSlice: 25,
9292
}
9393
c.Values = RedditVals{
9494
GetSubmissionFromCommentTries: 12,

comment.go

+15-73
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,17 @@
11
package mira
22

3-
// Get ID of the comment. Should be something "t1_..."
4-
func (c CommentWrap) GetId() string {
5-
return c.Json.Data.Things[0].Data.Name
6-
}
7-
8-
func (c CommentWrap) GetSubredditId() string {
9-
return c.Json.Data.Things[0].Data.SubredditId
10-
}
11-
12-
// Get ID of the comment. Should be something "t1_..."
13-
func (c CommentWrap) GetParentId() string {
14-
return c.Json.Data.Things[0].Data.ParentId
15-
}
16-
17-
// Get the name of the author. With no u/ preppended
18-
func (c CommentWrap) GetAuthor() string {
19-
return c.Json.Data.Things[0].Data.Author
20-
}
21-
22-
// Get the name of the author. With no u/ preppended
23-
func (c CommentWrap) GetAuthorId() string {
24-
return c.Json.Data.Things[0].Data.AuthorFullname
25-
}
26-
27-
// Get the subreddit's name. With no r/ preppended
28-
func (c CommentWrap) GetSubreddit() string {
29-
return c.Json.Data.Things[0].Data.Subreddit
30-
}
31-
32-
// Get the UNIX timestamp when the comment was created
33-
func (c CommentWrap) CreatedAt() float64 {
34-
return c.Json.Data.Things[0].Data.Created
35-
}
36-
37-
// Get the body of the message
38-
func (c CommentWrap) GetBody() string {
39-
return c.Json.Data.Things[0].Data.Body
40-
}
41-
42-
// Get the score of the comment (Ups - Downs)
43-
func (c CommentWrap) GetScore() float64 {
44-
return c.Json.Data.Things[0].Data.Score
45-
}
46-
47-
// Get the number of upvotes on the comment
48-
func (c CommentWrap) GetUps() float64 {
49-
return c.Json.Data.Things[0].Data.Ups
50-
}
51-
52-
// Get the number of downvotes
53-
func (c CommentWrap) GetDowns() float64 {
54-
return c.Json.Data.Things[0].Data.Downs
55-
}
56-
57-
// Return true if the comment is stickied, false otherwise
58-
func (c CommentWrap) IsSticky() bool {
59-
return c.Json.Data.Things[0].Data.Stickied
60-
}
61-
62-
// Return true if the comment is removed, false otherwise
63-
func (c CommentWrap) IsRemoved() bool {
64-
return c.Json.Data.Things[0].Data.Removed
65-
}
66-
67-
// Return true if the comment is approved, false otherwise
68-
func (c CommentWrap) IsApproved() bool {
69-
return c.Json.Data.Things[0].Data.Approved
70-
}
71-
72-
// Return true if the author is submission's author, false otherwise
73-
func (c CommentWrap) IsAuthor() bool {
74-
return c.Json.Data.Things[0].Data.IsSubmitter
75-
}
3+
func (c CommentWrap) GetId() string { return c.Json.Data.Things[0].Data.Name }
4+
func (c CommentWrap) GetSubredditId() string { return c.Json.Data.Things[0].Data.SubredditId }
5+
func (c CommentWrap) GetParentId() string { return c.Json.Data.Things[0].Data.ParentId }
6+
func (c CommentWrap) GetAuthor() string { return c.Json.Data.Things[0].Data.Author }
7+
func (c CommentWrap) GetAuthorId() string { return c.Json.Data.Things[0].Data.AuthorFullname }
8+
func (c CommentWrap) GetSubreddit() string { return c.Json.Data.Things[0].Data.Subreddit }
9+
func (c CommentWrap) CreatedAt() float64 { return c.Json.Data.Things[0].Data.Created }
10+
func (c CommentWrap) GetBody() string { return c.Json.Data.Things[0].Data.Body }
11+
func (c CommentWrap) GetScore() float64 { return c.Json.Data.Things[0].Data.Score }
12+
func (c CommentWrap) GetUps() float64 { return c.Json.Data.Things[0].Data.Ups }
13+
func (c CommentWrap) GetDowns() float64 { return c.Json.Data.Things[0].Data.Downs }
14+
func (c CommentWrap) IsSticky() bool { return c.Json.Data.Things[0].Data.Stickied }
15+
func (c CommentWrap) IsRemoved() bool { return c.Json.Data.Things[0].Data.Removed }
16+
func (c CommentWrap) IsApproved() bool { return c.Json.Data.Things[0].Data.Approved }
17+
func (c CommentWrap) IsAuthor() bool { return c.Json.Data.Things[0].Data.IsSubmitter }

commentlisting.go

+17-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
package mira
22

3-
func (l *CommentListing) GetChildren() []Comment {
4-
return l.Data.Children
5-
}
6-
7-
func (ldc *Comment) GetId() string {
8-
return ldc.Data.Name
9-
}
10-
11-
func (ldc *Comment) GetParentId() string {
12-
return ldc.Data.ParentId
13-
}
14-
15-
func (ldc *Comment) IsRoot() bool {
16-
return string(ldc.Data.ParentId[1]) == "3"
17-
}
18-
19-
func (ldc *Comment) GetBody() string {
20-
return ldc.Data.Body
21-
}
22-
23-
func (ldc *Comment) GetSubreddit() string {
24-
return ldc.Data.Subreddit
25-
}
26-
27-
func (ldc *Comment) GetScore() float64 {
28-
return ldc.Data.Score
29-
}
30-
31-
func (ldc *Comment) GetCreated() float64 {
32-
return ldc.Data.Created
33-
}
34-
35-
func (ldc *Comment) GetAuthor() string {
36-
return ldc.Data.Author
37-
}
38-
39-
func (ldc *Comment) IsComment() bool {
40-
return ldc.Kind == "t1"
41-
}
42-
43-
func (ldc *Comment) IsCommentReply() bool {
44-
return ldc.Data.Subject == "comment reply"
45-
}
3+
func (l *CommentListing) GetChildren() []Comment { return l.Data.Children }
4+
func (ldc *Comment) GetId() string { return ldc.Data.Name }
5+
func (ldc *Comment) GetParentId() string { return ldc.Data.ParentId }
6+
func (ldc *Comment) IsRoot() bool { return string(ldc.Data.ParentId[1]) == "3" }
7+
func (ldc *Comment) GetTitle() string { return ldc.Data.Body }
8+
func (ldc *Comment) GetBody() string { return ldc.Data.Body }
9+
func (ldc *Comment) GetSubreddit() string { return ldc.Data.Subreddit }
10+
func (ldc *Comment) GetUps() float64 { return ldc.Data.Score }
11+
func (ldc *Comment) GetDowns() float64 { return 0 }
12+
func (ldc *Comment) GetCreated() float64 { return ldc.Data.Created }
13+
func (ldc *Comment) GetAuthor() string { return ldc.Data.Author }
14+
func (ldc *Comment) IsComment() bool { return ldc.Kind == "t1" }
15+
func (ldc *Comment) IsCommentReply() bool { return ldc.Data.Subject == "comment reply" }
16+
func (ldc *Comment) GetName() string { return ldc.Data.Name }
17+
func (ldc *Comment) GetKarma() float64 { return ldc.Data.Score }
18+
func (ldc *Comment) GetUrl() string { return ldc.Data.LinkTitle }
19+
func (ldc *Comment) GetFlair() string { return ldc.Data.Context }

me.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package mira
2+
3+
func (me *Me) GetId() string { return me.Id }
4+
func (me *Me) GetName() string { return me.Name }
5+
func (me *Me) GetAuthor() string { return me.Name }
6+
func (me *Me) GetParentId() string { return me.Id }
7+
func (me *Me) GetTitle() string { return me.Name }
8+
func (me *Me) GetBody() string { return me.Name }
9+
func (me *Me) GetKarma() float64 { return me.CommentKarma + me.LinkKarma }
10+
func (me *Me) GetUps() float64 { return 0 }
11+
func (me *Me) GetDowns() float64 { return 0 }
12+
func (me *Me) GetSubreddit() string { return me.Name }
13+
func (me *Me) GetCreated() float64 { return me.Created }
14+
func (me *Me) GetFlair() string { return "" }
15+
func (me *Me) GetUrl() string { return me.IconImg }
16+
func (me *Me) IsRoot() bool { return true }

post.go

+22-77
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,24 @@
11
package mira
22

3-
import "time"
4-
5-
func (p *PostListing) GetChildren() []PostListingChild {
6-
return p.Data.Children
7-
}
8-
9-
func (plc *PostListingChild) GetAge() float64 {
10-
return float64(time.Now().UTC().Unix()) - plc.Data.CreatedUtc
11-
}
12-
13-
func (plc *PostListingChild) GetTimeCreated() float64 {
14-
return plc.Data.CreatedUtc
15-
}
16-
17-
func (plc *PostListingChild) GetSubreddit() string {
18-
return plc.Data.Subreddit
19-
}
20-
21-
func (plc *PostListingChild) GetSubredditId() string {
22-
return plc.Data.SubredditId
23-
}
24-
25-
func (plc *PostListingChild) GetTitle() string {
26-
return plc.Data.Title
27-
}
28-
29-
func (plc *PostListingChild) GetId() string {
30-
return plc.Data.Name
31-
}
32-
33-
func (plc *PostListingChild) GetAuthor() string {
34-
return plc.Data.Author
35-
}
36-
37-
func (plc *PostListingChild) GetAuthorId() string {
38-
return plc.Data.AuthorFullname
39-
}
40-
41-
func (plc *PostListingChild) GetCreated() float64 {
42-
return plc.Data.Created
43-
}
44-
45-
func (plc *PostListingChild) GetUps() float64 {
46-
return plc.Data.Ups
47-
}
48-
49-
func (plc *PostListingChild) GetDowns() float64 {
50-
return plc.Data.Downs
51-
}
52-
53-
func (plc *PostListingChild) GetScore() float64 {
54-
return plc.Data.Score
55-
}
56-
57-
func (plc *PostListingChild) GetText() string {
58-
return plc.Data.Selftext
59-
}
60-
61-
func (plc *PostListingChild) GetAuthorFlair() string {
62-
return plc.Data.AuthorFlairText
63-
}
64-
65-
func (plc *PostListingChild) GetPermalink() string {
66-
return plc.Data.Permalink
67-
}
68-
69-
func (plc *PostListingChild) GetUrl() string {
70-
return plc.Data.Url
71-
}
72-
73-
func (plc *PostListingChild) GetCommentCount() float64 {
74-
return plc.Data.NumComments
75-
}
76-
77-
func (plc *PostListingChild) GetCrosspostCount() float64 {
78-
return plc.Data.NumCrossposts
79-
}
3+
func (p *PostListing) GetChildren() []PostListingChild { return p.Data.Children }
4+
func (plc *PostListingChild) GetSubreddit() string { return plc.Data.Subreddit }
5+
func (plc *PostListingChild) GetSubredditId() string { return plc.Data.SubredditId }
6+
func (plc *PostListingChild) GetName() string { return plc.Data.Title }
7+
func (plc *PostListingChild) GetTitle() string { return plc.Data.Title }
8+
func (plc *PostListingChild) GetId() string { return plc.Data.Name }
9+
func (plc *PostListingChild) GetParentId() string { return plc.Data.Name }
10+
func (plc *PostListingChild) GetAuthor() string { return plc.Data.Author }
11+
func (plc *PostListingChild) GetAuthorId() string { return plc.Data.AuthorFullname }
12+
func (plc *PostListingChild) GetCreated() float64 { return plc.Data.Created }
13+
func (plc *PostListingChild) GetKarma() float64 { return plc.Data.Ups - plc.Data.Downs }
14+
func (plc *PostListingChild) GetUps() float64 { return plc.Data.Ups }
15+
func (plc *PostListingChild) GetDowns() float64 { return plc.Data.Downs }
16+
func (plc *PostListingChild) GetScore() float64 { return plc.Data.Score }
17+
func (plc *PostListingChild) GetBody() string { return plc.Data.Selftext }
18+
func (plc *PostListingChild) GetAuthorFlair() string { return plc.Data.AuthorFlairText }
19+
func (plc *PostListingChild) GetPermalink() string { return plc.Data.Permalink }
20+
func (plc *PostListingChild) GetUrl() string { return plc.Data.Url }
21+
func (plc *PostListingChild) GetFlair() string { return plc.Data.LinkFlairText }
22+
func (plc *PostListingChild) GetCommentCount() float64 { return plc.Data.NumComments }
23+
func (plc *PostListingChild) GetCrosspostCount() float64 { return plc.Data.NumCrossposts }
24+
func (plc *PostListingChild) IsRoot() bool { return true }

reddit.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func (c *Reddit) Me() *Reddit {
4343
return c
4444
}
4545

46-
func (c *Reddit) Subreddit(name string) *Reddit {
47-
c.Chain.Name = name
46+
func (c *Reddit) Subreddit(name ...string) *Reddit {
47+
c.Chain.Name = strings.Join(name, "+")
4848
c.Chain.Type = "subreddit"
4949
return c
5050
}
@@ -106,7 +106,7 @@ func (c *Reddit) Comments(sort string, tdur string, limit int) ([]Comment, error
106106
}
107107
}
108108

109-
func (c *Reddit) Info() (interface{}, error) {
109+
func (c *Reddit) Info() (MiraInterface, error) {
110110
switch c.Chain.Type {
111111
case "me":
112112
return c.getMe()
@@ -134,14 +134,14 @@ func (c *Reddit) getMe() (*Me, error) {
134134
return ret, nil
135135
}
136136

137-
func (c *Reddit) getSubmission(id string) (*PostListing, error) {
137+
func (c *Reddit) getSubmission(id string) (*PostListingChild, error) {
138138
target := RedditOauth + "/api/info.json"
139139
ans, err := c.MiraRequest("GET", target, map[string]string{
140140
"id": id,
141141
})
142142
ret := &PostListing{}
143143
json.Unmarshal(ans, ret)
144-
return ret, err
144+
return &ret.GetChildren()[0], err
145145
}
146146

147147
func (c *Reddit) getComment(id string) (*Comment, error) {
@@ -500,20 +500,32 @@ func (c *Reddit) Compose(subject, text string) error {
500500
}
501501

502502
func (c *Reddit) ReadMessage(message_id string) error {
503+
err := c.checkType("me")
504+
if err != nil {
505+
return err
506+
}
503507
target := RedditOauth + "/api/read_message"
504-
_, err := c.MiraRequest("POST", target, map[string]string{
508+
_, err = c.MiraRequest("POST", target, map[string]string{
505509
"id": message_id,
506510
})
507511
return err
508512
}
509513

510514
func (c *Reddit) ReadAllMessages() error {
515+
err := c.checkType("me")
516+
if err != nil {
517+
return err
518+
}
511519
target := RedditOauth + "/api/read_all_messages"
512-
_, err := c.MiraRequest("POST", target, nil)
520+
_, err = c.MiraRequest("POST", target, nil)
513521
return err
514522
}
515523

516524
func (c *Reddit) ListUnreadMessages() ([]Comment, error) {
525+
err := c.checkType("me")
526+
if err != nil {
527+
return nil, err
528+
}
517529
target := RedditOauth + "/message/unread"
518530
ans, err := c.MiraRequest("GET", target, map[string]string{
519531
"mark": "true",
@@ -523,18 +535,23 @@ func (c *Reddit) ListUnreadMessages() ([]Comment, error) {
523535
return ret.GetChildren(), err
524536
}
525537

526-
func (c *Reddit) SubredditUpdateSidebar(sr, text string) ([]byte, error) {
538+
func (c *Reddit) SubredditUpdateSidebar(text string) error {
539+
err := c.checkType("subreddit")
540+
if err != nil {
541+
return err
542+
}
527543
target := RedditOauth + "/api/site_admin"
528-
return c.MiraRequest("POST", target, map[string]string{
529-
"sr": sr,
544+
_, err = c.MiraRequest("POST", target, map[string]string{
545+
"sr": c.Chain.Name,
530546
"name": "None",
531547
"description": text,
532-
"title": sr,
548+
"title": c.Chain.Name,
533549
"wikimode": "anyone",
534550
"link_type": "any",
535551
"type": "public",
536552
"api_type": "json",
537553
})
554+
return err
538555
}
539556

540557
func (c *Reddit) checkType(rtype ...string) error {

0 commit comments

Comments
 (0)