From 5aef0aa174547652028fc3e582a28af33e07f4bd Mon Sep 17 00:00:00 2001 From: Stefan M Date: Wed, 15 May 2024 15:42:36 +0200 Subject: [PATCH] Add reddit source (#22) --- source/reddit.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 source/reddit.go diff --git a/source/reddit.go b/source/reddit.go new file mode 100644 index 0000000..7da0ef4 --- /dev/null +++ b/source/reddit.go @@ -0,0 +1,70 @@ +package source + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/ioki-mobility/summaraizer" +) + +// Reddit is a source that fetches comments from a Reddit post. +type Reddit struct { + UrlPath string // The path to the Reddit post. Everything after `https://www.reddit.com`. +} + +// Fetch fetches comments from a Reddit post. +func (r *Reddit) Fetch() (summaraizer.Comments, error) { + request, err := http.NewRequest("GET", "https://www.reddit.com"+r.UrlPath+".json", nil) + request.Header.Set("User-Agent", "Go:summaraizer:0.0.0") + if err != nil { + return nil, err + } + resp, err := http.DefaultClient.Do(request) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var response []redditResponse + err = json.Unmarshal(body, &response) + if err != nil { + return nil, err + } + + var comments summaraizer.Comments + firstPost := response[0].Data.Children[0].Data + fistPostBody := fmt.Sprintf("%s\n\n%s", firstPost.Title, firstPost.Selftext) + comments = append(comments, summaraizer.Comment{ + Author: firstPost.Author, + Body: fistPostBody, + }) + if len(response) == 2 { + for _, child := range response[1].Data.Children { + comments = append(comments, summaraizer.Comment{ + Author: child.Data.Author, + Body: child.Data.Body, + }) + } + } + return comments, nil +} + +type redditResponse struct { + Data struct { + Children []struct { + Data struct { + Author string `json:"author"` + Title string `json:"title,omitempty"` + Selftext string `json:"selftext,omitempty"` + Body string `json:"body,omitempty"` + } `json:"data"` + } `json:"children"` + } `json:"data"` +}