From 34827fe85292838f7d6fbcd4d8c6e7b225017c86 Mon Sep 17 00:00:00 2001 From: stefanodvx <69367859+stefanodvx@users.noreply.github.com> Date: Tue, 22 Apr 2025 12:06:13 +0200 Subject: [PATCH] reddit: prevent infinite loop on error --- ext/reddit/main.go | 9 +++++++-- ext/reddit/models.go | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ext/reddit/main.go b/ext/reddit/main.go index a3de250..c02df5e 100644 --- a/ext/reddit/main.go +++ b/ext/reddit/main.go @@ -88,7 +88,7 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) { contentID := ctx.MatchedContentID contentURL := ctx.MatchedContentURL - manifest, err := GetRedditData(client, host, slug) + manifest, err := GetRedditData(client, host, slug, false) if err != nil { return nil, err } @@ -100,6 +100,7 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) { data := manifest[0].Data.Children[0].Data title := data.Title isNsfw := data.Over18 + var mediaList []*models.Media if !data.IsVideo { @@ -228,6 +229,7 @@ func GetRedditData( client models.HTTPClient, host string, slug string, + raise bool, ) (RedditResponse, error) { url := fmt.Sprintf("https://%s/%s/.json", host, slug) @@ -252,13 +254,16 @@ func GetRedditData( defer res.Body.Close() if res.StatusCode != http.StatusOK { + if raise { + return nil, fmt.Errorf("failed to get reddit data: %s", res.Status) + } // try with alternative domain altHost := "old.reddit.com" if host == "old.reddit.com" { altHost = "www.reddit.com" } - return GetRedditData(client, altHost, slug) + return GetRedditData(client, altHost, slug, true) } var response RedditResponse diff --git a/ext/reddit/models.go b/ext/reddit/models.go index b074ce0..e9ebc29 100644 --- a/ext/reddit/models.go +++ b/ext/reddit/models.go @@ -1,13 +1,19 @@ package reddit -type RedditResponse []struct { - Data struct { - Children []struct { - Data PostData `json:"data"` - } `json:"children"` - } `json:"data"` +type Child struct { + Data *PostData `json:"data"` } +type Data struct { + Children []*Child `json:"children"` +} + +type ResponseItem struct { + Data *Data `json:"data"` +} + +type RedditResponse []*ResponseItem + type PostData struct { ID string `json:"id"` Title string `json:"title"`