-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |