Skip to content

Commit

Permalink
Add reddit source (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefMa authored May 15, 2024
1 parent 0d22331 commit 5aef0aa
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions source/reddit.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 5aef0aa

Please sign in to comment.