-
Notifications
You must be signed in to change notification settings - Fork 1
/
grok.go
225 lines (186 loc) · 5.92 KB
/
grok.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package grokeddit
import (
"bytes"
"encoding/json"
"errors"
"io"
"reflect"
)
type Groked struct {
ListingPrev *GlobalId // Indicates thing before this listing (if listing was groked)
ListingNext *GlobalId // Indicates thing after this listing (if listing was groked)
Children []Thing // Children things to the listing, or the single thing groked
}
type thing struct {
Kind string
Data struct {
Author string // name of the poster
Body_html string // html from a comment
Created_utc float64 // utc of creation time
Display_name string // Name of subreddit (only used with subreddit thing type)
Edited interface{} // false or utc of last edit
Id string // unique indentifier for the thing
Link_id string // Identifies the link associated with a comment
Parent_id string // parent id of a comment
Replies json.RawMessage // replies to a comment
Selftext_html string // html from a new post
Subreddit string // name of the subreddit associated with the thing
Subreddit_id string // id of the subreddit associated with the thing
Title string // title of the post
Url string // url of the post
}
}
type listing struct {
Data struct {
Children []thing // the children of the listing
Before string // indicates value for prev filter
After string // indicates value for next filter
}
}
func createNewThing(in thing) (Thing, error) {
//
// Normalize Kind. Do first, since nothing else will work with
// unsupported kind being parsed.
//
currentKind, error := ParseKind(in.Kind)
if error != nil {
return Thing{}, errors.New("Unable to grok: " + error.Error())
}
//
// Normalize body text
//
var bodyHtml string
// sucks that the switch happens twice, but I'd rather do
// the unmarshal after we know it can be properly decoded
switch currentKind {
case Comment:
bodyHtml = in.Data.Body_html
case Link:
bodyHtml = in.Data.Selftext_html
default:
}
//
// Normalize creation and last modification timestamp
//
creationTime := int64(in.Data.Created_utc)
lastModificationTime := creationTime
if in.Data.Edited != nil {
editValue := reflect.ValueOf(in.Data.Edited)
if editValue.Kind() == reflect.Float64 {
lastModificationTime = int64(editValue.Float())
} else if editValue.Kind() != reflect.Bool {
return Thing{}, errors.New("Unable to grok: Unexpected type \"" + editValue.Type().String() + "\" for edited field")
}
}
//
// Normalize Id fields
//
thingId, error := ParseId(in.Data.Id) // thing id should always be present
if error != nil {
return Thing{}, errors.New("Unable to grok: " + error.Error())
}
subredditName := in.Data.Subreddit
subredditId := GlobalId{thingId, Subreddit}
parentId := GlobalId{}
linkId := GlobalId{}
if currentKind != Subreddit {
subredditId, error = ParseGlobalId(in.Data.Subreddit_id)
if error != nil {
return Thing{}, errors.New("Unable to grok subreddit id: " + error.Error())
}
if len(in.Data.Parent_id) != 0 {
parentId, error = ParseGlobalId(in.Data.Parent_id)
if error != nil {
return Thing{}, errors.New("Unable to grok parent id: " + error.Error())
}
}
if len(in.Data.Link_id) != 0 {
linkId, error = ParseGlobalId(in.Data.Link_id)
if error != nil {
return Thing{}, errors.New("Unable to grok link id: " + error.Error())
}
}
} else { // type subreddit
subredditName = in.Data.Display_name
}
//
// Cull through replies too
//
var newReplies Groked
// If reply field was present
if in.Data.Replies != nil && len(in.Data.Replies) > 0 {
// if reply field wasn't empty string
if in.Data.Replies[0] != '"' {
// gross! indirect recursion! easiest way for now
newReplies, error = GrokListing(bytes.NewReader(in.Data.Replies))
if error != nil {
return Thing{}, error
}
}
}
return Thing{
in.Data.Author,
creationTime,
GlobalId{thingId, currentKind},
lastModificationTime,
linkId,
parentId,
newReplies,
subredditName,
subredditId,
bodyHtml,
in.Data.Title,
in.Data.Url,
}, nil
}
func internalGrok(parsedListing listing) (Groked, error) {
groked := Groked{}
groked.Children = make([]Thing, 0, len(parsedListing.Data.Children))
if len(parsedListing.Data.Before) > 0 {
previousListing, error := ParseGlobalId(parsedListing.Data.Before)
if error != nil {
return groked, errors.New("Invalid previous listing value: " + error.Error())
}
groked.ListingPrev = &previousListing
}
if len(parsedListing.Data.After) > 0 {
nextListing, error := ParseGlobalId(parsedListing.Data.After)
if error != nil {
return groked, errors.New("Invalid next listing value: " + error.Error())
}
groked.ListingNext = &nextListing
}
for _, element := range parsedListing.Data.Children {
thing, error := createNewThing(element)
// There should be only "Things" in a child listing. So
// this will return an error if a non-thing is found.
if error != nil {
return groked, error
}
groked.Children = append(groked.Children, thing)
}
return groked, nil
}
func GrokListing(dataSource io.Reader) (Groked, error) {
var parsedData listing
if error := json.NewDecoder(dataSource).Decode(&parsedData); error != nil && error != io.EOF {
return Groked{}, errors.New("Unable to grok reddit JSON object: " + error.Error())
}
return internalGrok(parsedData)
}
func GrokListingArray(dataSource io.Reader) ([]Groked, error) {
var grokList []Groked
var parsedData []listing
if error := json.NewDecoder(dataSource).Decode(&parsedData); error != nil && error != io.EOF {
return grokList, errors.New("Unable to grok reddit JSON array: " + error.Error())
}
grokList = make([]Groked, len(parsedData))
for index, parsedListing := range parsedData {
newGrok, error := internalGrok(parsedListing)
if error != nil {
return grokList, error
}
grokList[index] = newGrok
}
return grokList, nil
}